-
Notifications
You must be signed in to change notification settings - Fork 8
/
checks.py
96 lines (72 loc) 路 2.93 KB
/
checks.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
"Slots-related checks and inspection tools"
import platform
import sys
from typing import Callable, Collection, Iterator, Optional
from .common import either
is_typeddict: Callable[[type], bool]
try:
from typing import is_typeddict
try:
from typing_extensions import is_typeddict as _is_typing_ext_typeddict
except ModuleNotFoundError:
pass
else:
is_typeddict = either(is_typeddict, _is_typing_ext_typeddict)
except ModuleNotFoundError: # pragma: no cover
from typing_extensions import is_typeddict as is_any_typeddict
def slots(c: type) -> Optional[Collection[str]]:
try:
slots_raw = c.__dict__["__slots__"]
except KeyError:
return None
if isinstance(slots_raw, str):
return (slots_raw,)
elif isinstance(slots_raw, Iterator):
raise NotImplementedError("Iterator __slots__ not supported. See #22")
else:
return slots_raw
def has_slots(c: type) -> bool:
return (
"__slots__" in c.__dict__
or not (issubclass(c, BaseException) or is_pure_python(c))
or is_any_typeddict(c)
)
def has_slotless_base(c: type) -> bool:
return not all(map(has_slots, c.__bases__))
def slots_overlap(c: type) -> bool:
maybe_slots = slots(c)
if maybe_slots is None:
return False
slots_ = set(maybe_slots)
for ancestor in c.__mro__[1:]:
if not slots_.isdisjoint(slots(ancestor) or ()):
return True
return False
def has_duplicate_slots(c: type) -> bool:
slots_ = slots(c) or ()
return len(set(slots_)) != len(list(slots_))
# The 'is a pure python class' logic below is adapted
# from https://stackoverflow.com/a/41012823/
# If the active Python interpreter is the official CPython interpreter,
# prefer a more reliable CPython-specific solution guaranteed to succeed.
if platform.python_implementation() == "CPython":
# Magic numbers defined by the Python codebase at "Include/object.h".
Py_TPFLAGS_IMMUTABLETYPE = 1 << 8
Py_TPFLAGS_HEAPTYPE = 1 << 9
# Starting with CPython 3.10, `Py_TPFLAGS_HEAPTYPE` should no longer
# be relied on, and `!Py_TPFLAGS_IMMUTABLETYPE` should be used instead.
if sys.version_info >= (3, 10):
def is_pure_python(cls: type) -> bool:
"Whether the class is pure-Python or C-based"
return not (cls.__flags__ & Py_TPFLAGS_IMMUTABLETYPE)
else: # pragma: no cover
def is_pure_python(cls: type) -> bool:
"Whether the class is pure-Python or C-based"
return bool(cls.__flags__ & Py_TPFLAGS_HEAPTYPE)
# Else, fallback to a CPython-agnostic solution typically but *NOT*
# necessarily succeeding. For all real-world objects of interest, this is
# effectively successful. Edge cases exist but are suitably rare.
else: # pragma: no cover
def is_pure_python(cls: type) -> bool:
"Whether the class is pure-Python or C-based"
return "__dict__" in dir(cls) or hasattr(cls, "__slots__")