Skip to content

Commit

Permalink
chore: Switch from pylint to ruff (#82)
Browse files Browse the repository at this point in the history
  • Loading branch information
danielhollas authored Nov 22, 2024
1 parent 7269ab5 commit 801cbb9
Show file tree
Hide file tree
Showing 18 changed files with 127 additions and 115 deletions.
13 changes: 6 additions & 7 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,14 @@ repos:
args: ['-i']
additional_dependencies: [toml]

- repo: local
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.7.4
hooks:
- id: ruff
args: [--fix, --exit-non-zero-on-fix, --show-fixes]

- id: pylint
name: pylint
entry: pylint
types: [file, python]
language: system
exclude: '^(docs/)|(examples/)'
- repo: local
hooks:

- id: mypy
name: mypy
Expand Down
1 change: 0 additions & 1 deletion aiida_test_cache/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
"""A pytest plugin for testing AiiDA plugins."""

__version__ = '0.1.0.dev1'
5 changes: 2 additions & 3 deletions aiida_test_cache/_config.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
# -*- coding: utf-8 -*-
"""
Helpers for managing the ``.aiida-test-cache-config.yml`` configuration file.
"""

import collections
import pathlib
import typing as ty
import collections
from enum import Enum
from voluptuous import Schema

import yaml
from voluptuous import Schema

CONFIG_FILE_NAME = '.aiida-test-cache-config.yml'

Expand Down
2 changes: 1 addition & 1 deletion aiida_test_cache/archive_cache/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# -*- coding: utf-8 -*-
"""
Defines fixtures for automatically creating / loading an AiiDA DB export,
to enable AiiDA - level caching.
"""
# ruff: noqa: F403, F405

from ._fixtures import *

Expand Down
38 changes: 25 additions & 13 deletions aiida_test_cache/archive_cache/_fixtures.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,36 @@
# -*- coding: utf-8 -*-
"""
Defines pytest fixtures for automatically enable caching in tests and create aiida archives if not existent.
Meant to be useful for WorkChain tests.
"""
# pylint: disable=unused-argument, protected-access, redefined-outer-name

import os
import pathlib
from contextlib import contextmanager
import shutil
import typing as ty
from contextlib import contextmanager

import pytest

from aiida import plugins
from aiida.common.links import LinkType
from aiida.orm import Code, Dict, SinglefileData, List, FolderData, RemoteData, StructureData
from aiida.orm import CalcJobNode, QueryBuilder
from aiida.manage.caching import enable_caching
from aiida.orm import (
CalcJobNode,
Code,
Dict,
FolderData,
List,
QueryBuilder,
RemoteData,
SinglefileData,
StructureData,
)

from ._utils import monkeypatch_hash_objects, get_node_from_hash_objects_caller
from ._utils import load_node_archive, create_node_archive
from .._config import Config
from ._utils import (
create_node_archive,
get_node_from_hash_objects_caller,
load_node_archive,
monkeypatch_hash_objects,
)

__all__ = (
"pytest_addoption", "absolute_archive_path", "enable_archive_cache", "liberal_hash",
Expand Down Expand Up @@ -146,7 +155,9 @@ def _absolute_archive_path(

@pytest.fixture(scope='function')
def enable_archive_cache(
liberal_hash: None, archive_cache_forbid_migration: bool, archive_cache_overwrite: bool,
liberal_hash: None, # noqa: ARG001
archive_cache_forbid_migration: bool,
archive_cache_overwrite: bool,
absolute_archive_path: ty.Callable
) -> ty.Callable:
"""
Expand Down Expand Up @@ -225,11 +236,12 @@ def liberal_hash(monkeypatch: pytest.MonkeyPatch, testing_config: Config) -> Non

#Load the corresponding entry points
node_ignored_attributes = {
plugins.DataFactory(entry_point): tuple(set(ignored)) + ('version', )
plugins.DataFactory(entry_point): (*tuple(set(ignored)), "version")
for entry_point, ignored in hash_ignore_config.get('node_attributes', {}).items()
}
calcjob_ignored_attributes = tuple(hash_ignore_config.get('calcjob_attributes', [])
) + ('version', )
calcjob_ignored_attributes = (
*tuple(hash_ignore_config.get("calcjob_attributes", [])), "version"
)
calcjob_ignored_inputs = tuple(hash_ignore_config.get('calcjob_inputs', []))

def mock_objects_to_hash_code(self):
Expand Down
27 changes: 12 additions & 15 deletions aiida_test_cache/archive_cache/_utils.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
"""
Defines helper functions for the archive_cache pytest fixtures
"""
import typing as ty
from functools import partial
import os
import pathlib
import tempfile
import os
import typing as ty
from functools import partial

import pytest

from aiida.orm import ProcessNode, QueryBuilder, Node
from aiida.cmdline.utils.echo import echo_warning
from aiida.orm import Node, ProcessNode, QueryBuilder

__all__ = ('rehash_processes', 'monkeypatch_hash_objects')

Expand Down Expand Up @@ -47,8 +46,6 @@ def monkeypatch_hash_objects(
monkeypatched on the Nodes, i.e. the _CLS_NODE_CACHING attribute
"""
#pylint: disable=too-few-public-methods,protected-access

try:
monkeypatch.setattr(node_class, "_get_objects_to_hash", hash_objects_func)
except AttributeError:
Expand All @@ -75,15 +72,14 @@ def get_node_from_hash_objects_caller(caller: ty.Any) -> Node:
try:
#Case for AiiDA 2.0: The class holding the _get_objects_to_hash method
#is the NodeCaching class not the actual node
return caller._node #type: ignore[no-any-return] #pylint: disable=protected-access
return caller._node #type: ignore[no-any-return]
except AttributeError:
return caller #type: ignore[no-any-return]


#Cross-compatible importing function for import AiiDA archives in 1.X and 2.X
try:
from aiida.tools.archive import create_archive
from aiida.tools.archive import import_archive
from aiida.tools.archive import create_archive, import_archive
import_archive = partial(import_archive, merge_extras=('n', 'c', 'u'), import_new_extras=True)

def import_with_migrate(
Expand All @@ -96,9 +92,8 @@ def import_with_migrate(
Import AiiDA Archive. If the version is incompatible
try to migrate the archive if --archive-cache-forbid-migration option is not specified
"""
#pylint: disable=import-outside-toplevel
from aiida.tools.archive import get_format
from aiida.common.exceptions import IncompatibleStorageSchema
from aiida.tools.archive import get_format

try:
import_archive(archive_path, *args, **kwargs)
Expand Down Expand Up @@ -131,10 +126,12 @@ def import_with_migrate(
Import AiiDA Archive. If the version is incompatible
try to migrate the archive if --archive-cache-forbid-migration option is not specified
"""
#pylint: disable=import-outside-toplevel
from aiida.tools.importexport import EXPORT_VERSION, IncompatibleArchiveVersionError
# these are only availbale after aiida >= 1.5.0, maybe rely on verdi import instead
from aiida.tools.importexport import detect_archive_type
from aiida.tools.importexport import (
EXPORT_VERSION,
IncompatibleArchiveVersionError,
detect_archive_type,
)
from aiida.tools.importexport.archive.migrators import get_migrator # type: ignore[import-not-found]

try:
Expand Down
5 changes: 3 additions & 2 deletions aiida_test_cache/mock_code/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
Defines fixtures for mocking AiiDA codes, with caching at the level of
the executable.
"""
# ruff: noqa: F405

from ._hasher import InputHasher
from ._fixtures import *
from ._fixtures import * # noqa: F403
from ._hasher import InputHasher # noqa: F401

# Note: This is necessary for the sphinx doc - otherwise it does not find aiida_test_cache.mock_code.mock_code_factory
__all__ = (
Expand Down
15 changes: 7 additions & 8 deletions aiida_test_cache/mock_code/_cli.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Implements the executable for running a mock AiiDA code.
"""
from datetime import datetime
import fnmatch
import os
import sys
import shutil
import subprocess
import sys
import typing as ty
import fnmatch
from datetime import datetime
from pathlib import Path

from ._env_keys import MockVariables


def run() -> None: # pylint: disable=too-many-branches
def run() -> None:
"""
Run the mock AiiDA code. If the corresponding result exists, it is
simply copied over to the current working directory. Otherwise,
Expand All @@ -39,12 +38,12 @@ def _log(msg: str, error=False) -> None:

try:
hasher_cls = env.get_hasher()
except Exception as exc: # pylint: disable=broad-except
except Exception as exc:
_log(f"loading hasher: {exc}", error=True)

try:
hash_digest = hasher_cls(env, _log)(Path('.'))
except Exception as exc: # pylint: disable=broad-except
except Exception as exc:
_log(f"computing hash: {exc}", error=True)

res_dir = env.data_dir / f"mock-{env.label}-{hash_digest}"
Expand Down Expand Up @@ -107,7 +106,7 @@ def copy_files(
# accessing its content, hence using os.walk.
for dirpath, _, filenames in os.walk(src_dir):
relative_dir = Path(dirpath).relative_to(src_dir)
dirs_to_check = list(relative_dir.parents) + [relative_dir]
dirs_to_check = [*list(relative_dir.parents), relative_dir]

if relative_dir.parts and relative_dir.parts[0] == ('.aiida'):
continue
Expand Down
9 changes: 4 additions & 5 deletions aiida_test_cache/mock_code/_env_keys.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
# -*- coding: utf-8 -*-
"""
Defines the environment variable names for the mock code execution.
"""
from dataclasses import dataclass
from enum import Enum
import inspect
import os
from pathlib import Path
import typing as ty
from dataclasses import dataclass
from enum import Enum
from pathlib import Path

from ._hasher import InputHasher, load_hasher


@dataclass
class MockVariables: # pylint: disable=too-many-instance-attributes
class MockVariables:
"""
A class containing variables defined for the mock code execution.
"""
Expand Down
31 changes: 15 additions & 16 deletions aiida_test_cache/mock_code/_fixtures.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,24 @@
# -*- coding: utf-8 -*-
"""
Defines a pytest fixture for creating mock AiiDA codes.
"""

import uuid
import shutil
import collections
import os
import pathlib
import shutil
import typing as ty
import uuid
import warnings
import collections
import os
from pkg_resources import parse_version

import click
import pytest

from aiida.orm import Code
from aiida import __version__ as aiida_version
from aiida.orm import Code
from pkg_resources import parse_version

from .._config import CONFIG_FILE_NAME, Config, ConfigActions
from ._env_keys import MockVariables
from ._hasher import InputHasher
from .._config import Config, CONFIG_FILE_NAME, ConfigActions

__all__ = (
"pytest_addoption",
Expand Down Expand Up @@ -90,7 +88,7 @@ def mock_disable_mpi(request):


@pytest.fixture(scope='session')
def testing_config(testing_config_action): # pylint: disable=redefined-outer-name
def testing_config(testing_config_action):
"""Get content of .aiida-test-cache-config.yml
testing_config_action :
Expand All @@ -110,7 +108,7 @@ def testing_config(testing_config_action): # pylint: disable=redefined-outer-na
def _forget_mpi_decorator(func):
"""Modify :py:meth:`aiida.orm.Code.get_prepend_cmdline_params` to discard MPI parameters."""

def _get_prepend_cmdline_params(self, mpi_args=None, extra_mpirun_params=None): # pylint: disable=unused-argument
def _get_prepend_cmdline_params(self, mpi_args=None, extra_mpirun_params=None): # noqa: ARG001
return func(self)

return _get_prepend_cmdline_params
Expand All @@ -121,7 +119,7 @@ def mock_code_factory(
aiida_localhost, testing_config, testing_config_action, mock_regenerate_test_data,
mock_fail_on_missing, mock_disable_mpi, monkeypatch, request: pytest.FixtureRequest,
tmp_path: pathlib.Path
): # pylint: disable=all
):
"""
Fixture to create a mock AiiDA Code.
Expand All @@ -145,7 +143,7 @@ def _get_mock_code(
_regenerate_test_data: bool = mock_regenerate_test_data,
_fail_on_missing: bool = mock_fail_on_missing,
_disable_mpi: bool = mock_disable_mpi,
): # pylint: disable=too-many-arguments,too-many-branches,too-many-locals
):
"""
Creates a mock AiiDA code. If the same inputs have been run previously,
the results are copied over from the corresponding sub-directory of
Expand Down Expand Up @@ -178,13 +176,14 @@ def _get_mock_code(
If True, regenerate test data instead of reusing.
.. deprecated:: 0.1.0
Keyword `ingore_files` is deprecated and will be removed in `v1.0`. Use `ignore_paths` instead.
Keyword `ignore_files` is deprecated and will be removed in `v1.0`. Use `ignore_paths` instead.
"""
if ignore_files != ('_aiidasubmit.sh', ):
warnings.warn(
'keyword `ignore_files` is deprecated and will be removed in `v1.0`. Use `ignore_paths` instead.',
DeprecationWarning
) # pylint: disable=no-member
DeprecationWarning,
stacklevel=2
)

# It's easy to forget the final comma and pass a string, e.g. `ignore_paths = ('_aiidasubmit.sh')`
for arg in (ignore_paths, ignore_files):
Expand Down
Loading

0 comments on commit 801cbb9

Please sign in to comment.