Skip to content

Prevent crash on retrieving TypeVar from class #2544

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 5 commits into from
Dec 14, 2016

Conversation

TRManderson
Copy link
Contributor

Fixes #2530.

The alias is still broken because it's not at the top level, but we no longer crash. The choice to return the upper bound of the TypeVar as the type of the expression was a best guess, I wasn't sure what to do there, so please correct me if I'm wrong.

@gvanrossum
Copy link
Member

Can you handle the merge conflict? (We just merged a PR that touches almost all test files... Sorry...)

@gvanrossum gvanrossum changed the title Accessing TypeVar members of classes no longer crashes mypy Prevent crash on retrieving TypeVar from class Dec 9, 2016
@gvanrossum
Copy link
Member

Can you deal with the conflict? (Same cause as before -- there's just no way to land such a PR without causing conflicts.)

@TRManderson
Copy link
Contributor Author

Merged 👍

T = TypeVar('T', int)
def f(self, x: T) -> T:
A = C.T
return x
Copy link
Member

Choose a reason for hiding this comment

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

Can you add the missing newline here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed

@@ -378,6 +378,9 @@ def analyze_class_attribute_access(itype: Instance,
not_ready_callback(name, context)
return AnyType()

if isinstance(node.node, TypeVarExpr):
return node.node.upper_bound
Copy link
Member

Choose a reason for hiding this comment

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

If we don't support it, this should also report an error message. (In error recovery mode, returning the upper bound sounds fine.)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

As far as I know (not very far) we theoretically do support accessing TypeVars that are members of a class, we just don't support aliasing them. I'll try and come up with a valid example (or counterexample) after I finish work.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Also, WRT returning the upper bound, I feel like it's wrong in this context - the type of accessing a class member TypeVar should be TypeVar(I'm not 100% on how that's internally represented), and aliasing should still fail.

@gvanrossum
Copy link
Member

OK, it sounds like you know what you want to do, I'll wait until you tell me you're ready for the next review.

@gvanrossum
Copy link
Member

LGTM now -- if you're happy with it I can merge it.

@TRManderson
Copy link
Contributor Author

Yeah, I've got a few things I want to do regarding warnings but they're better off in another PR. Thanks :)

@gvanrossum gvanrossum merged commit e9d28a0 into python:master Dec 14, 2016
@gvanrossum
Copy link
Member

Thanks!

@TRManderson TRManderson deleted the aliasing-crash branch December 14, 2016 00:56
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