Skip to content

Commit

Permalink
bump adaptix to v3.0.0b8
Browse files Browse the repository at this point in the history
  • Loading branch information
Tishka17 committed Sep 29, 2024
1 parent 5d3ede5 commit 0a3b173
Show file tree
Hide file tree
Showing 10 changed files with 55 additions and 83 deletions.
6 changes: 3 additions & 3 deletions src/dishka/_adaptix/common.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import TYPE_CHECKING, Any, Callable, Tuple, Type, TypeVar, Union
from typing import TYPE_CHECKING, Any, Callable, TypeVar, Union

K_contra = TypeVar("K_contra", contravariant=True)
V_co = TypeVar("V_co", covariant=True)
Expand All @@ -12,9 +12,9 @@

TypeHint = Any

VarTuple = Tuple[T, ...]
VarTuple = tuple[T, ...]

Catchable = Union[Type[BaseException], VarTuple[Type[BaseException]]]
Catchable = Union[type[BaseException], VarTuple[type[BaseException]]]

# https://github.com/python/typing/issues/684#issuecomment-548203158
if TYPE_CHECKING:
Expand Down
13 changes: 5 additions & 8 deletions src/dishka/_adaptix/feature_requirement.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
import re
import sys
from abc import ABC, abstractmethod
from typing import Any, Iterable
from collections.abc import Iterable
from typing import Any

from .common import VarTuple

Expand All @@ -17,11 +18,11 @@ def _false():


class Requirement(ABC):
__slots__ = ("is_meet", "__bool__", "__dict__")
__slots__ = ("is_met", "__bool__", "__dict__")

def __init__(self):
self.is_meet = self._evaluate()
self.__bool__ = _true if self.is_meet else _false
self.is_met = self._evaluate()
self.__bool__ = _true if self.is_met else _false

@abstractmethod
def _evaluate(self) -> bool:
Expand Down Expand Up @@ -148,10 +149,6 @@ def fail_reason(self) -> str:
return f"{self.implementation_name} is required"


HAS_PY_39 = PythonVersionRequirement((3, 9))
HAS_ANNOTATED = HAS_PY_39
HAS_STD_CLASSES_GENERICS = HAS_PY_39

HAS_PY_310 = PythonVersionRequirement((3, 10))
HAS_TYPE_UNION_OP = HAS_PY_310
HAS_TYPE_GUARD = HAS_PY_310
Expand Down
1 change: 1 addition & 0 deletions src/dishka/_adaptix/type_tools/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@
make_norm_type,
normalize_type,
)
from .type_evaler import exec_type_checking, make_fragments_collector
24 changes: 8 additions & 16 deletions src/dishka/_adaptix/type_tools/basic_utils.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import types
import typing
from typing import Any, Dict, ForwardRef, Generic, NewType, Protocol, TypedDict, TypeVar, Union
from typing import Annotated, Any, ForwardRef, Generic, NewType, Protocol, TypedDict, TypeVar, Union

from ..common import TypeHint, VarTuple
from ..feature_requirement import HAS_ANNOTATED, HAS_PY_39, HAS_PY_312, HAS_STD_CLASSES_GENERICS
from ..feature_requirement import HAS_PY_312
from .constants import BUILTIN_ORIGIN_TO_TYPEVARS
from .fundamentals import get_generic_args, get_type_vars, strip_alias

Expand Down Expand Up @@ -81,16 +81,12 @@ def is_generic(tp: TypeHint) -> bool:
bool(get_type_vars(tp))
or (
strip_alias(tp) in BUILTIN_ORIGIN_TO_TYPEVARS
and tp != type
and tp is not type
and not is_parametrized(tp)
and (
bool(HAS_STD_CLASSES_GENERICS) or not isinstance(tp, type)
)
)
or (
bool(HAS_ANNOTATED)
and strip_alias(tp) == typing.Annotated
and tp != typing.Annotated
strip_alias(tp) == Annotated
and tp != Annotated
and is_generic(tp.__origin__)
)
)
Expand Down Expand Up @@ -129,17 +125,13 @@ def get_type_vars_of_parametrized(tp: TypeHint) -> VarTuple[TypeVar]:
if not params:
return ()
if isinstance(tp, type):
if HAS_STD_CLASSES_GENERICS and isinstance(tp, types.GenericAlias):
if isinstance(tp, types.GenericAlias):
return params
return ()
if strip_alias(tp) != tp and get_generic_args(tp) == ():
return ()
return params


if HAS_PY_39:
def eval_forward_ref(namespace: Dict[str, Any], forward_ref: ForwardRef):
return forward_ref._evaluate(namespace, None, frozenset())
else:
def eval_forward_ref(namespace: Dict[str, Any], forward_ref: ForwardRef):
return forward_ref._evaluate(namespace, None) # type: ignore[call-arg]
def eval_forward_ref(namespace: dict[str, Any], forward_ref: ForwardRef):
return forward_ref._evaluate(namespace, None, recursive_guard=frozenset())
3 changes: 2 additions & 1 deletion src/dishka/_adaptix/type_tools/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
import concurrent.futures
import queue
import re
from collections.abc import Mapping
from os import PathLike
from typing import Mapping, TypeVar
from typing import TypeVar

