Skip to content

Commit 347cd58

Browse files
Michael0x2agvanrossum
authored andcommitted
Raise custom error when builtin symbol is missing (#2062)
A common point of confusion people have when working on the mypy codebase is forgetting to include the appropriate builtin fixture to their unit tests. In particular, not including a fixture (or the right fixture) can result in a cryptic exception when the code attempts to look up the builtin symbol and fails. This commit adds a check to the most common failure point and raises a custom exception when it detects a builtin symbol is missing (which should never happen when running mypy normally).
1 parent d76554b commit 347cd58

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

mypy/checker.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2236,7 +2236,16 @@ def lookup_qualified(self, name: str) -> SymbolTableNode:
22362236
n = self.modules[parts[0]]
22372237
for i in range(1, len(parts) - 1):
22382238
n = cast(MypyFile, n.names.get(parts[i], None).node)
2239-
return n.names[parts[-1]]
2239+
last = parts[-1]
2240+
if last in n.names:
2241+
return n.names[last]
2242+
elif len(parts) == 2 and parts[0] == 'builtins':
2243+
raise KeyError("Could not find builtin symbol '{}'. (Are you running a "
2244+
"test case? If so, make sure to include a fixture that "
2245+
"defines this symbol.)".format(last))
2246+
else:
2247+
msg = "Failed qualified lookup: '{}' (fullname = '{}')."
2248+
raise KeyError(msg.format(last, name))
22402249

22412250
def enter_partial_types(self) -> None:
22422251
"""Push a new scope for collecting partial types."""

0 commit comments

Comments
 (0)