Skip to content

Commit

Permalink
refactor(generics): try using Generic[args] to make generics
Browse files Browse the repository at this point in the history
  • Loading branch information
ThirVondukr committed Feb 5, 2025
1 parent de411b3 commit f0d622d
Show file tree
Hide file tree
Showing 3 changed files with 1 addition and 57 deletions.
43 changes: 1 addition & 42 deletions aioinject/_features/generics.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
from __future__ import annotations

import functools
import sys
import textwrap
import types
import typing as t
from collections.abc import Callable
from types import GenericAlias
from typing import TYPE_CHECKING, Any, TypeGuard

Expand Down Expand Up @@ -79,43 +76,5 @@ def get_generic_parameter_map(
args_map[arg.__name__] for arg in generic_arguments
)
# We can use `[]` when we drop support for 3.10
result[dependency.name] = _py310_compat_resolve_generics(
inner_type, resolved_args
)
result[dependency.name] = inner_type[resolved_args]
return result


def is_py_gt3_311() -> bool: # pragma: no cover
return sys.version_info >= (3, 11)


def _py310_compat_resolve_generics_factory() -> (
Callable[[type, tuple[type, ...]], type]
): # pragma: no cover
# we need to exec a string to avoid syntax errors
# we will create a function that will return the resolved generic
# for python 3.11 and later we can use `generic_alias[*args]` which will consider
# see `test_partially_resolved_generic` for more details

if is_py_gt3_311():
fn_impl = textwrap.dedent("""
def _resolve_generic(
generic_alias: type,
args: tuple[type, ...],
) -> type:
return generic_alias[*args]
""")
else:
fn_impl = textwrap.dedent("""
def _resolve_generic(
generic_alias: type,
args: tuple[type, ...],
) -> type:
return generic_alias.__getitem__(*args)
""")
exec_globals: dict[str, Any] = {}
exec(fn_impl, exec_globals) # noqa: S102
return exec_globals["_resolve_generic"]


_py310_compat_resolve_generics = _py310_compat_resolve_generics_factory()
5 changes: 0 additions & 5 deletions tests/features/test_generics.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

from aioinject import Container, Object, Scoped
from aioinject.providers import Dependency, Transient
from tests.utils_ import py_gte_311


T = TypeVar("T")
Expand Down Expand Up @@ -95,10 +94,6 @@ async def test_resolve_generics(
assert isinstance(instance, instanceof)


@py_gte_311("""
prior to 3.11 using generic_alias[] considered a syntax error
its very hard to overcome this due to `test_partially_resolved_generic`
""")
async def test_nested_generics() -> None:
container = Container()
container.register(
Expand Down
10 changes: 0 additions & 10 deletions tests/utils_.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import functools
import sys
from collections.abc import Callable
from typing import ParamSpec, TypeVar

import pytest


T = TypeVar("T")
P = ParamSpec("P")
Expand All @@ -16,10 +13,3 @@ def decorator(*args: P.args, **kwargs: P.kwargs) -> T:
return func(*args, **kwargs)

return decorator


def py_gte_311(reason: str) -> pytest.MarkDecorator:
return pytest.mark.skipif(
sys.version_info < (3, 11),
reason=reason,
)

0 comments on commit f0d622d

Please sign in to comment.