8000 Remove redundant lookup funcs in fixup.py (#11253) · python/mypy@15f2385 · GitHub
[go: up one dir, main page]

Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 15f2385

Browse files
Remove redundant lookup funcs in fixup.py (#11253)
Removes the redundant lookup funcs in fixup.py. Related to #4157. * Removes lookup_qualified_stnode: uncessary nested call to lookup_fully_qualified. * Removes lookup_qualified: inconsistency return types with other lookup_qualified funcs. Let the callers extract the SymbolNode from the SymbolTableNode.
1 parent e6b91bd commit 15f2385

File tree

3 files changed

+25
-35
lines changed

3 files changed

+25
-35
lines changed

mypy/fixup.py

Lines changed: 21 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,8 @@
44
from typing_extensions import Final
55

66
from mypy.nodes import (
7-
MypyFile, SymbolNode, SymbolTable, SymbolTableNode,
8-
TypeInfo, FuncDef, OverloadedFuncDef, Decorator, Var,
9-
TypeVarExpr, ClassDef, Block, TypeAlias,
7+
MypyFile, SymbolTable, TypeInfo, FuncDef, OverloadedFuncDef,
8+
Decorator, Var, TypeVarExpr, ClassDef, Block, TypeAlias,
109
)
1110
from mypy.types import (
1211
CallableType, Instance, Overloaded, TupleType, TypedDictType,
@@ -58,7 +57,8 @@ def visit_type_info(self, info: TypeInfo) -> None:
5857
if info.metaclass_type:
5958
info.metaclass_type.accept(self.type_fixer)
6059
if info._mro_refs:
61-
info.mro = [lookup_qualified_typeinfo(self.modules, name, self.allow_missing)
60+
info.mro = [lookup_fully_qualified_typeinfo(self.modules, name,
61+
allow_missing=self.allow_missing)
6262
for name in info._mro_refs]
6363
info._mro_refs = None
6464
finally:
@@ -74,8 +74,8 @@ def visit_symbol_table(self, symtab: SymbolTable, table_fullname: str) -> None:
7474
if cross_ref in self.modules:
7575
value.node = self.modules[cross_ref]
7676
else:
77-
stnode = lookup_qualified_stnode(self.modules, cross_ref,
78-
self.allow_missing)
77+
stnode = lookup_fully_qualified(cross_ref, self.modules,
78+
raise_on_missing=not self.allow_missing)
7979
if stnode is not None:
8080
assert stnode.node is not None, (table_fullname + "." + key, cross_ref)
8181
value.node = stnode.node
@@ -151,7 +151,8 @@ def visit_instance(self, inst: Instance) -> None:
151151
if type_ref is None:
152152
return # We've already been here.
153153
inst.type_ref = None
154-
inst.type = lookup_qualified_typeinfo(self.modules, type_ref, self.allow_missing)
154+
inst.type = lookup_fully_qualified_typeinfo(self.modules, type_ref,
155+
allow_missing=self.allow_missing)
155156
# TODO: Is this needed or redundant?
156157
# Also fix up the bases, just in case.
157158
for base in inst.type.bases:
@@ -167,7 +168,8 @@ def visit_type_alias_type(self, t: TypeAliasType) -> None:
< 10000 /div>
167168
if type_ref is None:
168169
return # We've already been here.
169170
t.type_ref = None
170-
t.alias = lookup_qualified_alias(self.modules, type_ref, self.allow_missing)
171+
t.alias = lookup_fully_qualified_alias(self.modules, type_ref,
172+
allow_missing=self.allow_missing)
171173
for a in t.args:
172174
a.accept(self)
173175

@@ -228,8 +230,8 @@ def visit_typeddict_type(self, tdt: TypedDictType) -> None:
228230
it.accept(self)
229231
if tdt.fallback is not None:
230232
if tdt.fallback.type_ref is not None:
231-
if lookup_qualified(self.modules, tdt.fallback.type_ref,
232-
self.allow_missing) is None:
233+
if lookup_fully_qualified(tdt.fallback.type_ref, self.modules,
234+
raise_on_missing=not self.allow_missing) is None:
233235
# We reject fake TypeInfos for TypedDict fallbacks because
234236
# the latter are used in type checking and must be valid.
235237
tdt.fallback.type_ref = 'typing._TypedDict'
@@ -261,9 +263,10 @@ def visit_type_type(self, t: TypeType) -> None:
261263
t.item.accept(self)
262264

263265

264-
def lookup_qualified_typeinfo(modules: Dict[str, MypyFile], name: str,
265-
allow_missing: bool) -> TypeInfo:
266-
node = lookup_qualified(modules, name, allow_missing)
266+
def lookup_fully_qualified_typeinfo(modules: Dict[str, MypyFile], name: str, *,
267+
allow_missing: bool) -> TypeInfo:
268+
stnode = lookup_fully_qualified(name, modules, raise_on_missing=not allow_missing)
269+
node = stnode.node if stnode else None
267270
if isinstance(node, TypeInfo):
268271
return node
269272
else:
@@ -275,9 +278,10 @@ def lookup_qualified_typeinfo(modules: Dict[str, MypyFile], name: str,
275278
return missing_info(modules)
276279

277280

278-
def lookup_qualified_alias(modules: Dict[str, MypyFile], name: str,
279-
allow_missing: bool) -> TypeAlias:
280-
node = lookup_qualified(modules, name, allow_missing)
281+
def lookup_fully_qualified_alias(modules: Dict[str, MypyFile], name: str, *,
282+
allow_missing: bool) -> TypeAlias:
283+
stnode = lookup_fully_qualified(name, modules, raise_on_missing=not allow_missing)
284+
node = stnode.node if stnode else None
281285
if isinstance(node, TypeAlias):
282286
return node
283287
else:
@@ -289,20 +293,6 @@ def lookup_qualified_alias(modules: Dict[str, MypyFile], name: str,
289293
return missing_alias()
290294

291295

292-
def lookup_qualified(modules: Dict[str, MypyFile], name: str,
293-
allow_missing: bool) -> Optional[SymbolNode]:
294-
stnode = lookup_qualified_stnode(modules, name, allow_missing)
295-
if stnode is None:
296-
return None
297-
else:
298-
return stnode.node
299-
300-
301-
def lookup_qualified_stnode(modules: Dict[str, MypyFile], name: str,
302-
allow_missing: bool) -> Optional[SymbolTableNode]:
303-
return lookup_fully_qualified(name, modules, raise_on_missing=not allow_missing)
304-
305-
306296
_SUGGESTION: Final = "<missing {}: *should* have gone away during fine-grained update>"
307297

308298

@@ -312,8 +302,7 @@ def missing_info(modules: Dict[str, MypyFile]) -> TypeInfo:
312302
dummy_def.fullname = suggestion
313303

314304
info = TypeInfo(SymbolTable(), dummy_def, "<missing>")
315-
obj_type = lookup_qualified(modules, 'builtins.object', False)
316-
assert isinstance(obj_type, TypeInfo)
305+
obj_type = lookup_fully_qualified_typeinfo(modules, 'builtins.object', allow_missing=False)
317306
info.bases = [Instance(obj_type, [])]
318307
info.mro = [info, obj_type]
319308
return info

mypy/lookup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
# TODO: gradually move existing lookup functions to this module.
1010

1111

12-
def lookup_fully_qualified(name: str, modules: Dict[str, MypyFile],
12+
def lookup_fully_qualified(name: str, modules: Dict[str, MypyFile], *,
1313
raise_on_missing: bool = False) -> Optional[SymbolTableNode]:
1414
"""Find a symbol using it fully qualified name.
1515

mypy/plugins/attrs.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
import mypy.plugin # To avoid circular imports.
99
from mypy.exprtotype import expr_to_unanalyzed_type, TypeTranslationError
10-
from mypy.fixup import lookup_qualified_stnode
10+
from mypy.lookup import lookup_fully_qualified
1111
from mypy.nodes import (
1212
Context, Argument, Var, ARG_OPT, ARG_POS, TypeInfo, AssignmentStmt,
1313
TupleExpr, ListExpr, NameExpr, CallExpr, RefExpr, FuncDef,
@@ -87,7 +87,8 @@ def argument(self, ctx: 'mypy.plugin.ClassDefContext') -> Argument:
8787
if self.converter.name:
8888
# When a converter is set the init_type is overridden by the first argument
8989
# of the converter method.
90-
converter = lookup_qualified_stnode(ctx.api.modules, self.converter.name, True)
90+
converter = lookup_fully_qualified(self.converter.name, ctx.api.modules,
91+
raise_on_missing=False)
9192
if not converter:
9293
# The converter may be a local variable. Check there too.
9394
converter = ctx.api.lookup_qualified(self.converter.name, self.info, True)

0 commit comments

Comments
 (0)
0