From d9a200acba8d562cc7d3616be2724adb476b502a Mon Sep 17 00:00:00 2001 From: Sebastiaan Huber Date: Mon, 31 Oct 2022 11:12:30 +0100 Subject: [PATCH] DevOps: Update the `mypy` pre-commit dependency (#246) The requirement is updated to `mypy==0.930`. This allows us to move the configuration from `tox.ini` to the `pyproject.toml` file. The config in `.pre-commit-config.yaml` is also corrected as the include path did not properly include the `src` starting folder. The remaining configuration for `tox.ini` is also moved to the `pyproject.toml` and deleted. --- .pre-commit-config.yaml | 11 ++++-- pyproject.toml | 63 +++++++++++++++++++++++++++++ src/plumpy/__init__.py | 4 +- src/plumpy/base/state_machine.py | 10 ++--- src/plumpy/events.py | 2 +- src/plumpy/mixins.py | 2 +- src/plumpy/persistence.py | 8 ++-- src/plumpy/processes.py | 5 ++- src/plumpy/utils.py | 2 +- tox.ini | 68 -------------------------------- 10 files changed, 88 insertions(+), 87 deletions(-) delete mode 100644 tox.ini diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 9cf325c8..18364c25 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -28,14 +28,17 @@ repos: additional_dependencies: ['toml'] - repo: https://github.com/pre-commit/mirrors-mypy - rev: v0.790 + rev: v0.982 hooks: - id: mypy - args: [--config-file=tox.ini] - additional_dependencies: ['aio_pika~=6.6'] + args: [--config-file=pyproject.toml] + additional_dependencies: [ + 'toml', + 'types-pyyaml', + ] files: > (?x)^( - plumpy/.*py| + src/plumpy/.*py| )$ - repo: https://github.com/PyCQA/pylint diff --git a/pyproject.toml b/pyproject.toml index 42c56364..f31c988b 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -53,6 +53,7 @@ pre-commit = [ 'mypy==0.982', 'pre-commit~=2.2', 'pylint==2.12.2', + 'types-pyyaml' ] tests = [ 'ipykernel==6.12.1', @@ -83,6 +84,28 @@ include_trailing_comma = true line_length = 120 multi_line_output = 3 +[tool.mypy] +show_error_codes = true +disallow_untyped_defs = true +disallow_incomplete_defs = true +check_untyped_defs = true +warn_unused_ignores = true +warn_redundant_casts = true + +[[tool.mypy.overrides]] +module = 'test.*' +check_untyped_defs = false + +[[tool.mypy.overrides]] +module = [ + 'aio_pika.*', + 'aiocontextvars.*', + 'kiwipy.*', + 'nest_asyncio.*', + 'tblib.*', +] +ignore_missing_imports = true + [tool.pylint.format] max-line-length = 120 @@ -118,3 +141,43 @@ column_limit = 120 dedent_closing_brackets = true indent_dictionary_value = false split_arguments_when_comma_terminated = true + +[tool.tox] +legacy_tox_ini = """ +[tox] +envlist = py37 + +[testenv] +usedevelop = true + +[testenv:py{37,38,39,310}] +description = Run the unit tests +extras = + tests +commands = pytest {posargs} + +[testenv:py{37,38,39,310}-pre-commit] +description = Run the style checks and formatting +extras = + pre-commit + tests +commands = pre-commit run {posargs} + +[testenv:docs-{update,clean}] +description = Build the documentation +extras = docs +whitelist_externals = rm +commands = + clean: rm -rf docs/_build + sphinx-build -nW --keep-going -b {posargs:html} docs/source/ docs/_build/{posargs:html} + +[testenv:docs-live] +description = Build the documentation and launch browser (with live updates) +extras = docs +deps = sphinx-autobuild +commands = + sphinx-autobuild \ + --re-ignore _build/.* \ + --port 0 --open-browser \ + -n -b {posargs:html} docs/source/ docs/_build/{posargs:html} +""" diff --git a/src/plumpy/__init__.py b/src/plumpy/__init__.py index 6015a744..a044d5c7 100644 --- a/src/plumpy/__init__.py +++ b/src/plumpy/__init__.py @@ -1,6 +1,6 @@ # -*- coding: utf-8 -*- +# mypy: disable-error-code=name-defined # pylint: disable=undefined-variable -# type: ignore[name-defined] __version__ = '0.21.0' import logging @@ -33,7 +33,7 @@ # for more details class NullHandler(logging.Handler): - def emit(self, record): + def emit(self, record: logging.LogRecord) -> None: pass diff --git a/src/plumpy/base/state_machine.py b/src/plumpy/base/state_machine.py index c9a08848..274ac968 100644 --- a/src/plumpy/base/state_machine.py +++ b/src/plumpy/base/state_machine.py @@ -76,12 +76,12 @@ def event( """A decorator to check for correct transitions, raising ``EventError`` on invalid transitions.""" if from_states != '*': if inspect.isclass(from_states): - from_states = (from_states,) # type: ignore + from_states = (from_states,) if not all(issubclass(state, State) for state in from_states): # type: ignore raise TypeError(f'from_states: {from_states}') if to_states != '*': if inspect.isclass(to_states): - to_states = (to_states,) # type: ignore + to_states = (to_states,) if not all(issubclass(state, State) for state in to_states): # type: ignore raise TypeError(f'to_states: {to_states}') @@ -111,7 +111,7 @@ def transition(self: Any, *a: Any, **kw: Any) -> Any: return transition if inspect.isfunction(from_states): - return wrapper(from_states) # type: ignore + return wrapper(from_states) return wrapper @@ -397,8 +397,8 @@ def _create_state_instance(self, state: Union[Hashable, State, Type[State]], *ar return state_cls(self, *args, **kwargs) def _ensure_state_class(self, state: Union[Hashable, Type[State]]) -> Type[State]: - if inspect.isclass(state) and issubclass(state, State): # type: ignore - return cast(Type[State], state) + if inspect.isclass(state) and issubclass(state, State): + return state try: return self.get_states_map()[cast(Hashable, state)] # pylint: disable=unsubscriptable-object diff --git a/src/plumpy/events.py b/src/plumpy/events.py index 64f5dd9c..735fb3f6 100644 --- a/src/plumpy/events.py +++ b/src/plumpy/events.py @@ -23,7 +23,7 @@ def new_event_loop(*args: Any, **kwargs: Any) -> asyncio.AbstractEventLoop: raise NotImplementedError('this method is not implemented because `plumpy` uses a single reentrant loop') -class PlumpyEventLoopPolicy(asyncio.DefaultEventLoopPolicy): # type: ignore +class PlumpyEventLoopPolicy(asyncio.DefaultEventLoopPolicy): """Custom event policy that always returns the same event loop that is made reentrant by ``nest_asyncio``.""" _loop: Optional[asyncio.AbstractEventLoop] = None diff --git a/src/plumpy/mixins.py b/src/plumpy/mixins.py index 05cb4c11..6302184b 100644 --- a/src/plumpy/mixins.py +++ b/src/plumpy/mixins.py @@ -15,7 +15,7 @@ class ContextMixin(persistence.Savable): CONTEXT: str = '_context' def __init__(self, *args: Any, **kwargs: Any): - super().__init__(*args, **kwargs) # type: ignore + super().__init__(*args, **kwargs) self._context: Optional[AttributesDict] = AttributesDict() @property diff --git a/src/plumpy/persistence.py b/src/plumpy/persistence.py index 7b49a330..b646546d 100644 --- a/src/plumpy/persistence.py +++ b/src/plumpy/persistence.py @@ -73,7 +73,7 @@ def _bundle_constructor(loader: yaml.Loader, data: Any) -> Generator[Bundle, Non yaml.add_representer(Bundle, _bundle_representer) -yaml.add_constructor(_BUNDLE_TAG, _bundle_constructor) +yaml.add_constructor(_BUNDLE_TAG, _bundle_constructor) # type: ignore[arg-type] class Persister(metaclass=abc.ABCMeta): @@ -340,7 +340,7 @@ def delete_process_checkpoints(self, pid: PID_TYPE) -> None: del self._checkpoints[pid] -SavableClsType = TypeVar('SavableClsType', bound='Type[Savable]') +SavableClsType = TypeVar('SavableClsType', bound='Type[Savable]') # type: ignore[name-defined] def auto_persist(*members: str) -> Callable[[SavableClsType], SavableClsType]: @@ -650,5 +650,5 @@ def load_instance_state(self, saved_state: SAVED_STATE_TYPE, load_context: LoadS super().load_instance_state(saved_state, load_context) if self._callbacks: # typing says asyncio.Future._callbacks needs to be called, but in the python 3.7 code it is a simple list - for callback in self._callbacks: # type: ignore - self.remove_done_callback(callback) + for callback in self._callbacks: + self.remove_done_callback(callback) # type: ignore[arg-type] diff --git a/src/plumpy/processes.py b/src/plumpy/processes.py index 41bd3609..0066cb8b 100644 --- a/src/plumpy/processes.py +++ b/src/plumpy/processes.py @@ -72,7 +72,10 @@ class ProcessStateMachineMeta(abc.ABCMeta, state_machine.StateMachineMeta): # Make ProcessStateMachineMeta instances (classes) YAML - able -yaml.representer.Representer.add_representer(ProcessStateMachineMeta, yaml.representer.Representer.represent_name) +yaml.representer.Representer.add_representer( + ProcessStateMachineMeta, + yaml.representer.Representer.represent_name # type: ignore[arg-type] +) def ensure_not_closed(func: Callable[..., Any]) -> Callable[..., Any]: diff --git a/src/plumpy/utils.py b/src/plumpy/utils.py index 91cbe21e..a11ebd01 100644 --- a/src/plumpy/utils.py +++ b/src/plumpy/utils.py @@ -171,7 +171,7 @@ def load_function(name: str, instance: Optional[Any] = None) -> Callable[..., An obj = load_object(name) if inspect.ismethod(obj): if instance is not None: - return obj.__get__(instance, instance.__class__) + return obj.__get__(instance, instance.__class__) # type: ignore[attr-defined] return obj diff --git a/tox.ini b/tox.ini deleted file mode 100644 index 3b47c63b..00000000 --- a/tox.ini +++ /dev/null @@ -1,68 +0,0 @@ -# To use tox, see https://tox.readthedocs.io -# Simply pip or conda install tox -# If you use conda, you may also want to install tox-conda -# then run `tox` or `tox -- {pytest args}` -# run in parallel using `tox -p` - -# see also test/rmq/docker-compose.yml to start a rabbitmq server, required for the those tests - -[tox] -envlist = py37 - -[testenv] -usedevelop = true - -[testenv:py{37,38,39}] -description = Run the unit tests -extras = - tests -commands = pytest {posargs} - -[testenv:py{37,38,39}-pre-commit] -description = Run the style checks and formatting -extras = - pre-commit - tests -commands = pre-commit run {posargs} - -[testenv:docs-{update,clean}] -description = Build the documentation -extras = docs -whitelist_externals = rm -commands = - clean: rm -rf docs/_build - sphinx-build -nW --keep-going -b {posargs:html} docs/source/ docs/_build/{posargs:html} - -[testenv:docs-live] -description = Build the documentation and launch browser (with live updates) -extras = docs -deps = sphinx-autobuild -commands = - sphinx-autobuild \ - --re-ignore _build/.* \ - --port 0 --open-browser \ - -n -b {posargs:html} docs/source/ docs/_build/{posargs:html} - - -[mypy] -show_error_codes = True -disallow_untyped_defs = True -disallow_incomplete_defs = True -check_untyped_defs = True -warn_unused_ignores = True -warn_redundant_casts = True - -[mypy-aiocontextvars.*] -ignore_missing_imports = True - -[mypy-frozendict.*] -ignore_missing_imports = True - -[mypy-kiwipy.*] -ignore_missing_imports = True - -[mypy-nest_asyncio.*] -ignore_missing_imports = True - -[mypy-tblib.*] -ignore_missing_imports = True