-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Always create correct instances #3305
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
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1063,15 +1063,23 @@ def class_type(self, info: TypeInfo) -> Type: | |
|
||
def named_type(self, qualified_name: str, args: List[Type] = None) -> Instance: | ||
sym = self.lookup_qualified(qualified_name, None) | ||
assert isinstance(sym.node, TypeInfo) | ||
return Instance(sym.node, args or []) | ||
node = sym.node | ||
assert isinstance(node, TypeInfo) | ||
if args: | ||
10000 | # TODO: assert len(args) == len(node.defn.type_vars) | |
return Instance(node, args) | ||
return Instance(node, [AnyType()] * len(node.defn.type_vars)) | ||
|
||
def named_type_or_none(self, qualified_name: str, args: List[Type] = None) -> Instance: | ||
sym = self.lookup_fully_qualified_or_none(qualified_name) | ||
if not sym: | ||
return None | ||
assert isinstance(sym.node, TypeInfo) | ||
return Instance(sym.node, args or []) | ||
node = sym.node | ||
assert isinstance(node, TypeInfo) | ||
if args: | ||
# TODO: assert len(args) == len(node.defn.type_vars) | ||
return Instance(node, args) | ||
return Instance(node, [AnyType()] * len(node.defn.type_vars)) | ||
|
||
def is_typeddict(self, expr: Expression) -> bool: | ||
return (isinstance(expr, RefExpr) and isinstance(expr.node, TypeInfo) and | ||
|
@@ -2040,7 +2048,7 @@ def build_namedtuple_typeinfo(self, name: str, items: List[str], types: List[Typ | |
# Actual signature should return OrderedDict[str, Union[types]] | ||
ordereddictype = (self.named_type_or_none('builtins.dict', [strtype, AnyType()]) | ||
or self.object_type()) | ||
fallback = self.named_type('__builtins__.tuple', types) | ||
fallback = self.named_type('__builtins__.tuple', [join.join_type_list(types)]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can't use type joins safely during semantic analysis, since MROs may be incomplete :-( One potential way to fix this would be to postpone all joins to a new, fourth semantic analysis pass. That sounds risky though, so for the 0.510 release we could just use This has happened so many times that I've been considering asserting during joins and other type operations that need MROs that they aren't called during semantic analysis, at least when running tests. |
||
# Note: actual signature should accept an invariant version of Iterable[UnionType[types]]. | ||
# but it can't be expressed. 'new' and 'len' should be callable types. | ||
iterable_type = self.named_type_or_none('typing.Iterable', [AnyType()]) | ||
|
@@ -3113,9 +3121,10 @@ def lookup_qualified(self, name: str, ctx: Context) -> SymbolTableNode: | |
return n | ||
|
||
def builtin_type(self, fully_qualified_name: str) -> Instance: | ||
node = self.lookup_fully_qualified(fully_qualified_name) | ||
assert isinstance(node.node, TypeInfo) | ||
return Instance(node.node, []) | ||
sym = self.lookup_fully_qualified(fully_qualified_name) | ||
node = sym.node | ||
assert isinstance(node, TypeInfo) | ||
return Instance(node, [AnyType()] * len(node.defn.type_vars)) | ||
|
||
def lookup_fully_qualified(self, name: str) -> SymbolTableNode: | ||
"""Lookup a fully qualified name. | ||
|
@@ -3673,8 +3682,12 @@ def fail_blocker(self, msg: str, ctx: Context) -> None: | |
def builtin_type(self, name: str, args: List[Type] = None) -> Instance: | ||
names = self.modules['builtins'] | ||
sym = names.names[name] | ||
assert isinstance(sym.node, TypeInfo) | ||
return Instance(sym.node, args or []) | ||
node = sym.node | ||
assert isinstance(node, TypeInfo) | ||
if args: | ||
# TODO: assert len(args) == len(node.defn.type_vars) | ||
return Instance(node, args) | ||
return Instance(node, [AnyType()] * len(node.defn.type_vars)) | ||
|
||
|
||
def replace_implicit_first_type(sig: FunctionLike, new: Type) -> FunctionLike: | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is an Aha! that deserves a comment!