Skip to content

Commit

Permalink
Remove unnecessary code
Browse files Browse the repository at this point in the history
  • Loading branch information
rnag committed Nov 10, 2024
1 parent adce2d1 commit bfdb6c6
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 55 deletions.
6 changes: 2 additions & 4 deletions dataclass_wizard/parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
T, M, S, DD, LSQ, N, NT, DT
)
from .utils.typing_compat import (
get_origin, get_args, get_named_tuple_field_types,
get_origin, get_args,
get_keys_for_typed_dict, eval_forward_ref_if_needed)


Expand Down Expand Up @@ -482,9 +482,7 @@ def __post_init__(self, cls: Type,
get_parser: GetParserType):

# Get the field annotations for the `NamedTuple` type
type_anns: Dict[str, Type[Any]] = get_named_tuple_field_types(
self.base_type
)
type_anns: Dict[str, Type[Any]] = self.base_type.__annotations__

self.field_to_parser: Optional[FieldToParser] = {
f: get_parser(ftype, cls, extras)
Expand Down
53 changes: 2 additions & 51 deletions dataclass_wizard/utils/typing_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@
'get_origin',
'get_args',
'get_keys_for_typed_dict',
'get_named_tuple_field_types',
'is_typed_dict',
'is_generic',
'is_base_generic',
'is_annotated',
'eval_forward_ref',
'eval_forward_ref_if_needed'
Expand All @@ -20,6 +18,8 @@
import sys
import types
import typing
# noinspection PyUnresolvedReferences,PyProtectedMember
from typing import _AnnotatedAlias

from .string_conv import repl_or_with_union
from ..constants import PY310_OR_ABOVE, PY313_OR_ABOVE
Expand All @@ -46,32 +46,10 @@ def get_keys_for_typed_dict(cls):
return cls.__required_keys__, cls.__optional_keys__


try: # pragma: no cover
from typing_extensions import _AnnotatedAlias
except ImportError:
from typing import _AnnotatedAlias


def _is_annotated(cls):
return isinstance(cls, _AnnotatedAlias)


def _is_base_generic(cls):
if isinstance(cls, typing._GenericAlias):
if cls.__origin__ in {typing.Generic, typing._Protocol}:
return False

if isinstance(cls, typing._VariadicGenericAlias):
return True

return len(cls.__parameters__) > 0

if isinstance(cls, typing._SpecialForm):
return cls._name in {'ClassVar', 'Union', 'Optional'}

return False


def is_literal(cls) -> bool:
try:
return cls.__origin__ is PyLiteral
Expand Down Expand Up @@ -133,19 +111,6 @@ def _get_origin(cls, raise_=False):
return cls


def _get_named_tuple_field_types(cls, raise_=True):
"""
Note: The latest Python versions only support the `__annotations__`
attribute.
"""
try:
return cls.__annotations__
except AttributeError:
if raise_:
raise
return None


def is_typed_dict(cls: typing.Type) -> bool:
"""
Checks if `cls` is a sub-class of ``TypedDict``
Expand All @@ -164,13 +129,6 @@ def is_generic(cls):
return isinstance(cls, _BASE_GENERIC_TYPES)


def is_base_generic(cls):
"""
Detects generic base classes, for example `List` (but not `List[int]`)
"""
return _is_base_generic(cls)


def get_args(cls):
"""
Get type arguments with all substitutions performed.
Expand Down Expand Up @@ -212,13 +170,6 @@ def get_origin(cls, raise_=False):
return _get_origin(cls, raise_=raise_)


def get_named_tuple_field_types(cls, raise_=True):
"""
Get annotations for a :class:`typing.NamedTuple` sub-class.
"""
return _get_named_tuple_field_types(cls, raise_)


def is_annotated(cls):
"""
Detects a :class:`typing.Annotated` class.
Expand Down

0 comments on commit bfdb6c6

Please sign in to comment.