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
« 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
6from __future__ import annotations
8import warnings
9from typing import Any, Final, Literal
12class UninferableBase:
13 """Special inference object, which is returned when inference fails.
15 This is meant to be used as a singleton. Use astroid.util.Uninferable to access it.
16 """
18 def __repr__(self) -> Literal["Uninferable"]:
19 return "Uninferable"
21 __str__ = __repr__
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
32 def __call__(self, *args: Any, **kwargs: Any) -> UninferableBase:
33 return self
35 def __bool__(self) -> Literal[False]:
36 return False
38 __nonzero__ = __bool__
40 def accept(self, visitor):
41 return visitor.visit_uninferable(self)
44Uninferable: Final = UninferableBase()
47class BadOperationMessage:
48 """Object which describes a TypeError occurred somewhere in the inference chain.
50 This is not an exception, but a container object which holds the types and
51 the error which occurred.
52 """
55class BadUnaryOperationMessage(BadOperationMessage):
56 """Object which describes operational failures on UnaryOps."""
58 def __init__(self, operand, op, error):
59 self.operand = operand
60 self.op = op
61 self.error = error
63 @property
64 def _object_type_helper(self):
65 from astroid import helpers # pylint: disable=import-outside-toplevel
67 return helpers.object_type
69 def _object_type(self, obj):
70 objtype = self._object_type_helper(obj)
71 if isinstance(objtype, UninferableBase):
72 return None
74 return objtype
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()
87 msg = "bad operand type for unary {}: {}"
88 return msg.format(self.op, operand_type)
91class BadBinaryOperationMessage(BadOperationMessage):
92 """Object which describes type errors for BinOps."""
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
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)
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
117def check_warnings_filter() -> bool:
118 """Return True if any other than the default DeprecationWarning filter is enabled.
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 )