Skip to content

Commit dfff5c0

Browse files
committed
rewrite without silent exceptions
1 parent e141c8c commit dfff5c0

1 file changed

Lines changed: 39 additions & 22 deletions

File tree

colrev/linter/colrev_records_variable_naming_convention.py

Lines changed: 39 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@
1414
if typing.TYPE_CHECKING: # pragma: no cover
1515
from pylint.lint import PyLinter
1616

17+
18+
import logging
19+
20+
LOGGER = logging.getLogger(__name__)
1721
# pylint: disable=broad-exception-caught
1822

1923

@@ -88,33 +92,46 @@ def visit_assign(self, node: nodes.Assign) -> None:
8892
if not inferred:
8993
return
9094

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+
91125
def _is_dict(n: nodes.NodeNG) -> bool:
92126
if isinstance(n, nodes.Dict):
93127
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+
)
105132

106133
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__
118135

119136
any_dict = any(_is_dict(n) for n in inferred)
120137

0 commit comments

Comments
 (0)