Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/attr/_compat.py: 77%

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

39 statements  

1# SPDX-License-Identifier: MIT 

2 

3import inspect 

4import platform 

5import sys 

6import threading 

7 

8from collections.abc import Mapping, Sequence # noqa: F401 

9from typing import _GenericAlias 

10 

11 

12PYPY = platform.python_implementation() == "PyPy" 

13PY_3_9_PLUS = sys.version_info[:2] >= (3, 9) 

14PY_3_10_PLUS = sys.version_info[:2] >= (3, 10) 

15PY_3_11_PLUS = sys.version_info[:2] >= (3, 11) 

16PY_3_12_PLUS = sys.version_info[:2] >= (3, 12) 

17PY_3_13_PLUS = sys.version_info[:2] >= (3, 13) 

18PY_3_14_PLUS = sys.version_info[:2] >= (3, 14) 

19 

20 

21if PY_3_14_PLUS: # pragma: no cover 

22 import annotationlib 

23 

24 _get_annotations = annotationlib.get_annotations 

25 

26else: 

27 

28 def _get_annotations(cls): 

29 """ 

30 Get annotations for *cls*. 

31 """ 

32 return cls.__dict__.get("__annotations__", {}) 

33 

34 

35class _AnnotationExtractor: 

36 """ 

37 Extract type annotations from a callable, returning None whenever there 

38 is none. 

39 """ 

40 

41 __slots__ = ["sig"] 

42 

43 def __init__(self, callable): 

44 try: 

45 self.sig = inspect.signature(callable) 

46 except (ValueError, TypeError): # inspect failed 

47 self.sig = None 

48 

49 def get_first_param_type(self): 

50 """ 

51 Return the type annotation of the first argument if it's not empty. 

52 """ 

53 if not self.sig: 

54 return None 

55 

56 params = list(self.sig.parameters.values()) 

57 if params and params[0].annotation is not inspect.Parameter.empty: 

58 return params[0].annotation 

59 

60 return None 

61 

62 def get_return_type(self): 

63 """ 

64 Return the return type if it's not empty. 

65 """ 

66 if ( 

67 self.sig 

68 and self.sig.return_annotation is not inspect.Signature.empty 

69 ): 

70 return self.sig.return_annotation 

71 

72 return None 

73 

74 

75# Thread-local global to track attrs instances which are already being repr'd. 

76# This is needed because there is no other (thread-safe) way to pass info 

77# about the instances that are already being repr'd through the call stack 

78# in order to ensure we don't perform infinite recursion. 

79# 

80# For instance, if an instance contains a dict which contains that instance, 

81# we need to know that we're already repr'ing the outside instance from within 

82# the dict's repr() call. 

83# 

84# This lives here rather than in _make.py so that the functions in _make.py 

85# don't have a direct reference to the thread-local in their globals dict. 

86# If they have such a reference, it breaks cloudpickle. 

87repr_context = threading.local() 

88 

89 

90def get_generic_base(cl): 

91 """If this is a generic class (A[str]), return the generic base for it.""" 

92 if cl.__class__ is _GenericAlias: 

93 return cl.__origin__ 

94 return None