8000 Fix crashes in class scoped imports by hauntsaninja · Pull Request #12023 · python/mypy · GitHub
[go: up one dir, main page]

Skip to content

Fix crashes in class scoped imports #12023

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 33 commits into from
Feb 17, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
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
8000
Diff view
Prev Previous commit
Next Next commit
fix more tests
  • Loading branch information
hauntsaninja committed Jan 20, 2022
commit 63f8634eb1d33c554d46bdf554f0b8418c5c57fc
27 changes: 19 additions & 8 deletions mypy/semanal.py
Original file line number Diff line number Diff line change
Expand Up @@ -4787,15 +4787,26 @@ def add_imported_symbol(self,
"""Add an alias to an existing symbol through import."""
assert not module_hidden or not module_public

node_node: Optional[SymbolNode] = node.node
if self.is_class_scope() and isinstance(node_node, (FuncBase, Var)):
# Imports inside class scope do not produce methods.
# We construct a Var so as to treat them more like assignments.
node_node = Var(node_node.name, node_node.type)
assert self.type is not None
node_node.info = self.type
symbol_node: Optional[SymbolNode] = node.node
if self.is_class_scope() and isinstance(symbol_node, (FuncBase, Var)):
# We construct a new node to represent this symbol and set its `info` attribute
# to `self.type`. Note that imports inside class scope do not produce methods, so
# in all cases constructing a Var gets us the assignment like behaviour we want.
existing = self.current_symbol_table().get(name)
if (
existing is not None
and isinstance(existing.node, Var)
and existing.type == symbol_node.type
):
# The redefinition checks in `add_symbol_table_node` don't work for our
# constructed Var, so check for possible redefinitions here.
symbol_node = existing.node
else:
symbol_node = Var(symbol_node.name, symbol_node.type)
assert self.type is not None
symbol_node.info = self.type

symbol = SymbolTableNode(node.kind, node_node,
symbol = SymbolTableNode(node.kind, symbol_node,
module_public=module_public,
module_hidden=module_hidden)
self.add_symbol_table_node(name, symbol, context)
Expand Down
16 changes: 16 additions & 0 deletions test-data/unit/check-classes.test
Original file line number Diff line number Diff line change
Expand Up @@ -7161,6 +7161,22 @@ reveal_type(Foo.mod.foo) # N: Revealed type is "builtins.int"
[file mod.py]
foo: int

[case testClassScopeImportFunctionAlias]
class Foo:
from mod import foo
bar = foo

from mod import const_foo
const_bar = const_foo

reveal_type(Foo.foo) # N: Revealed type is "def (x: builtins.int, y: builtins.int) -> builtins.int"
reveal_type(Foo.bar) # N: Revealed type is "def (x: builtins.int, y: builtins.int) -> builtins.int"
reveal_type(Foo.const_foo) # N: Revealed type is "builtins.int"
reveal_type(Foo.const_bar) # N: Revealed type is "builtins.int"
[file mod.py]
def foo(x: int, y: int) -> int: ...
const_foo: int

[case testClassScopeImportModuleStar]
class Foo:
from mod import *
Copy link
Member

Choose a reason for hiding this comment

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

This is actually a syntax error, you can only do import * in the global scope (at least in Python 3).

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Huh, TIL. Tried on Python 2 and it works but gives me a SyntaxWarning. Thanks Python 3!

Copy link
Member

Choose a reason for hiding this comment

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

I think tests should tell us about syntax errors. Why do they pass? 🤔

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Most syntax errors happen at parse time, but some, like this one, do not. The ones that happen later often cause problems for mypy, e.g. this: #11499 or from __future__ import braces

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Another example is nonlocal x at module level

Expand Down
2 changes: 1 addition & 1 deletion test-data/unit/semanal-modules.test
Original file line number Diff line number Diff line change
Expand Up @@ -568,7 +568,7 @@ MypyFile:1(
ImportFrom:2(_x, [y])
AssignmentStmt:3(
NameExpr(z* [m])
NameExpr(y [_x.y]))))
NameExpr(y [None]))))

[case testImportInClassBody2]
class A:
Expand Down
0