Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use safer is_subclass checks #475

Merged
merged 1 commit into from
Dec 27, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 14 additions & 14 deletions src/cattrs/_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ def is_hetero_tuple(type: Any) -> bool:


def is_protocol(type: Any) -> bool:
return issubclass(type, Protocol) and getattr(type, "_is_protocol", False)
return is_subclass(type, Protocol) and getattr(type, "_is_protocol", False)


def is_bare_final(type) -> bool:
Expand Down Expand Up @@ -245,7 +245,7 @@ def is_annotated(type) -> bool:
def is_tuple(type):
return (
type in (Tuple, tuple)
or (type.__class__ is _GenericAlias and issubclass(type.__origin__, Tuple))
or (type.__class__ is _GenericAlias and is_subclass(type.__origin__, Tuple))
or (getattr(type, "__origin__", None) is tuple)
)

Expand Down Expand Up @@ -324,7 +324,7 @@ def is_sequence(type: Any) -> bool:
type.__class__ is _GenericAlias
and (
(origin is not tuple)
and issubclass(origin, TypingSequence)
and is_subclass(origin, TypingSequence)
or origin is tuple
and type.__args__[1] is ...
)
Expand All @@ -336,7 +336,7 @@ def is_sequence(type: Any) -> bool:
def is_deque(type):
return (
type in (deque, Deque)
or (type.__class__ is _GenericAlias and issubclass(type.__origin__, deque))
or (type.__class__ is _GenericAlias and is_subclass(type.__origin__, deque))
or (getattr(type, "__origin__", None) is deque)
)

Expand All @@ -345,7 +345,7 @@ def is_mutable_set(type):
type in (TypingSet, TypingMutableSet, set)
or (
type.__class__ is _GenericAlias
and issubclass(type.__origin__, TypingMutableSet)
and is_subclass(type.__origin__, TypingMutableSet)
)
or (getattr(type, "__origin__", None) in (set, AbcMutableSet, AbcSet))
)
Expand All @@ -355,7 +355,7 @@ def is_frozenset(type):
type in (FrozenSet, frozenset)
or (
type.__class__ is _GenericAlias
and issubclass(type.__origin__, FrozenSet)
and is_subclass(type.__origin__, FrozenSet)
)
or (getattr(type, "__origin__", None) is frozenset)
)
Expand All @@ -370,13 +370,13 @@ def is_mapping(type):
type in (dict, Dict, TypingMapping, TypingMutableMapping, AbcMutableMapping)
or (
type.__class__ is _GenericAlias
and issubclass(type.__origin__, TypingMapping)
and is_subclass(type.__origin__, TypingMapping)
)
or (
getattr(type, "__origin__", None)
in (dict, AbcMutableMapping, AbcMapping)
)
or issubclass(type, dict)
or is_subclass(type, dict)
)

def is_counter(type):
Expand Down Expand Up @@ -427,7 +427,7 @@ def is_annotated(type) -> bool:

def is_tuple(type):
return type in (Tuple, tuple) or (
type.__class__ is _GenericAlias and issubclass(type.__origin__, Tuple)
type.__class__ is _GenericAlias and is_subclass(type.__origin__, Tuple)
)

def is_union_type(obj):
Expand All @@ -450,32 +450,32 @@ def is_sequence(type: Any) -> bool:
type.__class__ is _GenericAlias
and (
type.__origin__ not in (Union, Tuple, tuple)
and issubclass(type.__origin__, TypingSequence)
and is_subclass(type.__origin__, TypingSequence)
)
or (type.__origin__ in (Tuple, tuple) and type.__args__[1] is ...)
)

def is_deque(type: Any) -> bool:
return (
type in (deque, Deque)
or (type.__class__ is _GenericAlias and issubclass(type.__origin__, deque))
or (type.__class__ is _GenericAlias and is_subclass(type.__origin__, deque))
or type.__origin__ is deque
)

def is_mutable_set(type):
return type is set or (
type.__class__ is _GenericAlias and issubclass(type.__origin__, MutableSet)
type.__class__ is _GenericAlias and is_subclass(type.__origin__, MutableSet)
)

def is_frozenset(type):
return type is frozenset or (
type.__class__ is _GenericAlias and issubclass(type.__origin__, FrozenSet)
type.__class__ is _GenericAlias and is_subclass(type.__origin__, FrozenSet)
)

def is_mapping(type):
return type in (TypingMapping, dict) or (
type.__class__ is _GenericAlias
and issubclass(type.__origin__, TypingMapping)
and is_subclass(type.__origin__, TypingMapping)
)

bare_generic_args = {
Expand Down