From 598bf19f7099978a1c67350b299c189491c2de6f Mon Sep 17 00:00:00 2001 From: Ahmed Ilyas Date: Tue, 14 Nov 2023 12:11:04 +0100 Subject: [PATCH] Adopt ruff (#178) * chore: adopt ruff * fix: linting errors * chore: update github actions * chore: remove pylint config --- .github/workflows/python-actions.yaml | 9 +- .pylintrc | 371 -------------------- README.md | 3 +- electronicparsers/cp2k/parser.py | 2 +- electronicparsers/quantumespresso/parser.py | 2 +- electronicparsers/utils/utils.py | 2 +- pyproject.toml | 34 +- 7 files changed, 36 insertions(+), 387 deletions(-) delete mode 100644 .pylintrc diff --git a/.github/workflows/python-actions.yaml b/.github/workflows/python-actions.yaml index 65319501..fddd7ef9 100644 --- a/.github/workflows/python-actions.yaml +++ b/.github/workflows/python-actions.yaml @@ -21,15 +21,12 @@ jobs: - name: Install dependencies run: | pip install --upgrade pip - pip install .[tests] + pip install .[dev] pip install ./nomad[parsing,infrastructure] pip install types-PyYAML - - name: pycodestyle + - name: ruff run: | - python -m pycodestyle --ignore=E501,E701,E731 *parsers tests - - name: pylint - run: | - python -m pylint *parsers tests + python -m ruff check . - name: mypy run: | python -m mypy --ignore-missing-imports --follow-imports=silent --no-strict-optional *parsers tests diff --git a/.pylintrc b/.pylintrc deleted file mode 100644 index 565285a1..00000000 --- a/.pylintrc +++ /dev/null @@ -1,371 +0,0 @@ -[MASTER] - -# A comma-separated list of package or module names from where C extensions may -# be loaded. Extensions are loading into the active Python interpreter and may -# run arbitrary code. -extension-pkg-whitelist=pydantic - -# List of plugins (as comma separated values of python module names) to load, -# usually to register additional checkers. -load-plugins=nomad.metainfo.pylint_plugin - - -[MESSAGES CONTROL] - -# Disable the message, report, category or checker with the given id(s). You -# can either give multiple identifiers separated by comma (,) or put this -# option multiple times (only on the command line, not in the configuration -# file where it should appear only once). You can also use "--disable=all" to -# disable everything first and then reenable specific checks. For example, if -# you want to run only the similarities checker, you can use "--disable=all -# --enable=similarities". If you want to run only the classes checker, but have -# no Warning level messages displayed, use "--disable=all --enable=classes -# --disable=W". -disable=blacklisted-name, - unspecified-encoding, - invalid-name, - missing-docstring, - empty-docstring, - unneeded-not, - singleton-comparison, - unidiomatic-typecheck, - consider-using-enumerate, - consider-iterating-dictionary, - consider-using-f-string, - bad-classmethod-argument, - bad-mcs-method-argument, - bad-mcs-classmethod-argument, - single-string-used-for-slots, - line-too-long, - too-many-lines, - trailing-whitespace, - missing-final-newline, - trailing-newlines, - multiple-statements, - superfluous-parens, - bad-whitespace, - mixed-line-endings, - unexpected-line-ending-format, - bad-continuation, - wrong-spelling-in-comment, - wrong-spelling-in-docstring, - invalid-characters-in-docstring, - multiple-imports, - wrong-import-order, - ungrouped-imports, - wrong-import-position, - useless-import-alias, - import-outside-toplevel, - len-as-condition, - raw-checker-failed, - bad-inline-option, - locally-disabled, - file-ignored, - suppressed-message, - useless-suppression, - deprecated-pragma, - use-symbolic-message-instead, - c-extension-no-member, - literal-comparison, - comparison-with-itself, - no-self-use, - no-classmethod-decorator, - no-staticmethod-decorator, - useless-object-inheritance, - cyclic-import, - duplicate-code, - too-many-ancestors, - too-many-instance-attributes, - too-few-public-methods, - too-many-public-methods, - too-many-return-statements, - too-many-branches, - too-many-arguments, - too-many-locals, - too-many-statements, - too-many-boolean-expressions, - consider-merging-isinstance, - too-many-nested-blocks, - simplifiable-if-statement, - redefined-argument-from-local, - no-else-return, - consider-using-ternary, - trailing-comma-tuple, - stop-iteration-return, - simplify-boolean-expression, - inconsistent-return-statements, - useless-return, - consider-swap-variables, - consider-using-join, - consider-using-in, - consider-using-get, - chained-comparison, - consider-using-dict-comprehension, - consider-using-set-comprehension, - dangerous-default-value, - pointless-statement, - pointless-string-statement, - expression-not-assigned, - unnecessary-pass, - unnecessary-lambda, - assign-to-new-keyword, - useless-else-on-loop, - exec-used, - eval-used, - confusing-with-statement, - using-constant-test, - comparison-with-callable, - lost-exception, - assert-on-tuple, - attribute-defined-outside-init, - bad-staticmethod-argument, - protected-access, - arguments-differ, - signature-differs, - abstract-method, - super-init-not-called, - no-init, - non-parent-init-called, - useless-super-delegation, - bad-indentation, - mixed-indentation, - wildcard-import, - deprecated-module, - reimported, - import-self, - misplaced-future, - fixme, - global-variable-undefined, - global-statement, - global-at-module-level, - unused-argument, - unused-wildcard-import, - redefined-outer-name, - redefined-builtin, - undefined-loop-variable, - cell-var-from-loop, - possibly-unused-variable, - self-cls-assignment, - bare-except, - broad-except, - duplicate-except, - try-except-raise, - raising-format-tuple, - keyword-arg-before-vararg, - logging-not-lazy, - logging-format-interpolation, - logging-fstring-interpolation, - bad-format-string-key, - unused-format-string-key, - missing-format-argument-key, - unused-format-string-argument, - format-combined-specification, - missing-format-attribute, - invalid-format-index, - anomalous-unicode-escape-in-string, - boolean-datetime, - redundant-unittest-assert, - deprecated-method, - bad-thread-instantiation, - shallow-copy-environ, - invalid-envvar-default, - subprocess-popen-preexec-fn, - apply-builtin, - basestring-builtin, - buffer-builtin, - cmp-builtin, - coerce-builtin, - execfile-builtin, - file-builtin, - long-builtin, - raw_input-builtin, - reduce-builtin, - standarderror-builtin, - unicode-builtin, - xrange-builtin, - coerce-method, - delslice-method, - getslice-method, - setslice-method, - no-absolute-import, - old-division, - dict-iter-method, - dict-view-method, - next-method-called, - metaclass-assignment, - indexing-exception, - raising-string, - reload-builtin, - oct-method, - hex-method, - nonzero-method, - cmp-method, - input-builtin, - round-builtin, - intern-builtin, - unichr-builtin, - map-builtin-not-iterating, - zip-builtin-not-iterating, - range-builtin-not-iterating, - filter-builtin-not-iterating, - using-cmp-argument, - div-method, - idiv-method, - rdiv-method, - exception-message-attribute, - invalid-str-codec, - sys-max-int, - bad-python3-import, - deprecated-string-function, - deprecated-str-translate-call, - deprecated-itertools-function, - deprecated-types-field, - next-method-defined, - dict-items-not-iterating, - dict-keys-not-iterating, - dict-values-not-iterating, - deprecated-operator-function, - deprecated-urllib-function, - xreadlines-attribute, - deprecated-sys-function, - exception-escape, - comprehension-escape, - mongoengine-placeholder, - # TODO: These were temporarily added when upgrading to newer pylint version that - # catches these new linting issues. Should be addressed and removed. - self-assigning-variable, - use-dict-literal, - unnecessary-comprehension, - use-sequence-for-iteration, - f-string-without-interpolation, - super-with-arguments, - used-before-assignment, - consider-using-dict-items, - no-else-continue, - arguments-renamed, - consider-using-with, - raise-missing-from, - no-else-break - -# Enable the message, report, category or checker with the given id(s). You can -# either give multiple identifier separated by comma (,) or put this option -# multiple time (only on the command line, not in the configuration file where -# it should appear only once). See also the "--disable" option for examples. -enable=syntax-error, - unrecognized-inline-option, - bad-option-value, - init-is-generator, - return-in-init, - function-redefined, - not-in-loop, - return-outside-function, - yield-outside-function, - return-arg-in-generator, - nonexistent-operator, - duplicate-argument-name, - abstract-class-instantiated, - bad-reversed-sequence, - too-many-star-expressions, - invalid-star-assignment-target, - star-needs-assignment-target, - nonlocal-and-global, - continue-in-finally, - nonlocal-without-binding, - used-prior-global-declaration, - misplaced-format-function, - method-hidden, - access-member-before-definition, - no-method-argument, - no-self-argument, - invalid-slots-object, - assigning-non-slot, - invalid-slots, - inherit-non-class, - inconsistent-mro, - duplicate-bases, - non-iterator-returned, - unexpected-special-method-signature, - invalid-length-returned, - import-error, - relative-beyond-top-level, - used-before-assignment, - undefined-variable, - undefined-all-variable, - invalid-all-object, - no-name-in-module, - unbalanced-tuple-unpacking, - unpacking-non-sequence, - bad-except-order, - raising-bad-type, - bad-exception-context, - misplaced-bare-raise, - raising-non-exception, - notimplemented-raised, - catching-non-exception, - bad-super-call, - no-member, - not-callable, - assignment-from-no-return, - no-value-for-parameter, - too-many-function-args, - unexpected-keyword-arg, - redundant-keyword-arg, - missing-kwoa, - invalid-sequence-index, - invalid-slice-index, - assignment-from-none, - not-context-manager, - invalid-unary-operand-type, - unsupported-binary-operation, - repeated-keyword, - not-an-iterable, - not-a-mapping, - unsupported-membership-test, - unsubscriptable-object, - unsupported-assignment-operation, - unsupported-delete-operation, - invalid-metaclass, - unhashable-dict-key, - logging-unsupported-format, - logging-format-truncated, - logging-too-many-args, - logging-too-few-args, - bad-format-character, - truncated-format-string, - mixed-format-string, - format-needs-mapping, - missing-format-string-key, - too-many-format-args, - too-few-format-args, - bad-str-strip-call, - invalid-envvar-value, - print-statement, - parameter-unpacking, - unpacking-in-except, - old-raise-syntax, - backtick, - import-star-module-level, - yield-inside-async-function, - not-async-context-manager, - fatal, - astroid-error, - parse-error, - method-check-failed, - unreachable, - duplicate-key, - unnecessary-semicolon, - global-variable-not-assigned, - unused-import, - unused-variable, - binary-op-exception, - bad-format-string, - anomalous-backslash-in-string, - bad-open-mode - - -[TYPECHECK] - -# List of class names for which member attributes should not be checked (useful -# for classes with dynamically set attributes). This supports the use of -# qualified names. -ignored-classes=optparse.Values,thread._local,_thread._local,SearchResponse diff --git a/README.md b/README.md index d2ebe279..e81f4a21 100644 --- a/README.md +++ b/README.md @@ -77,6 +77,7 @@ nomad parse --show-archive ``` To parse a file in Python, you can program something like this: + ```python import sys from nomad.cli.parse import parse, normalize_all @@ -113,7 +114,7 @@ Clone the parser project and install it in development mode: ``` git clone https://github.com/nomad-coe/electronic-parsers.git electronic-parsers -pip install -e electronic-parsers +pip install -e "electronic-parsers[dev]" ``` Running the parser now, will use the parser's Python code from the clone project. diff --git a/electronicparsers/cp2k/parser.py b/electronicparsers/cp2k/parser.py index edb19d54..3c172302 100644 --- a/electronicparsers/cp2k/parser.py +++ b/electronicparsers/cp2k/parser.py @@ -850,7 +850,7 @@ def _normalize_filename(self, filename): if filename.startswith('='): filename = filename[1:] elif re.match(r'./', filename): - filename = filename + pass else: project_name = self.inp_parser.get('GLOBAL/PROJECT_NAME') if filename: diff --git a/electronicparsers/quantumespresso/parser.py b/electronicparsers/quantumespresso/parser.py index 513c9107..aaee2232 100644 --- a/electronicparsers/quantumespresso/parser.py +++ b/electronicparsers/quantumespresso/parser.py @@ -2225,7 +2225,7 @@ def parse_scc(self, run, calculation): homo = calculation.get('homo_lumo') if homo is not None: lumo = None - if isinstance(homo, np.ndarray) or isinstance(homo, list): + if isinstance(homo, (list, np.ndarray)): homo, lumo = homo sec_energy.highest_occupied = [float(homo)] * ureg.eV if lumo is not None: diff --git a/electronicparsers/utils/utils.py b/electronicparsers/utils/utils.py index 477fbc16..5b7fce02 100644 --- a/electronicparsers/utils/utils.py +++ b/electronicparsers/utils/utils.py @@ -74,7 +74,7 @@ def numpy_type_to_json_serializable(quantity: Union[np.bool_, np.int32, np.int64 """ if isinstance(quantity, np.bool_): return bool(quantity) - if isinstance(quantity, np.int32) or isinstance(quantity, np.int64): + if isinstance(quantity, (np.int32, np.int64)): return int(quantity) if isinstance(quantity, np.float64): return float(quantity) diff --git a/pyproject.toml b/pyproject.toml index 12346074..720880f0 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta" [project] name = 'electronicparsers' -version = '1.0' +version = '1.1' description = 'Collection of NOMAD parsers for electronic structure codes.' readme = "README.md" authors = [{ name = "The NOMAD Authors" }] @@ -22,18 +22,40 @@ dependencies = [ homepage = "https://github.com/nomad-coe/electronic-parsers" [project.optional-dependencies] -tests = [ +dev = [ 'mypy==1.0.1', - 'pylint==2.13.9', - 'pylint_plugin_utils==0.5', - 'pycodestyle==2.8.0', 'pytest==3.10.0', 'pytest-timeout==1.4.2', 'pytest-cov==2.7.1', 'astroid==2.11.7', - 'typing-extensions==4.4.0' + 'typing-extensions==4.4.0', + 'ruff==0.1.4' ] +[tool.ruff] +include = ["electronicparsers/*.py", "tests/*.py"] +select = [ + "E", # pycodestyle + "W", # pycodestyle + "PL", # pylint +] +ignore = [ + "E501", # Line too long ({width} > {limit} characters) + "E701", # Multiple statements on one line (colon) + "E731", # Do not assign a lambda expression, use a def + "E402", # Module level import not at top of file + "PLR0911", # Too many return statements + "PLR0912", # Too many branches + "PLR0913", # Too many arguments in function definition + "PLR0915", # Too many statements + "PLR2004", # Magic value used instead of constant + "PLW0603", # Using the global statement + "PLW2901", # redefined-loop-name + "PLR1714", # consider-using-in + "PLR5501", # else-if-used +] +fixable = ["ALL"] + [tool.setuptools.packages.find] include = ["electronicparsers*"]