8000 Remove redundant lookup funcs in fixup.py by 97littleleaf11 · Pull Request #11253 · python/mypy · GitHub
[go: up one dir, main page]

Skip to content

Remove redundant lookup funcs in fixup.py #11253

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Oct 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 21 additions & 32 deletions mypy/fixup.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@
from typing_extensions import Final

from mypy.nodes import (
MypyFile, SymbolNode, SymbolTable, SymbolTableNode,
TypeInfo, FuncDef, OverloadedFuncDef, Decorator, Var,
TypeVarExpr, ClassDef, Block, TypeAlias,
MypyFile, SymbolTable, TypeInfo, FuncDef, OverloadedFuncDef,
Decorator, Var, TypeVarExpr, ClassDef, Block, TypeAlias,
)
from mypy.types import (
CallableType, Instance, Overloaded, TupleType, TypedDictType,
Expand Down Expand Up @@ -58,7 +57,8 @@ def visit_type_info(self, info: TypeInfo) -> None:
if info.metaclass_type:
info.metaclass_type.accept(self.type_fixer)
if info._mro_refs:
info.mro = [lookup_qualified_typeinfo(self.modules, name, self.allow_missing)
info.mro = [lookup_fully_qualified_typeinfo(self.modules, name,
allow_missing=self.allow_missing)
for name in info._mro_refs]
info._mro_refs = None
finally:
Expand All @@ -74,8 +74,8 @@ def visit_symbol_table(self, symtab: SymbolTable, table_fullname: str) -> None:
if cross_ref in self.modules:
value.node = self.modules[cross_ref]
else:
stnode = lookup_qualified_stnode(self.modules, cross_ref,
self.allow_missing)
stnode = lookup_fully_qualified(cross_ref, self.modules,
raise_on_missing=not self.allow_missing)
if stnode is not None:
assert stnode.node is not None, (table_fullname + "." + key, cross_ref)
value.node = stnode.node
Expand Down Expand Up @@ -151,7 +151,8 @@ def visit_instance(self, inst: Instance) -> None:
if type_ref is None:
return # We've already been here.
inst.type_ref = None
inst.type = lookup_qualified_typeinfo(self.modules, type_ref, self.allow_missing)
inst.type = lookup_fully_qualified_typeinfo(self.modules, type_ref,
allow_missing=self.allow_missing)
# TODO: Is this needed or redundant?
# Also fix up the bases, just in case.
for base in inst.type.bases:
Expand All @@ -167,7 +168,8 @@ def visit_type_alias_type(self, t: TypeAliasType) -> None:
if type_ref is None:
return # We've already been here.
t.type_ref = None
t.alias = lookup_qualified_alias(self.modules, type_ref, self.allow_missing)
t.alias = lookup_fully_qualified_alias(self.modules, type_ref,
allow_missing=self.allow_missing)
for a in t.args:
a.accept(self)

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


def lookup_qualified_typeinfo(modules: Dict[str, MypyFile], name: str,
allow_missing: bool) -> TypeInfo:
node = lookup_qualified(modules, name, allow_missing)
def lookup_fully_qualified_typeinfo(modules: Dict[str, MypyFile], name: str, *,
allow_missing: bool) -> TypeInfo:
stnode = lookup_fully_qualified(name, modules, raise_on_missing=not allow_missing)
node = stnode.node if stnode else None
if isinstance(node, TypeInfo):
return node
else:
Expand All @@ -275,9 +278,10 @@ def lookup_qualified_typeinfo(modules: Dict[str, MypyFile], name: str,
return missing_info(modules)


def lookup_qualified_alias(modules: Dict[str, MypyFile], name: str,
allow_missing: bool) -> TypeAlias:
node = lookup_qualified(modules, name, allow_missing)
def lookup_fully_qualified_alias(modules: Dict[str, MypyFile], name: str, *,
allow_missing: bool) -> TypeAlias:
stnode = lookup_fully_qualified(name, modules, raise_on_missing=not allow_missing)
node = stnode.node if stnode else None
if isinstance(node, TypeAlias):
return node
else:
Expand All @@ -289,20 +293,6 @@ def lookup_qualified_alias(modules: Dict[str, MypyFile], name: str,
return missing_alias()


def lookup_qualified(modules: Dict[str, MypyFile], name: str,
allow_missing: bool) -> Optional[SymbolNode]:
stnode = lookup_qualified_stnode(modules, name, allow_missing)
if stnode is None:
return None
else:
return stnode.node


def lookup_qualified_stnode(modules: Dict[str, MypyFile], name: str,
allow_missing: bool) -> Optional[SymbolTableNode]:
return lookup_fully_qualified(name, modules, raise_on_missing=not allow_missing)


_SUGGESTION: Final = "<missing {}: *should* have gone away during fine-grained update>"


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

info = TypeInfo(SymbolTable(), dummy_def, "<missing>")
obj_type = lookup_qualified(modules, 'builtins.object', False)
assert isinstance(obj_type, TypeInfo)
obj_type = lookup_fully_qualified_typeinfo(modules, 'builtins.object', allow_missing=False)
info.bases = [Instance(obj_type, [])]
info.mro = [info, obj_type]
return info
Expand Down
2 changes: 1 addition & 1 deletion mypy/lookup.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
# TODO: gradually move existing lookup functions to this module.


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

Expand Down
5 changes: 3 additions & 2 deletions mypy/plugins/attrs.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

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