diff --git a/CHANGELOG.md b/CHANGELOG.md index aae03b1..64b2e02 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,9 @@ # Changelog +## Pedantic 2.2.3 +- remove support for deprecated `typing.ByteString` +- fix `WithDecoratedMethods` +- use start method `spawn` for `multiprocess` + ## Pedantic 2.2.2 - fix `GenericMixin` diff --git a/pedantic/decorators/fn_deco_in_subprocess.py b/pedantic/decorators/fn_deco_in_subprocess.py index 8809d49..dfc285e 100644 --- a/pedantic/decorators/fn_deco_in_subprocess.py +++ b/pedantic/decorators/fn_deco_in_subprocess.py @@ -4,8 +4,11 @@ from typing import Callable, TypeVar, Any, Awaitable, Optional, Type, Union try: + import multiprocess as mp from multiprocess import Process, Pipe from multiprocess.connection import Connection + + mp.set_start_method(method="spawn", force=True) except ImportError: Process: Optional[Type] = None Pipe: Optional[Type] = None diff --git a/pedantic/mixins/with_decorated_methods.py b/pedantic/mixins/with_decorated_methods.py index 9c7826c..7a86abf 100644 --- a/pedantic/mixins/with_decorated_methods.py +++ b/pedantic/mixins/with_decorated_methods.py @@ -11,13 +11,14 @@ class DecoratorType(StrEnum): The values of this enum are used as property names and the properties are added to the decorated functions. So I would recommend naming them with a leading underscore to keep them private and also write it lowercase. + Example: >>> class Decorators(DecoratorType): ... FOO = '_foo' """ -E = TypeVar('E', bound=DecoratorType) +DecoratorTypeVar = TypeVar('DecoratorTypeVar', bound=DecoratorType) T = TypeVar('T') C = TypeVar('C', bound=Callable) @@ -45,7 +46,7 @@ def fun(f: C) -> C: return decorator -class WithDecoratedMethods(ABC, Generic[E], GenericMixin): +class WithDecoratedMethods(ABC, Generic[DecoratorTypeVar], GenericMixin): """ A mixin that is used to figure out which method is decorated with custom parameterized decorators. Example: @@ -79,12 +80,12 @@ class WithDecoratedMethods(ABC, Generic[E], GenericMixin): } """ - def get_decorated_functions(self) -> dict[E, dict[C, T]]: - decorator_types = self.type_var + def get_decorated_functions(self) -> dict[DecoratorTypeVar, dict[C, T]]: + decorator_types = self.type_vars[DecoratorTypeVar] decorated_functions = {t: dict() for t in decorator_types} # type: ignore for attribute_name in dir(self): - if attribute_name.startswith('__'): + if attribute_name.startswith('__') or attribute_name in ['type_var', 'type_vars']: continue attribute = getattr(self, attribute_name) diff --git a/pedantic/tests/test_with_decorated_methods.py b/pedantic/tests/test_with_decorated_methods.py index b095712..b05325c 100644 --- a/pedantic/tests/test_with_decorated_methods.py +++ b/pedantic/tests/test_with_decorated_methods.py @@ -1,8 +1,10 @@ import unittest from functools import wraps +from typing import TypeVar, Generic from pedantic import DecoratorType, create_decorator, WithDecoratedMethods +T = TypeVar('T') class Decorators(DecoratorType): FOO = '_foo' @@ -96,3 +98,12 @@ def m1(self) -> int: assert instance.get_decorated_functions() == expected assert instance.m1() == 4422 # check that transformation was applied + + def test_with_decorated_methods_can_have_generic_child_class(self): + class MyClass(Generic[T], WithDecoratedMethods[Decorators]): + @foo(42) + def m1(self) -> None: ... + + instance = MyClass[int]() + actual = instance.get_decorated_functions() + assert actual == {Decorators.FOO: {instance.m1: 42}, Decorators.BAR: {}} \ No newline at end of file diff --git a/pedantic/type_checking_logic/check_types.py b/pedantic/type_checking_logic/check_types.py index 32a49b7..293329c 100644 --- a/pedantic/type_checking_logic/check_types.py +++ b/pedantic/type_checking_logic/check_types.py @@ -3,7 +3,7 @@ import types import typing from io import BytesIO, StringIO, BufferedWriter, TextIOWrapper -from typing import Any, Dict, Iterable, ItemsView, Callable, Union, Optional, Tuple, Mapping, TypeVar, NewType, \ +from typing import Any, Dict, Iterable, ItemsView, Callable, Optional, Tuple, Mapping, TypeVar, NewType, \ _ProtocolMeta import collections @@ -965,7 +965,6 @@ def _instancecheck_type(value: Any, type_: Any, type_vars: Dict, context: Dict[s 'typing.Sequence': _instancecheck_iterable, 'typing.Iterable': _instancecheck_iterable, 'typing.MutableSequence': _instancecheck_iterable, - 'typing.ByteString': _instancecheck_iterable, 'typing.Deque': _instancecheck_iterable, 'typing.List': _instancecheck_iterable, 'typing.Set': _instancecheck_iterable, diff --git a/pyproject.toml b/pyproject.toml index a59938b..92ecf82 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta" [project] name = "pedantic" -version = "2.2.2" +version = "2.2.3" description = "Some useful Python decorators for cleaner software development." readme = "README.md" requires-python = ">=3.11"