Repository navigation
Prohibit list[int], etc (those fail at runtime) #2869
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 1 commit
46ad58b
0673935
13d7067
66f1a1d
4fe5399
529a963
0746f87
18b0cd9
504a324
8591a7c
4ac37f4
c227b11
f011425
177173f
7d7e8ca
cc5fb0e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -90,6 +90,15 @@ def get_column(self) -> int: pass | |
| for alias, name in type_aliases.items()) # type: Dict[str, str] | ||
|
|
||
|
|
||
| nongen_builtins = {'builtins.tuple': 'typing.Tuple', | ||
| 'builtins.frozenset': 'typing.FrozenSet', | ||
| 'builtins.enumerate': '', | ||
| 'collections.defaultdict': 'typing.DefaultDict', | ||
| 'collections.Counter': 'typing.Counter', | ||
| 'collections.ChainMap': 'typing.ChainMap'} | ||
| nongen_builtins.update(reverse_type_aliases) | ||
|
|
||
|
|
||
| # See [Note Literals and literal_hash] below | ||
| Key = tuple | ||
|
|
||
|
|
@@ -2143,17 +2152,20 @@ class SymbolTableNode: | |
| # For deserialized MODULE_REF nodes, the referenced module name; | ||
| # for other nodes, optionally the name of the referenced object. | ||
| cross_ref = None # type: Optional[str] | ||
| # Was this node created by normaloze_type_alias? | ||
|
Member
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. normalize
Member
Author
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. Thanks! Fixed. |
||
| normalized = False # type: bool | ||
|
|
||
| def __init__(self, kind: int, node: Optional[SymbolNode], mod_id: str = None, | ||
| typ: 'mypy.types.Type' = None, | ||
| tvar_def: 'mypy.types.TypeVarDef' = None, | ||
| module_public: bool = True) -> None: | ||
| module_public: bool = True, normalized: bool = False) -> None: | ||
| self.kind = kind | ||
| self.node = node | ||
| self.type_override = typ | ||
| self.mod_id = mod_id | ||
| self.tvar_def = tvar_def | ||
| self.module_public = module_public | ||
| self.normalized = normalized | ||
|
|
||
| @property | ||
| def fullname(self) -> str: | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -65,7 +65,7 @@ | |
| SetComprehension, DictionaryComprehension, TYPE_ALIAS, TypeAliasExpr, | ||
| YieldExpr, ExecStmt, Argument, BackquoteExpr, ImportBase, AwaitExpr, | ||
| IntExpr, FloatExpr, UnicodeExpr, EllipsisExpr, TempNode, | ||
| COVARIANT, CONTRAVARIANT, INVARIANT, UNBOUND_IMPORTED, LITERAL_YES, | ||
| COVARIANT, CONTRAVARIANT, INVARIANT, UNBOUND_IMPORTED, LITERAL_YES, nongen_builtins, | ||
| ) | ||
| from mypy.typevars import has_no_typevars, fill_typevars | ||
| from mypy.visitor import NodeVisitor | ||
|
|
@@ -77,7 +77,9 @@ | |
| TupleType, UnionType, StarType, EllipsisType, function_type, TypedDictType, | ||
| ) | ||
| from mypy.nodes import implicit_module_attrs | ||
| from mypy.typeanal import TypeAnalyser, TypeAnalyserPass3, analyze_type_alias | ||
| from mypy.typeanal import ( | ||
| TypeAnalyser, TypeAnalyserPass3, analyze_type_alias, no_subscript_builtin_alias, | ||
| ) | ||
| from mypy.exprtotype import expr_to_unanalyzed_type, TypeTranslationError | ||
| from mypy.sametypes import is_same_type | ||
| from mypy.options import Options | ||
|
|
@@ -1194,7 +1196,8 @@ def visit_import_from(self, imp: ImportFrom) -> None: | |
| symbol = SymbolTableNode(node.kind, node.node, | ||
| self.cur_mod_id, | ||
| node.type_override, | ||
| module_public=module_public) | ||
| module_public=module_public, | ||
| normalized=node.normalized) | ||
| self.add_symbol(imported_id, symbol, imp) | ||
| elif module and not missing: | ||
| # Missing attribute. | ||
|
|
@@ -1230,12 +1233,18 @@ def process_import_over_existing_name(self, | |
|
|
||
| def normalize_type_alias(self, node: SymbolTableNode, | ||
| ctx: Context) -> SymbolTableNode: | ||
| normalized = False | ||
| if node.fullname in type_aliases: | ||
|
Member
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. This is the only place I can find where frozenset is treated differently -- do you understand why? |
||
| # Node refers to an aliased type such as typing.List; normalize. | ||
| node = self.lookup_qualified(type_aliases[node.fullname], ctx) | ||
| normalized = True | ||
| if node.fullname == 'typing.DefaultDict': | ||
| self.add_module_symbol('collections', '__mypy_collections__', False, ctx) | ||
| node = self.lookup_qualified('__mypy_collections__.defaultdict', ctx) | ||
| normalized = True | ||
| if normalized: | ||
| node = SymbolTableNode(node.kind, node.node, | ||
| node.mod_id, node.type_override, normalized=True) | ||
| return node | ||
|
|
||
| def correct_relative_import(self, node: Union[ImportFrom, ImportAll]) -> str: | ||
|
|
@@ -1271,7 +1280,8 @@ def visit_import_all(self, i: ImportAll) -> None: | |
| continue | ||
| self.add_symbol(name, SymbolTableNode(node.kind, node.node, | ||
| self.cur_mod_id, | ||
| node.type_override), i) | ||
| node.type_override, | ||
| normalized=node.normalized), i) | ||
| else: | ||
| # Don't add any dummy symbols for 'from x import *' if 'x' is unknown. | ||
| pass | ||
|
|
@@ -1322,7 +1332,7 @@ def anal_type(self, t: Type, allow_tuple_literal: bool = False, | |
| a = TypeAnalyser(self.lookup_qualified, | ||
| self.lookup_fully_qualified, | ||
| self.fail, | ||
| aliasing=aliasing) | ||
| aliasing=aliasing, allow_unnormalized=self.is_stub_file) | ||
| return t.accept(a) | ||
| else: | ||
| return None | ||
|
|
@@ -1344,7 +1354,7 @@ def visit_assignment_stmt(self, s: AssignmentStmt) -> None: | |
| res = analyze_type_alias(s.rvalue, | ||
| self.lookup_qualified, | ||
| self.lookup_fully_qualified, | ||
| self.fail) | ||
| self.fail, allow_unnormalized=self.is_stub_file) | ||
| if res and (not isinstance(res, Instance) or res.args): | ||
| # TODO: What if this gets reassigned? | ||
| name = s.lvalues[0] | ||
|
|
@@ -2633,7 +2643,7 @@ def visit_index_expr(self, expr: IndexExpr) -> None: | |
| res = analyze_type_alias(expr, | ||
| self.lookup_qualified, | ||
| self.lookup_fully_qualified, | ||
| self.fail) | ||
| self.fail, allow_unnormalized=self.is_stub_file) | ||
| expr.analyzed = TypeAliasExpr(res, fallback=self.alias_fallback(res), | ||
| in_runtime=True) | ||
| elif refers_to_class_or_function(expr.base): | ||
|
|
@@ -2654,6 +2664,14 @@ def visit_index_expr(self, expr: IndexExpr) -> None: | |
| types.append(typearg) | ||
| expr.analyzed = TypeApplication(expr.base, types) | ||
| expr.analyzed.line = expr.line | ||
| # list, dict, set are not directly subscriptable | ||
| try: | ||
| t = expr_to_unanalyzed_type(expr.base) | ||
| except TypeTranslationError: | ||
| return | ||
| n = self.lookup_qualified(t.name, expr) | ||
| if not n.normalized and n.fullname in nongen_builtins: | ||
| self.fail(no_subscript_builtin_alias(n.fullname), expr) | ||
| else: | ||
| expr.index.accept(self) | ||
|
|
||
|
|
||
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.
I'm curious why this isn't in
type_aliasesabove. How is frozenset different from set?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.
Oh, it looks like an old oversight. I just checked stubs for
builtins.frozensetandtyping.FrozenSet, the former is much better (it has all actual methods). I can easily fix this, but this requires a tiny PR to typeshed to be merged first and synced python/typeshed#979Could you please merge it?