Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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`

Expand Down
3 changes: 3 additions & 0 deletions pedantic/decorators/fn_deco_in_subprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 6 additions & 5 deletions pedantic/mixins/with_decorated_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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)
Expand Down
11 changes: 11 additions & 0 deletions pedantic/tests/test_with_decorated_methods.py
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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: {}}
3 changes: 1 addition & 2 deletions pedantic/type_checking_logic/check_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down