From d0e3fca1b62b4f9b66a63f8e8e78840f8ace0a23 Mon Sep 17 00:00:00 2001 From: slawwan Date: Sat, 9 Nov 2024 22:58:56 +0500 Subject: [PATCH] fixed imports --- marshmallow_recipe/generics.py | 13 ++++++------- tests/test_generics.py | 4 ++-- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/marshmallow_recipe/generics.py b/marshmallow_recipe/generics.py index 7080688..8392cfd 100644 --- a/marshmallow_recipe/generics.py +++ b/marshmallow_recipe/generics.py @@ -1,12 +1,11 @@ import dataclasses +import types import typing -from dataclasses import Field -from types import GenericAlias, UnionType from typing import Annotated, Any, Generic, TypeAlias, TypeVar, Union, get_args, get_origin _GenericAlias: TypeAlias = typing._GenericAlias # type: ignore -TypeLike: TypeAlias = type | TypeVar | UnionType | GenericAlias | _GenericAlias +TypeLike: TypeAlias = type | TypeVar | types.UnionType | types.GenericAlias | _GenericAlias TypeVarMap: TypeAlias = dict[TypeVar, TypeLike] ClassTypeVarMap: TypeAlias = dict[TypeLike, TypeVarMap] FieldsTypeVarMap: TypeAlias = dict[str, TypeVarMap] @@ -32,7 +31,7 @@ def get_fields_class_map(t: TypeLike) -> FieldsClassMap: if not dataclasses.is_dataclass(origin): return {} - names: dict[str, Field] = {} + names: dict[str, dataclasses.Field] = {} result: FieldsClassMap = {} mro = origin.__mro__ # type: ignore @@ -51,15 +50,15 @@ def build_subscripted_type(t: TypeLike, type_var_map: TypeVarMap) -> TypeLike: return build_subscripted_type(type_var_map[t], type_var_map) origin = get_origin(t) - if origin is Union or isinstance(t, UnionType): + if origin is Union or isinstance(t, types.UnionType): return Union[*(build_subscripted_type(x, type_var_map) for x in get_args(t))] # type: ignore if origin is Annotated: t, *annotations = get_args(t) return Annotated[build_subscripted_type(t, type_var_map), *annotations] # type: ignore - if origin and isinstance(t, GenericAlias): - return GenericAlias(origin, tuple(build_subscripted_type(x, type_var_map) for x in get_args(t))) + if origin and isinstance(t, types.GenericAlias): + return types.GenericAlias(origin, tuple(build_subscripted_type(x, type_var_map) for x in get_args(t))) if origin and isinstance(t, _GenericAlias): return _GenericAlias(origin, tuple(build_subscripted_type(x, type_var_map) for x in get_args(t))) diff --git a/tests/test_generics.py b/tests/test_generics.py index 68139dd..fef0298 100644 --- a/tests/test_generics.py +++ b/tests/test_generics.py @@ -1,5 +1,5 @@ import dataclasses -from types import UnionType +import types from typing import Annotated, Any, Generic, Iterable, List, TypeVar, Union import pytest @@ -236,7 +236,7 @@ class Zzz(Generic[_T1]): _TList = TypeVar("_TList") _TDictIntTStr = TypeVar("_TDictIntTStr") -GENERIC_MAP: dict[TypeVar, type[Any] | UnionType] = { +GENERIC_MAP: dict[TypeVar, type[Any] | types.UnionType] = { _TInt: int, _TIntNone: int | None, _TStr: str,