10000 Remove builtin_type by 97littleleaf11 · Pull Request #11214 · python/mypy · GitHub
[go: up one dir, main page]

Skip to content

Remove builtin_type #11214

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

Closed
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Remove builtin_type
  • Loading branch information
97littleleaf11 committed Sep 27, 2021
commit 57b6eee9762f6fb9e9d711318c2e19cf0c9cf3c3
5 changes: 0 additions & 5 deletions mypy/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -297,11 +297,6 @@ def class_type(self, self_type: Type) -> Type:
"""Generate type of first argument of class methods from type of self."""
raise NotImplementedError

@abstractmethod
def builtin_type(self, fully_qualified_name: str) -> Instance:
"""Deprecated: use named_type instead."""
raise NotImplementedError

@abstractmethod
def lookup_fully_qualified(self, name: str) -> Optional[SymbolTableNode]:
"""Lookup a symbol by its fully qualified name.
Expand Down
2 changes: 1 addition & 1 deletion mypy/plugins/attrs.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ def argument(self, ctx: 'mypy.plugin.ClassDefContext') -> Argument:
converter_type: Optional[Type] = None
if converter and isinstance(converter.node, TypeInfo):
from mypy.checkmember import type_object_type # To avoid import cycle.
converter_type = type_object_type(converter.node, ctx.api.builtin_type)
converter_type = type_object_type(converter.node, ctx.api.named_type_or_none)
elif converter and isinstance(converter.node, OverloadedFuncDef):
10000 converter_type = converter.node.type
elif converter and converter.type:
Expand Down
20 changes: 5 additions & 15 deletions mypy/semanal.py
Original file line number Diff line number Diff line change
Expand Up @@ -732,7 +732,7 @@ def analyze_overloaded_func_def(self, defn: OverloadedFuncDef) -> None:
# This is a property.
first_item.func.is_overload = True
self.analyze_property_with_multi_part_definition(defn)
typ = function_type(first_item.func, self.builtin_type('builtins.function'))
typ = function_type(first_item.func, self.named_type_or_none('builtins.function'))
assert isinstance(typ, CallableType)
types = [typ]
else:
Expand Down Expand Up @@ -789,7 +789,7 @@ def analyze_overload_sigs_and_impl(
item.accept(self)
# TODO: support decorated overloaded functions properly
if isinstance(item, Decorator):
callable = function_type(item.func, self.builtin_type('builtins.function'))
callable = function_type(item.func, self.named_type_or_none('builtins.function'))
assert isinstance(callable, CallableType)
if not any(refers_to_fullname(dec, 'typing.overload')
for dec in item.decorators):
Expand Down Expand Up @@ -1736,7 +1736,7 @@ def analyze_metaclass(self, defn: ClassDef) -> None:
defn.info.metaclass_type.type.fullname == 'builtins.type'):
# All protocols and their subclasses have ABCMeta metaclass by default.
# TODO: add a metaclass conflict check if there is another metaclass.
abc_meta = self.named_type_or_none('abc.ABCMeta', [])
abc_meta = self.named_type_or_none('abc.ABCMeta')
if abc_meta is not None: # May be None in tests with incomplete lib-stub.
defn.info.metaclass_type = abc_meta
if defn.info.metaclass_type is None:
Expand Down Expand Up @@ -4308,7 +4308,7 @@ def get_module_symbol(self, node: MypyFile, name: str) -> Optional[SymbolTableNo
gvar = self.create_getattr_var(names['__getattr__'], name, fullname)
if gvar:
sym = SymbolTableNode(GDEF, gvar)
elif self.is_missing_module(fullname):
elif fullname in self.missing_modules:
# We use the fullname of the original definition so that we can
# detect whether two names refer to the same thing.
var_type = AnyType(TypeOfAny.from_unimported_type)
Expand All @@ -4319,9 +4319,6 @@ def get_module_symbol(self, node: MypyFile, name: str) -> Optional[SymbolTableNo
sym = None
return sym

def is_missing_module(self, module: str) -> bool:
return module in self.missing_modules

def implicit_symbol(self, sym: SymbolTableNode, name: str, parts: List[str],
source_type: AnyType) -> SymbolTableNode:
"""Create symbol for a qualified name reference through Any type."""
Expand Down Expand Up @@ -4385,13 +4382,6 @@ def lookup_fully_qualified(self, fullname: str) -> Optional[SymbolTableNode]:
self.record_incomplete_ref()
return result

def builtin_type(self, fully_qualified_name: str) -> Instance:
sym = self.lookup_fully_qualified(fully_qualified_name)
assert sym is not None
node = sym.node
assert isinstance(node, TypeInfo)
return Instance(node, [AnyType(TypeOfAny.special_form)] * len(node.defn.type_vars))

def object_type(self) -> Instance:
return self.named_type('__builtins__.object')

Expand Down Expand Up @@ -4421,7 +4411,7 @@ def named_type_or_none(self, qualified_name: str,
if args is not None:
# TODO: assert len(args) == len(node.defn.type_vars)
return Instance(node, args)
return Instance(node, [AnyType(TypeOfAny.unannotated)] * len(node.defn.type_vars))
return Instance(node, [AnyType(TypeOfAny.special_form)] * len(node.defn.type_vars))
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's quite confusing that builtin_type and named_type use TypeOfAny.special_form while named_type_or_none uses TypeOfAny.unannotated. Is it correct that changing it to special_form


def lookup_current_scope(self, name: str) -> Optional[SymbolTableNode]:
if self.locals[-1] is not None:
Expand Down
4 changes: 3 additions & 1 deletion mypy/suggestions.py
Original file line number Diff line number Diff line change
Expand Up @@ -649,7 +649,9 @@ def ensure_loaded(self, state: State, force: bool = False) -> MypyFile:
return state.tree

def builtin_type(self, s: str) -> Instance:
return self.manager.semantic_analyzer.builtin_type(s)
ret = self.manager.semantic_analyzer.named_type_or_none(s)
assert ret
return ret

def json_suggestion(self, mod: str, func_name: str, node: FuncDef,
suggestion: PyAnnotateSignature) -> str:
Expand Down
4 changes: 2 additions & 2 deletions test-data/unit/plugins/common_api_incremental.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def add_info_hook(ctx) -> None:

info = TypeInfo(SymbolTable(), class_def, ctx.api.cur_mod_id)
class_def.info = info
obj = ctx.api.builtin_type('builtins.object')
obj = ctx.api.named_type_or_none('builtins.object')
info.mro = [info, obj.type]
info.bases = [obj]
ctx.api.add_symbol_table_node(ctx.name, SymbolTableNode(GDEF, info))
Expand All @@ -33,7 +33,7 @@ def add_info_hook(ctx) -> None:

def add_magic_hook(ctx) -> None:
info = ctx.cls.info
str_type = ctx.api.named_type_or_none('builtins.str', [])
str_type = ctx.api.named_type_or_none('builtins.str')
assert str_type is not None
var = Var('__magic__', str_type)
var.info = info
Expand Down
2 changes: 1 addition & 1 deletion test-data/unit/plugins/dyn_class.py
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def add_info_hook(ctx):

info = TypeInfo(SymbolTable(), class_def, ctx.api.cur_mod_id)
class_def.info = info
obj = ctx.api.builtin_type('builtins.object')
obj = ctx.api.named_type_or_none('builtins.object')
info.mro = [info, obj.type]
info.bases = [obj]
ctx.api.add_symbol_table_node(ctx.name, SymbolTableNode(GDEF, info))
Expand Down
2 changes: 1 addition & 1 deletion test-data/unit/plugins/dyn_class_from_method.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def add_info_hook(ctx: DynamicClassDefContext):
class_def.info = info
queryset_type_fullname = ctx.call.args[0].fullname
queryset_info = ctx.api.lookup_fully_qualified(queryset_type_fullname).node # type: TypeInfo
obj = ctx.api.builtin_type('builtins.object')
obj = ctx.api.named_type_or_none('builtins.object')
info.mro = [info, queryset_info, obj.type]
info.bases = [Instance(queryset_info, [])]
ctx.api.add_symbol_table_node(ctx.name, SymbolTableNode(GDEF, info))
Expand Down
0