Skip to content

Commit

Permalink
refactor type hints to be backwards compatible with python 3.8
Browse files Browse the repository at this point in the history
  • Loading branch information
arne.vanlondersele committed Jan 14, 2025
1 parent 91a5f85 commit b982236
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
14 changes: 7 additions & 7 deletions dacite/generics.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import sys
from dataclasses import Field, is_dataclass
from typing import Any, Generic, List, Literal, TypeVar, get_type_hints
from typing import Any, Dict, Generic, List, Literal, Tuple, Type, TypeVar, get_type_hints

try:
from typing import get_args, get_origin
Expand All @@ -10,7 +10,7 @@
from .dataclasses import get_fields as dataclasses_get_fields


def _add_generics(type_origin: Any, type_args: tuple, generics: dict) -> None:
def _add_generics(type_origin: Any, type_args: Tuple, generics: Dict[TypeVar, Type]) -> None:
"""Adds (type var, concrete type) entries derived from a type's origin and args to the provided generics dict."""
if type_origin and type_args and hasattr(type_origin, "__parameters__"):
for param, arg in zip(type_origin.__parameters__, type_args):
Expand All @@ -20,7 +20,7 @@ def _add_generics(type_origin: Any, type_args: tuple, generics: dict) -> None:
generics[param] = arg


def _dereference(type_name: str, data_class: type) -> type:
def _dereference(type_name: str, data_class: Type) -> Type:
"""
Try to find the class belonging to the reference in the provided module and,
if not found, iteratively look in parent modules.
Expand All @@ -39,7 +39,7 @@ def _dereference(type_name: str, data_class: type) -> type:
raise AttributeError("Could not find reference.")


def _concretize(hint: type, generics: dict[type, type], data_class: type) -> type:
def _concretize(hint: Type, generics: Dict[TypeVar, Type], data_class: Type) -> Type:
"""Recursively replace type vars and forward references by concrete types."""

if hint.__class__ is str:
Expand All @@ -57,13 +57,13 @@ def _concretize(hint: type, generics: dict[type, type], data_class: type) -> typ
return hint


def orig(data_class: type) -> Any:
def orig(data_class: Type) -> Any:
if is_dataclass(data_class):
return data_class
return get_origin(data_class)


def get_concrete_type_hints(data_class: type, *args, **kwargs) -> dict[str, Any]:
def get_concrete_type_hints(data_class: Type, *args, **kwargs) -> Dict[str, Any]:
"""
An overwrite of typing.get_type_hints supporting generics and forward references,
i.e. substituting concrete types in type vars and references.
Expand All @@ -90,6 +90,6 @@ def get_concrete_type_hints(data_class: type, *args, **kwargs) -> dict[str, Any]
return hints


def get_fields(data_class: type) -> List[Field]:
def get_fields(data_class: Type) -> List[Field]:
"""An overwrite of dacite's get_fields function, supporting generics."""
return dataclasses_get_fields(orig(data_class))
2 changes: 1 addition & 1 deletion tests/core/test_forward_reference.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class Person:
@dataclass
class Club:
name: str
members: list["Person"]
members: List["Person"]


def test_self_reference():
Expand Down

0 comments on commit b982236

Please sign in to comment.