|
1 | 1 | import sys |
2 | 2 | from collections import deque |
| 3 | +from collections.abc import Mapping as AbcMapping |
| 4 | +from collections.abc import MutableMapping as AbcMutableMapping |
3 | 5 | from collections.abc import MutableSet as AbcMutableSet |
4 | 6 | from collections.abc import Set as AbcSet |
5 | 7 | from dataclasses import MISSING, Field, is_dataclass |
@@ -219,8 +221,6 @@ def get_final_base(type) -> Optional[type]: |
219 | 221 |
|
220 | 222 | if sys.version_info >= (3, 9): |
221 | 223 | from collections import Counter |
222 | | - from collections.abc import Mapping as AbcMapping |
223 | | - from collections.abc import MutableMapping as AbcMutableMapping |
224 | 224 | from collections.abc import MutableSequence as AbcMutableSequence |
225 | 225 | from collections.abc import MutableSet as AbcMutableSet |
226 | 226 | from collections.abc import Sequence as AbcSequence |
@@ -404,18 +404,17 @@ def is_bare(type): |
404 | 404 | not hasattr(type, "__origin__") and not hasattr(type, "__args__") |
405 | 405 | ) |
406 | 406 |
|
407 | | - def is_mapping(type): |
| 407 | + def is_mapping(type: Any) -> bool: |
| 408 | + """A predicate function for mappings.""" |
408 | 409 | return ( |
409 | 410 | type in (dict, Dict, TypingMapping, TypingMutableMapping, AbcMutableMapping) |
410 | 411 | or ( |
411 | 412 | type.__class__ is _GenericAlias |
412 | 413 | and is_subclass(type.__origin__, TypingMapping) |
413 | 414 | ) |
414 | | - or ( |
415 | | - getattr(type, "__origin__", None) |
416 | | - in (dict, AbcMutableMapping, AbcMapping) |
| 415 | + or is_subclass( |
| 416 | + getattr(type, "__origin__", type), (dict, AbcMutableMapping, AbcMapping) |
417 | 417 | ) |
418 | | - or is_subclass(type, dict) |
419 | 418 | ) |
420 | 419 |
|
421 | 420 | def is_counter(type): |
@@ -515,10 +514,17 @@ def is_frozenset(type): |
515 | 514 | type.__class__ is _GenericAlias and is_subclass(type.__origin__, FrozenSet) |
516 | 515 | ) |
517 | 516 |
|
518 | | - def is_mapping(type): |
519 | | - return type in (TypingMapping, dict) or ( |
520 | | - type.__class__ is _GenericAlias |
521 | | - and is_subclass(type.__origin__, TypingMapping) |
| 517 | + def is_mapping(type: Any) -> bool: |
| 518 | + """A predicate function for mappings.""" |
| 519 | + return ( |
| 520 | + type in (TypingMapping, dict) |
| 521 | + or ( |
| 522 | + type.__class__ is _GenericAlias |
| 523 | + and is_subclass(type.__origin__, TypingMapping) |
| 524 | + ) |
| 525 | + or is_subclass( |
| 526 | + getattr(type, "__origin__", type), (dict, AbcMutableMapping, AbcMapping) |
| 527 | + ) |
522 | 528 | ) |
523 | 529 |
|
524 | 530 | bare_generic_args = { |
|
0 commit comments