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
Merged
Show file tree
Hide file tree
Changes from 3 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
5 changes: 4 additions & 1 deletion mypy/checkmember.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
Overloaded, TypeVarType, UnionType, PartialType,
DeletedType, NoneTyp, TypeType, function_type
)
from mypy.nodes import TypeInfo, FuncBase, Var, FuncDef, SymbolNode, Context, MypyFile
from mypy.nodes import TypeInfo, FuncBase, Var, FuncDef, SymbolNode, Context, MypyFile, TypeVarExpr
from mypy.nodes import ARG_POS, ARG_STAR, ARG_STAR2
from mypy.nodes import Decorator, OverloadedFuncDef
from mypy.messages import MessageBuilder
Expand Down Expand Up @@ -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.


if isinstance(node.node, TypeInfo):
return type_object_type(node.node, builtin_type)

Expand Down
8 changes: 8 additions & 0 deletions test-data/unit/check-typevar-values.test
Original file line number Diff line number Diff line change
Expand Up @@ -489,3 +489,11 @@ def outer(x: T) -> T:
return inner1('a') # E: Argument 1 to "inner1" has incompatible type "str"; expected "int"
return inner1(x)
[out]

[case testClassMemberTypeVarInFunctionBody]
from typing import TypeVar
class C:
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