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

Conversation

ilevkivskyi
Copy link
Member

In new semantic analyzer, qualified_name() returns a bare name if called inside a top-level (non-method) function. I am not sure for motivation and can't say it is generally wrong. Here is the hot-fix for a case where this is definitely wrong: generated classes.

@ilevkivskyi ilevkivskyi requested a review from JukkaL April 4, 2019 14:23
Copy link
Collaborator

@JukkaL JukkaL left a comment

Choose a reason for hiding this comment

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

Not sure if this is best way to fix the issue, but I didn't look very carefully. Left a few additional idea.

It's okay to also merge this as is and create a follow-up issue about cleaning this up, since the whole concept of nested classes leaking into module symbol tables could be improved.

@@ -2803,6 +2803,10 @@ 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.

if self.is_func_scope() and not self.type:
# 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.
class_def.fullname = self.cur_mod_id + '.' + class_def.fullname
Copy link
Collaborator

Choose a reason for hiding this comment

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

It looks like this can collide with another class name. Maybe you should use the @line suffix that we use elsewhere?

Copy link
Member Author

Choose a reason for hiding this comment

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

This should be already done by class generation code. I will add a comment.

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.

@ilevkivskyi
Copy link
Member Author

The issue for this is essentially #6422, but this is only partial fix.

Copy link
Collaborator

@JukkaL JukkaL left a comment

Choose a reason for hiding this comment

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

Thanks for the updates!

@ilevkivskyi ilevkivskyi merged commit 871a871 into python:master Apr 4, 2019
@ilevkivskyi ilevkivskyi deleted the fix-newan-crash-nt branch April 4, 2019 16:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants