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

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

38 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_10_PLUS = sys.version_info[:2] >= (3, 10) 

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

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

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

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

18 

19 

20if PY_3_14_PLUS: # pragma: no cover 

21 import annotationlib 

22 

23 _get_annotations = annotationlib.get_annotations 

24 

25else: 

26 

27 def _get_annotations(cls): 

28 """ 

29 Get annotations for *cls*. 

30 """ 

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

32 

33 

34class _AnnotationExtractor: 

35 """ 

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

37 is none. 

38 """ 

39 

40 __slots__ = ["sig"] 

41 

42 def __init__(self, callable): 

43 try: 

44 self.sig = inspect.signature(callable) 

45 except (ValueError, TypeError): # inspect failed 

46 self.sig = None 

47 

48 def get_first_param_type(self): 

49 """ 

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

51 """ 

52 if not self.sig: 

53 return None 

54 

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

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

57 return params[0].annotation 

58 

59 return None 

60 

61 def get_return_type(self): 

62 """ 

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

64 """ 

65 if ( 

66 self.sig 

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

68 ): 

69 return self.sig.return_annotation 

70 

71 return None 

72 

73 

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

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

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

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

78# 

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

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

81# the dict's repr() call. 

82# 

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

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

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

86repr_context = threading.local() 

87 

88 

89def get_generic_base(cl): 

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

91 if cl.__class__ is _GenericAlias: 

92 return cl.__origin__ 

93 return None