From 561460ec7fddc25a731f3cfcfb077376f4b608be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9gis=20Behmo?= Date: Mon, 6 Feb 2023 22:10:33 +0100 Subject: [PATCH] chore: upgrade to mypy 1.0.0 Now that mypy 1.0.0 is out, we can get add some elegant type aliases for filter and action callback functions. --- requirements/base.txt | 4 ++-- requirements/dev.txt | 10 +++------- requirements/docs.txt | 8 ++------ tutor/core/hooks/actions.py | 12 +++++------- tutor/core/hooks/filters.py | 21 ++++++--------------- tutor/hooks/catalog.py | 2 +- 6 files changed, 19 insertions(+), 38 deletions(-) diff --git a/requirements/base.txt b/requirements/base.txt index 9a0c9df..49d9abe 100644 --- a/requirements/base.txt +++ b/requirements/base.txt @@ -1,5 +1,5 @@ # -# This file is autogenerated by pip-compile with Python 3.8 +# This file is autogenerated by pip-compile with Python 3.10 # by the following command: # # pip-compile requirements/base.in @@ -26,7 +26,7 @@ kubernetes==25.3.0 # via -r requirements/base.in markupsafe==2.1.1 # via jinja2 -mypy==0.991 +mypy==1.0.0 # via -r requirements/base.in mypy-extensions==0.4.3 # via mypy diff --git a/requirements/dev.txt b/requirements/dev.txt index caa9d45..04e9c70 100644 --- a/requirements/dev.txt +++ b/requirements/dev.txt @@ -1,6 +1,6 @@ # -# This file is autogenerated by pip-compile with python 3.8 -# To update, run: +# This file is autogenerated by pip-compile with Python 3.10 +# by the following command: # # pip-compile requirements/dev.in # @@ -92,7 +92,7 @@ mccabe==0.7.0 # via pylint more-itertools==9.0.0 # via jaraco-classes -mypy==0.991 +mypy==1.0.0 # via -r requirements/base.txt mypy-extensions==0.4.3 # via @@ -204,11 +204,7 @@ types-setuptools==65.6.0.2 typing-extensions==4.4.0 # via # -r requirements/base.txt - # astroid - # black # mypy - # pylint - # rich urllib3==1.26.13 # via # -r requirements/base.txt diff --git a/requirements/docs.txt b/requirements/docs.txt index afd1b36..3683c6d 100644 --- a/requirements/docs.txt +++ b/requirements/docs.txt @@ -1,5 +1,5 @@ # -# This file is autogenerated by pip-compile with Python 3.8 +# This file is autogenerated by pip-compile with Python 3.10 # by the following command: # # pip-compile requirements/docs.in @@ -42,8 +42,6 @@ idna==3.4 # requests imagesize==1.4.1 # via sphinx -importlib-metadata==6.0.0 - # via sphinx jinja2==3.1.2 # via # -r requirements/base.txt @@ -54,7 +52,7 @@ markupsafe==2.1.1 # via # -r requirements/base.txt # jinja2 -mypy==0.991 +mypy==1.0.0 # via -r requirements/base.txt mypy-extensions==0.4.3 # via @@ -149,8 +147,6 @@ websocket-client==1.4.2 # via # -r requirements/base.txt # kubernetes -zipp==3.11.0 - # via importlib-metadata # The following packages are considered to be unsafe in a requirements file: # setuptools diff --git a/tutor/core/hooks/actions.py b/tutor/core/hooks/actions.py index 7562181..e6050d9 100644 --- a/tutor/core/hooks/actions.py +++ b/tutor/core/hooks/actions.py @@ -14,15 +14,13 @@ from .contexts import Contextualized #: Action generic signature. T = ParamSpec("T") -# Similarly to CallableFilter, it should be possible to create a CallableAction alias in -# the future. -# CallableAction = t.Callable[T, None] +ActionCallbackFunc = t.Callable[T, None] class ActionCallback(Contextualized, t.Generic[T]): def __init__( self, - func: t.Callable[T, None], + func: ActionCallbackFunc[T], priority: t.Optional[int] = None, ): super().__init__() @@ -75,7 +73,7 @@ class Action(t.Generic[T]): def add( self, priority: t.Optional[int] = None - ) -> t.Callable[[t.Callable[T, None]], t.Callable[T, None]]: + ) -> t.Callable[[ActionCallbackFunc[T]], ActionCallbackFunc[T]]: """ Decorator to add a callback to an action. @@ -97,7 +95,7 @@ class Action(t.Generic[T]): to return any value. Returned values will be ignored. """ - def inner(func: t.Callable[T, None]) -> t.Callable[T, None]: + def inner(func: ActionCallbackFunc[T]) -> ActionCallbackFunc[T]: callback = ActionCallback(func, priority=priority) priorities.insert_callback(callback, self.callbacks) return func @@ -218,7 +216,7 @@ def get_template(name: str) -> ActionTemplate[t.Any]: def add( name: str, priority: t.Optional[int] = None -) -> t.Callable[[t.Callable[T, None]], t.Callable[T, None]]: +) -> t.Callable[[ActionCallbackFunc[T]], ActionCallbackFunc[T]]: """ Decorator to add a callback action associated to a name. """ diff --git a/tutor/core/hooks/filters.py b/tutor/core/hooks/filters.py index f2b505e..90518d9 100644 --- a/tutor/core/hooks/filters.py +++ b/tutor/core/hooks/filters.py @@ -17,16 +17,13 @@ T2 = ParamSpec("T2") #: Specialized typevar for list elements L = t.TypeVar("L") -# I wish we could create such an alias, which would greatly simply the definitions -# below. Unfortunately this does not work, yet. It will once the following issue is -# resolved: https://github.com/python/mypy/issues/11855 -# CallableFilter = t.Callable[Concatenate[T1, T2], T1] +FilterCallbackFunc = t.Callable[Concatenate[T1, T2], T1] class FilterCallback(contexts.Contextualized, t.Generic[T1, T2]): def __init__( self, - func: t.Callable[Concatenate[T1, T2], T1], + func: FilterCallbackFunc[T1, T2], priority: t.Optional[int] = None, ): super().__init__() @@ -83,9 +80,7 @@ class Filter(t.Generic[T1, T2]): def add( self, priority: t.Optional[int] = None - ) -> t.Callable[ - [t.Callable[Concatenate[T1, T2], T1]], t.Callable[Concatenate[T1, T2], T1] - ]: + ) -> t.Callable[[FilterCallbackFunc[T1, T2]], FilterCallbackFunc[T1, T2]]: """ Decorator to add a filter callback. @@ -112,10 +107,8 @@ class Filter(t.Generic[T1, T2]): final_value = my_filter.apply(initial_value, some_other_argument_value) """ - def inner( - func: t.Callable[Concatenate[T1, T2], T1] - ) -> t.Callable[Concatenate[T1, T2], T1]: - callback = FilterCallback(func, priority=priority) + def inner(func: FilterCallbackFunc[T1, T2]) -> FilterCallbackFunc[T1, T2]: + callback: FilterCallback[T1, T2] = FilterCallback(func, priority=priority) priorities.insert_callback(callback, self.callbacks) return func @@ -318,9 +311,7 @@ def get_template(name: str) -> FilterTemplate[t.Any, t.Any]: def add( name: str, priority: t.Optional[int] = None -) -> t.Callable[ - [t.Callable[Concatenate[T1, T2], T1]], t.Callable[Concatenate[T1, T2], T1] -]: +) -> t.Callable[[FilterCallbackFunc[T1, T2]], FilterCallbackFunc[T1, T2]]: """ Decorator for functions that will be applied to a single named filter. """ diff --git a/tutor/hooks/catalog.py b/tutor/hooks/catalog.py index cb45f1f..7e9a239 100644 --- a/tutor/hooks/catalog.py +++ b/tutor/hooks/catalog.py @@ -212,7 +212,7 @@ class Filters: #: - ``path`` is a tuple that corresponds to a template relative path. #: Example: ``("myplugin", "hooks", "myservice", "pre-init")`` (see :py:data:`IMAGES_BUILD`). #: The command to execute will be read from that template, after it is rendered. - COMMANDS_INIT: Filter[list[tuple[str, tuple[str, ...]]], str] = filters.get( + COMMANDS_INIT: Filter[list[tuple[str, tuple[str, ...]]], []] = filters.get( "commands:init" )