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.
This commit is contained in:
Régis Behmo 2023-02-06 22:10:33 +01:00 committed by Régis Behmo
parent 4b14d20c5e
commit 561460ec7f
6 changed files with 19 additions and 38 deletions

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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.
"""

View File

@ -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.
"""

View File

@ -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"
)