from ..common import VarTuple

Expand Down
9 changes: 3 additions & 6 deletions src/dishka/_adaptix/type_tools/fundamentals.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from typing import TypeVar, get_args, get_origin, get_type_hints

from ..common import TypeHint, VarTuple
from ..feature_requirement import HAS_ANNOTATED, HAS_SUPPORTED_PYDANTIC_PKG
from ..feature_requirement import HAS_SUPPORTED_PYDANTIC_PKG

__all__ = ("is_pydantic_class", "strip_alias", "get_type_vars", "get_generic_args", "get_all_type_hints")

Expand Down Expand Up @@ -41,8 +41,5 @@ def get_generic_args(tp: TypeHint) -> VarTuple[TypeHint]:
return get_args(tp)


if HAS_ANNOTATED:
def get_all_type_hints(obj, globalns=None, localns=None):
return get_type_hints(obj, globalns, localns, include_extras=True)
else:
get_all_type_hints = get_type_hints
def get_all_type_hints(obj, globalns=None, localns=None):
return get_type_hints(obj, globalns, localns, include_extras=True)
7 changes: 4 additions & 3 deletions src/dishka/_adaptix/type_tools/generic_resolver.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import typing
from collections.abc import Collection, Hashable, Mapping
from dataclasses import dataclass, replace
from itertools import chain
from typing import Callable, Collection, Dict, Generic, Hashable, Mapping, TypeVar
from typing import Callable, Generic, TypeVar

from ..common import TypeHint
from ..feature_requirement import HAS_TV_TUPLE, HAS_UNPACK
from . import get_generic_args
from .basic_utils import get_type_vars, get_type_vars_of_parametrized, is_generic, is_parametrized, strip_alias
from .fundamentals import get_generic_args
from .implicit_params import fill_implicit_params
from .normalize_type import normalize_type

Expand Down Expand Up @@ -85,7 +86,7 @@ def _get_members_by_parents(self, tp) -> MembersStorage[K, M]:
if not hasattr(tp, "__orig_bases__"):
return members_storage

bases_members: Dict[K, TypeHint] = {}
bases_members: dict[K, TypeHint] = {}
for base in reversed(tp.__orig_bases__):
bases_members.update(self.get_resolved_members(base).members)

Expand Down
4 changes: 2 additions & 2 deletions src/dishka/_adaptix/type_tools/implicit_params.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import sys
import typing
from typing import Any, ForwardRef, Tuple, TypeVar
from typing import Any, ForwardRef, TypeVar

from ..common import TypeHint, VarTuple
from ..feature_requirement import HAS_PARAM_SPEC, HAS_TV_TUPLE
Expand All @@ -18,7 +18,7 @@ def _process_type_var(self, type_var) -> TypeHint:
if HAS_PARAM_SPEC and isinstance(type_var, typing.ParamSpec):
return ...
if HAS_TV_TUPLE and isinstance(type_var, typing.TypeVarTuple):
return typing.Unpack[Tuple[Any, ...]]
return typing.Unpack[tuple[Any, ...]]
if type_var.__constraints__:
return create_union(
tuple(
Expand Down
15 changes: 3 additions & 12 deletions src/dishka/_adaptix/type_tools/norm_utils.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
import typing
from dataclasses import InitVar
from typing import ClassVar, Final, TypeVar
from typing import Annotated, ClassVar, Final, TypeVar

from ..feature_requirement import HAS_ANNOTATED, HAS_TYPED_DICT_REQUIRED
from ..feature_requirement import HAS_TYPED_DICT_REQUIRED
from .normalize_type import BaseNormType

_TYPE_TAGS = [Final, ClassVar, InitVar]

if HAS_ANNOTATED:
_TYPE_TAGS.append(typing.Annotated)
_TYPE_TAGS = [Final, ClassVar, InitVar, Annotated]

if HAS_TYPED_DICT_REQUIRED:
_TYPE_TAGS.extend([typing.Required, typing.NotRequired])
Expand All @@ -26,12 +23,6 @@ def strip_tags(norm: BaseNormType) -> BaseNormType:
N = TypeVar("N", bound=BaseNormType)


def strip_annotated(value: N) -> N:
if HAS_ANNOTATED and isinstance(value, BaseNormType) and value.origin == typing.Annotated:
return strip_annotated(value)
return value


def is_class_var(norm: BaseNormType) -> bool:
if norm.origin == ClassVar:
return True
Expand Down
Loading

0 comments on commit 0a3b173

Please sign in to comment.