Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/attr/_compat.py: 86%
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1# SPDX-License-Identifier: MIT
3import inspect
4import platform
5import sys
6import threading
8from collections.abc import Mapping, Sequence # noqa: F401
9from typing import _GenericAlias
12PYPY = platform.python_implementation() == "PyPy"
13PY_3_8_PLUS = sys.version_info[:2] >= (3, 8)
14PY_3_9_PLUS = sys.version_info[:2] >= (3, 9)
15PY_3_10_PLUS = sys.version_info[:2] >= (3, 10)
16PY_3_11_PLUS = sys.version_info[:2] >= (3, 11)
17PY_3_12_PLUS = sys.version_info[:2] >= (3, 12)
18PY_3_13_PLUS = sys.version_info[:2] >= (3, 13)
19PY_3_14_PLUS = sys.version_info[:2] >= (3, 14)
22if sys.version_info < (3, 8):
23 try:
24 from typing_extensions import Protocol
25 except ImportError: # pragma: no cover
26 Protocol = object
27else:
28 from typing import Protocol # noqa: F401
30if PY_3_14_PLUS: # pragma: no cover
31 import annotationlib
33 _get_annotations = annotationlib.get_annotations
35else:
37 def _get_annotations(cls):
38 """
39 Get annotations for *cls*.
40 """
41 return cls.__dict__.get("__annotations__", {})
44class _AnnotationExtractor:
45 """
46 Extract type annotations from a callable, returning None whenever there
47 is none.
48 """
50 __slots__ = ["sig"]
52 def __init__(self, callable):
53 try:
54 self.sig = inspect.signature(callable)
55 except (ValueError, TypeError): # inspect failed
56 self.sig = None
58 def get_first_param_type(self):
59 """
60 Return the type annotation of the first argument if it's not empty.
61 """
62 if not self.sig:
63 return None
65 params = list(self.sig.parameters.values())
66 if params and params[0].annotation is not inspect.Parameter.empty:
67 return params[0].annotation
69 return None
71 def get_return_type(self):
72 """
73 Return the return type if it's not empty.
74 """
75 if (
76 self.sig
77 and self.sig.return_annotation is not inspect.Signature.empty
78 ):
79 return self.sig.return_annotation
81 return None
84# Thread-local global to track attrs instances which are already being repr'd.
85# This is needed because there is no other (thread-safe) way to pass info
86# about the instances that are already being repr'd through the call stack
87# in order to ensure we don't perform infinite recursion.
88#
89# For instance, if an instance contains a dict which contains that instance,
90# we need to know that we're already repr'ing the outside instance from within
91# the dict's repr() call.
92#
93# This lives here rather than in _make.py so that the functions in _make.py
94# don't have a direct reference to the thread-local in their globals dict.
95# If they have such a reference, it breaks cloudpickle.
96repr_context = threading.local()
99def get_generic_base(cl):
100 """If this is a generic class (A[str]), return the generic base for it."""
101 if cl.__class__ is _GenericAlias:
102 return cl.__origin__
103 return None