Skip to content

New semantic analyzer: fix deserialization of generated classes #6627

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 2 commits into from
Apr 4, 2019
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
9 changes: 8 additions & 1 deletion mypy/newsemanal/semanal.py
Original file line number Diff line number Diff line change
Expand Up @@ -2802,7 +2802,14 @@ def process_typevar_parameters(self, args: List[Expression],

def basic_new_typeinfo(self, name: str, basetype_or_fallback: Instance) -> TypeInfo:
class_def = ClassDef(name, Block([]))
class_def.fullname = self.qualified_name(name)
if self.is_func_scope() and not self.type:
Copy link
Collaborator

Choose a reason for hiding this comment

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

This would be better as an if/else statement.

Copy link
Member Author

Choose a reason for hiding this comment

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

OK.

# Full names of generated classes should always be prefixed with the module names
# even if they are nested in a function, since these classes will be (de-)serialized.
# (Note that the caller should append @line to the name to avoid collisions.)
# TODO: clean this up, see #6422.
class_def.fullname = self.cur_mod_id + '.' + self.qualified_name(name)
else:
class_def.fullname = self.qualified_name(name)

info = TypeInfo(SymbolTable(), class_def, self.cur_mod_id)
class_def.info = info
Expand Down
19 changes: 19 additions & 0 deletions test-data/unit/check-incremental.test
Original file line number Diff line number Diff line change
Expand Up @@ -4949,6 +4949,25 @@ NT = NamedTuple('BadName', [('x', int)])
[out2]
tmp/a.py:3: error: Revealed type is 'Tuple[builtins.int, fallback=b.BadName@2]'

[case testNewAnalyzerIncrementalBrokenNamedTupleNested]
# flags: --new-semantic-analyzer
import a
[file a.py]
from b import C
x: C
[file a.py.2]
from b import C
x: C
# touch
[file b.py]
class C: ...
from collections import namedtuple
def test() -> None:
NT = namedtuple('BadName', ['x', 'y'])
Copy link
Collaborator

Choose a reason for hiding this comment

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

I wonder if we could instead not serialize the class, since it's only visible within the function body.

Another idea would be to ignore the 'BadName' argument here and treat it as NT. Would this fix the issue?

Copy link
Member Author

Choose a reason for hiding this comment

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

Actually, this is a good idea. A type can leak to outer scope only from a method, so we can just don't serialize the class for non-method functions.

Copy link
Member Author

Choose a reason for hiding this comment

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

As we discussed privately, we will stick to just fixing the full name for now.

[builtins fixtures/list.pyi]
[out]
[out2]

[case testNewAnalyzerIncrementalMethodNamedTuple]
# flags: --new-semantic-analyzer
import a
Expand Down