8000 Make it an error to use a class-attribute type var outside a type by sixolet · Pull Request #3105 · python/mypy · GitHub
[go: up one dir, main page]

Skip to content

Make it an error to use a class-attribute type var outside a type #3105

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 7 commits into from
Apr 3, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Fix the error message to handle more cases
  • Loading branch information
sixolet committed Apr 1, 2017
commit fe6eee173d611bb574ef5e2f1200919da0f0e125
4 changes: 2 additions & 2 deletions mypy/checkmember.py
Original file line number Diff line number Diff line change
Expand Up @@ -406,8 +406,8 @@ def analyze_class_attribute_access(itype: Instance,
return AnyType()

if isinstance(node.node, TypeVarExpr):
msg.fail('Type variable "{}" is invalid as target for type alias'.format(
node.node.fullname() or node.node.name()), context)
msg.fail('Type variable "{}" is invalid except as a parameter to another type'.format(
node.node.fullname() or node.node.name()), context)
return AnyType()

if isinstance(node.node, TypeInfo):
Expand Down
1 change: 1 addition & 0 deletions test-data/unit/check-generics.test
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class A(Generic[T]):

class B: pass
class C: pass

[out]
main:4: error: Incompatible types in assignment (expression has type "B", variable has type "C")

Expand Down
3 changes: 2 additions & 1 deletion test-data/unit/check-typevar-values.test
Original file line number Diff line number Diff line change
Expand Up @@ -503,7 +503,8 @@ class C:
L = List[C.T] # a valid type alias
Copy link
Member

Choose a reason for hiding this comment

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

Would doing y: C.T = x be valid?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes, and good point -- that's not "as a parameter to a type" that's "as a type"

How to express "Don't try and use this at runtime"?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I guess "Is only valid when used as a type or a parameter to a type in a type alias" covers all the bases, but is wicked confusing if I don't already know exactly what it's talking about.

Copy link
Member

Choose a reason for hiding this comment

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

Some time ago we had "... is invalid in runtime context" for subscripted generics. Maybe it is better to say "... can not be used in runtime context, only in type context".

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Oh! How about

  • "Type variable 'C.T' is not a valid expression"
  • "Type variable 'C.T' cannot be used as an expression"
    ?

Copy link
Member

Choose a reason for hiding this comment

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

The second option sounds good.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Ok, rolling with @ilevkivskyi's suggestion for now.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

... as I incur more and more test changes as I can't decide. I think I like how it is now.

l: L = []
l.append(x)
A = C.T # E: Type variable "T" is invalid as target for type alias
C.T # E: Type variable "T" is invalid except as a parameter to another type
A = C.T # E: Type variable "T" is invalid except as a parameter to another type
return l[0]

[builtins fixtures/list.pyi]
Expand Down
0