Skip to content

Commit

Permalink
fixed support for Python < 3.10
Browse files Browse the repository at this point in the history
  • Loading branch information
npgrosser committed Nov 25, 2023
1 parent 7628c79 commit 40e71a0
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 10 deletions.
8 changes: 3 additions & 5 deletions autowired/_container.py
Original file line number Diff line number Diff line change
Expand Up @@ -282,11 +282,9 @@ def autowire(
:return: The auto-wired object
:raises AutowiredException: if the object cannot be auto-wired
"""
logger.trace(
f"Auto-wiring {t.__name__} with {len(explicit_kw_args)} explicit args"
)
logger.trace(f"Auto-wiring {t} with {len(explicit_kw_args)} explicit args")
if t.__module__.split(".")[0] in _illegal_autowiredType_modules:
raise IllegalAutoWireType(f"Cannot auto-wire object of type {t.__name__}")
raise IllegalAutoWireType(f"Cannot auto-wire object of type {t}")

dependencies = _get_dependencies_for_type(t)

Expand All @@ -309,7 +307,7 @@ def autowire(
if dep.required:
raise UnresolvableDependencyException(
f"Failed to resolve dependency {dep.name} "
f"of type {dep.type.__name__} for {t.__name__}. "
f"of type {dep.type} for {t}. "
) from e

try:
Expand Down
16 changes: 13 additions & 3 deletions autowired/_typing_utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
from types import UnionType
from typing import Type, get_args, get_origin, Union, Any, Optional, List, Tuple

try:
from types import UnionType
except ImportError: # pragma: no cover
UnionType = None


def is_subtype(t1: Type, t2: Type) -> bool:
"""
Expand Down Expand Up @@ -70,12 +74,18 @@ def is_subtype(t1: Type, t2: Type) -> bool:
return True


def _as_union_types(t: Type | UnionType) -> tuple[Type, ...]:
def _as_union_types(t) -> Tuple[Type, ...]:
"""
Returns the types of a Union type, or a tuple containing only t if t is not a Union type.
"""
if isinstance(t, UnionType) or get_origin(t) is Union:

if get_origin(t) is Union:
return get_args(t)

if UnionType is not None:
if get_origin(t) is UnionType:
return get_args(t)

return (t,)


Expand Down
4 changes: 2 additions & 2 deletions tests/test_typing_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,14 +104,14 @@ def test_mixed_generic_non_generic():
def test_union_types(new_union_syntax: bool):
def union(*args):
if new_union_syntax:
return Union[args]
else:
if len(args) == 1:
return args[0]
if len(args) == 2:
return args[0] | args[1]
if len(args) == 3:
return args[0] | args[1] | args[2]
else:
return Union[args]

# case 1: both types are unions
# 1.1: same types
Expand Down

0 comments on commit 40e71a0

Please sign in to comment.