Skip to content
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

Fix member access on generic classes #6418

Merged
merged 5 commits into from Feb 24, 2019

Conversation

@ilevkivskyi
Copy link
Collaborator

commented Feb 16, 2019

Fixes #3645
Fixes #1337
Fixes #5664

The fix is straightforward, I just add/propagate the bound type variable values by mapping to supertype.

I didn't find any corner cases with class methods, and essentially follow the same logic as when we generate the callable from __init__ for generic classes in calls like C() or C[int]().

For class attributes there are two things I fixed. First we used to prohibit ambiguous access:

class C(Generic[T]):
    x: T
C.x  # Error!
C[int].x  # Error!

but the type variables were leaking after an error, now they are erased to Any. Second, I now make an exception and allow accessing attributes on Type[C], this is very similar to how we allow instantiation of Type[C] even if it is abstract (because we expect concrete subclasses there), plus this allows accessing variables on cls (first argument in class methods), for example:

class C(Generic[T]):
    x: T
    def get(cls) -> T:
        return cls.x  # OK

(I also added a bunch of more detailed comments in this part of code.)

@ilevkivskyi ilevkivskyi requested review from msullivan and JukkaL Feb 16, 2019

@msullivan
Copy link
Collaborator

left a comment

This seems good to me. One additional comment request

if is_classmethod:
assert isuper is not None
t = expand_type_by_instance(t, isuper)
ids = {t.id for t in itype.args if isinstance(t, TypeVarType)}

This comment has been minimized.

Copy link
@msullivan

msullivan Feb 22, 2019

Collaborator

Could you add some comments about what is going on here?

This comment has been minimized.

Copy link
@ilevkivskyi

ilevkivskyi Feb 23, 2019

Author Collaborator

I added a detailed comment (and even found a little bug while writing it).

This comment has been minimized.

Copy link
@ilevkivskyi

ilevkivskyi Feb 24, 2019

Author Collaborator

I double-checked that everything is OK with internal code bases.

@ilevkivskyi ilevkivskyi merged commit 11f6e54 into python:master Feb 24, 2019

2 checks passed

continuous-integration/appveyor/pr AppVeyor build succeeded
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details

@ilevkivskyi ilevkivskyi deleted the ilevkivskyi:generic-classmethods branch Feb 24, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.