Skip to content

Update generic class attribute and type variable usage #239

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 6 commits into from
May 11, 2017

Conversation

pkch
Copy link
Contributor

@pkch pkch commented Apr 7, 2017

Clarification based on python/mypy#2878

@the-knights-who-say-ni
Copy link

Hello, and thanks for your contribution!

I'm a bot set up to make sure that the project can legally accept your contribution by verifying you have signed the PSF contributor agreement (CLA).

Unfortunately we couldn't find an account corresponding to your GitHub username on bugs.python.org (b.p.o) to verify you have signed the CLA. This is necessary for legal reasons before we can look at your contribution. Please follow the steps outlined in the CPython devguide to rectify this issue.

Thanks again to your contribution and we look forward to looking at it!

pep-0484.txt Outdated
p.x # Ok
Node[int]().x # Ok

In addition, ``ClassVar`` cannot be parametrized.
Copy link
Member

Choose a reason for hiding this comment

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

parameterized

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh I thought so too, but I searched the text and found it uses parametrized. Now I searched again, and I found it uses both, 2 times each (not counting my own usage) 😆 I guess I should have checked dictionary instead ...

pep-0484.txt Outdated
@@ -610,6 +611,21 @@ the runtime class of the objects created by instantiating them doesn't
record the distinction. This behavior is called "type erasure"; it is
common practice in languages with generics (e.g. Java, TypeScript).

Class attribute of a generic class can be looked up only through the instance
Copy link
Contributor

Choose a reason for hiding this comment

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

"attributes" is probably what you meant here.

Copy link
Contributor Author

@pkch pkch Apr 7, 2017

Choose a reason for hiding this comment

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

Hmm, I am not sure.

Somehow I interpret instance attribute as referring to the physical object stored on an instance, and of course if there is one, we can't actually refer to the class attribute at all.

I really meant "by using the instance", but if this is confusing, I can try to rephrase.

@pkch pkch force-pushed the generic-class-attr branch from 813fe5c to 2bcdba4 Compare April 7, 2017 19:00
@pkch
Copy link
Contributor Author

pkch commented Apr 11, 2017

I added another clarification: that TypeVar cannot contain type variables (based on the discussion in python/mypy#2756 (comment)). If needed, I can separate it into a separate PR.

@pkch pkch changed the title Clarify generic class attribute access Update generic class attribute and type variable usage Apr 11, 2017
@pkch
Copy link
Contributor Author

pkch commented Apr 11, 2017

The last commit is as discussed in python/mypy#3148 (comment)

@ilevkivskyi
Copy link
Member

@gvanrossum mentioning you just in case (this is not urgent).

Copy link
Member

@gvanrossum gvanrossum left a comment

Choose a reason for hiding this comment

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

LGTM -- @ilevkivskyi if you also approve I can merge this.

pep-0484.txt Outdated
@@ -610,6 +612,24 @@ the runtime class of the objects created by instantiating them doesn't
record the distinction. This behavior is called "type erasure"; it is
common practice in languages with generics (e.g. Java, TypeScript).

Using generic classes (parameterized or not) to access attributes will result
in both type check failure and runtime error. Outside the class definition
Copy link
Member

Choose a reason for hiding this comment

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

and runtime error.

Is it really a runtime error for examples below? I just tried it and all worked fine at runtime.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah right. I misunderstood your comment that it's fixed in typing. I thought you meant it now causes runtime error, but actually what you meant is that it no longer returns different values depending on how it's accessed.

pep-0484.txt Outdated
Node[int]().x # Ok (evaluates to None)
p.x = 1 # Ok, but assigning to instance attribute

In addition, ``ClassVar`` cannot be parameterized.
Copy link
Member

Choose a reason for hiding this comment

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

ClassVar is not a part of PEP 484.

pep-0484.txt Outdated
<type> itself cannot be parametrized by type variables). This means that an
actual type substituted (explicitly or implicitly) for the type variable must
be a subtype of the boundary type. A common example is the definition of a
Comparable type that works well enough to catch the most common errors::
Copy link
Member

Choose a reason for hiding this comment

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

While yo at it, can you please make this

``Comparable``

i.e. use backquotes.

@gvanrossum gvanrossum merged commit 534c911 into python:master May 11, 2017
@gvanrossum
Copy link
Member

Thanks!

@ilevkivskyi
Copy link
Member

@pkch Would you like to also make a small PR updating the PEP 526 and saying that things like ClassVar[T] are prohibited?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants