Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/astroid/util.py: 62%

63 statements  

« prev     ^ index     » next       coverage.py v7.2.7, created at 2023-06-07 06:53 +0000

1# Licensed under the LGPL: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.en.html 

2# For details: https://github.com/pylint-dev/astroid/blob/main/LICENSE 

3# Copyright (c) https://github.com/pylint-dev/astroid/blob/main/CONTRIBUTORS.txt 

4 

5 

6from __future__ import annotations 

7 

8import warnings 

9from typing import Any, Final, Literal 

10 

11 

12class UninferableBase: 

13 """Special inference object, which is returned when inference fails. 

14 

15 This is meant to be used as a singleton. Use astroid.util.Uninferable to access it. 

16 """ 

17 

18 def __repr__(self) -> Literal["Uninferable"]: 

19 return "Uninferable" 

20 

21 __str__ = __repr__ 

22 

23 def __getattribute__(self, name: str) -> Any: 

24 if name == "next": 

25 raise AttributeError("next method should not be called") 

26 if name.startswith("__") and name.endswith("__"): 

27 return object.__getattribute__(self, name) 

28 if name == "accept": 

29 return object.__getattribute__(self, name) 

30 return self 

31 

32 def __call__(self, *args: Any, **kwargs: Any) -> UninferableBase: 

33 return self 

34 

35 def __bool__(self) -> Literal[False]: 

36 return False 

37 

38 __nonzero__ = __bool__ 

39 

40 def accept(self, visitor): 

41 return visitor.visit_uninferable(self) 

42 

43 

44Uninferable: Final = UninferableBase() 

45 

46 

47class BadOperationMessage: 

48 """Object which describes a TypeError occurred somewhere in the inference chain. 

49 

50 This is not an exception, but a container object which holds the types and 

51 the error which occurred. 

52 """ 

53 

54 

55class BadUnaryOperationMessage(BadOperationMessage): 

56 """Object which describes operational failures on UnaryOps.""" 

57 

58 def __init__(self, operand, op, error): 

59 self.operand = operand 

60 self.op = op 

61 self.error = error 

62 

63 @property 

64 def _object_type_helper(self): 

65 from astroid import helpers # pylint: disable=import-outside-toplevel 

66 

67 return helpers.object_type 

68 

69 def _object_type(self, obj): 

70 objtype = self._object_type_helper(obj) 

71 if isinstance(objtype, UninferableBase): 

72 return None 

73 

74 return objtype 

75 

76 def __str__(self) -> str: 

77 if hasattr(self.operand, "name"): 

78 operand_type = self.operand.name 

79 else: 

80 object_type = self._object_type(self.operand) 

81 if hasattr(object_type, "name"): 

82 operand_type = object_type.name 

83 else: 

84 # Just fallback to as_string 

85 operand_type = object_type.as_string() 

86 

87 msg = "bad operand type for unary {}: {}" 

88 return msg.format(self.op, operand_type) 

89 

90 

91class BadBinaryOperationMessage(BadOperationMessage): 

92 """Object which describes type errors for BinOps.""" 

93 

94 def __init__(self, left_type, op, right_type): 

95 self.left_type = left_type 

96 self.right_type = right_type 

97 self.op = op 

98 

99 def __str__(self) -> str: 

100 msg = "unsupported operand type(s) for {}: {!r} and {!r}" 

101 return msg.format(self.op, self.left_type.name, self.right_type.name) 

102 

103 

104def _instancecheck(cls, other) -> bool: 

105 wrapped = cls.__wrapped__ 

106 other_cls = other.__class__ 

107 is_instance_of = wrapped is other_cls or issubclass(other_cls, wrapped) 

108 warnings.warn( 

109 "%r is deprecated and slated for removal in astroid " 

110 "2.0, use %r instead" % (cls.__class__.__name__, wrapped.__name__), 

111 PendingDeprecationWarning, 

112 stacklevel=2, 

113 ) 

114 return is_instance_of 

115 

116 

117def check_warnings_filter() -> bool: 

118 """Return True if any other than the default DeprecationWarning filter is enabled. 

119 

120 https://docs.python.org/3/library/warnings.html#default-warning-filter 

121 """ 

122 return any( 

123 issubclass(DeprecationWarning, filter[2]) 

124 and filter[0] != "ignore" 

125 and filter[3] != "__main__" 

126 for filter in warnings.filters 

127 )