|
14 | 14 | if typing.TYPE_CHECKING: # pragma: no cover |
15 | 15 | from pylint.lint import PyLinter |
16 | 16 |
|
| 17 | + |
| 18 | +import logging |
| 19 | + |
| 20 | +LOGGER = logging.getLogger(__name__) |
17 | 21 | # pylint: disable=broad-exception-caught |
18 | 22 |
|
19 | 23 |
|
@@ -88,33 +92,46 @@ def visit_assign(self, node: nodes.Assign) -> None: |
88 | 92 | if not inferred: |
89 | 93 | return |
90 | 94 |
|
| 95 | + def _safe_pytype(n: nodes.NodeNG) -> str | None: |
| 96 | + pytype = getattr(n, "pytype", None) |
| 97 | + if not callable(pytype): |
| 98 | + return None |
| 99 | + |
| 100 | + try: |
| 101 | + return pytype() |
| 102 | + except (AttributeError, InferenceError, TypeError): |
| 103 | + LOGGER.debug( |
| 104 | + "Could not infer pytype for astroid node %s", |
| 105 | + n.__class__.__name__, |
| 106 | + exc_info=True, |
| 107 | + ) |
| 108 | + return None |
| 109 | + |
| 110 | + def _safe_qname(n: nodes.NodeNG) -> str | None: |
| 111 | + qname = getattr(n, "qname", None) |
| 112 | + if not callable(qname): |
| 113 | + return None |
| 114 | + |
| 115 | + try: |
| 116 | + return qname() |
| 117 | + except (AttributeError, InferenceError, TypeError): |
| 118 | + LOGGER.debug( |
| 119 | + "Could not infer qname for astroid node %s", |
| 120 | + n.__class__.__name__, |
| 121 | + exc_info=True, |
| 122 | + ) |
| 123 | + return None |
| 124 | + |
91 | 125 | def _is_dict(n: nodes.NodeNG) -> bool: |
92 | 126 | if isinstance(n, nodes.Dict): |
93 | 127 | return True |
94 | | - try: |
95 | | - if getattr(n, "pytype", None) and n.pytype() == "builtins.dict": |
96 | | - return True |
97 | | - except Exception: # pragma: no cover |
98 | | - pass |
99 | | - try: |
100 | | - if getattr(n, "qname", None) and n.qname() == "builtins.dict": |
101 | | - return True |
102 | | - except Exception: # pragma: no cover |
103 | | - pass |
104 | | - return False |
| 128 | + |
| 129 | + return ( |
| 130 | + _safe_pytype(n) == "builtins.dict" or _safe_qname(n) == "builtins.dict" |
| 131 | + ) |
105 | 132 |
|
106 | 133 | def _type_str(n: nodes.NodeNG) -> str: |
107 | | - try: |
108 | | - if getattr(n, "pytype", None): |
109 | | - return n.pytype() |
110 | | - except Exception: # pragma: no cover |
111 | | - pass |
112 | | - try: |
113 | | - if getattr(n, "qname", None): |
114 | | - return n.qname() |
115 | | - except Exception: # pragma: no cover |
116 | | - pass |
117 | | - return n.__class__.__name__ |
| 134 | + return _safe_pytype(n) or _safe_qname(n) or n.__class__.__name__ |
118 | 135 |
|
119 | 136 | any_dict = any(_is_dict(n) for n in inferred) |
120 | 137 |
|
|
0 commit comments