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
Member

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.)

Copy link
Collaborator

@msullivan msullivan left a comment

Choose a reason for hiding this comment

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

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)}
Copy link
Collaborator

Choose a reason for hiding this comment

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

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

Copy link
Member Author

@ilevkivskyi ilevkivskyi Feb 23, 2019

Choose a reason for hiding this comment

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

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

Copy link
Member Author

Choose a reason for hiding this comment

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

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

@ilevkivskyi ilevkivskyi merged commit 11f6e54 into python:master Feb 24, 2019
@ilevkivskyi ilevkivskyi deleted the generic-classmethods branch February 24, 2019 01:17
hauntsaninja added a commit to hauntsaninja/mypy that referenced this pull request Nov 3, 2024
This behaviour was intentionally chosen in python#6418

I agree with Ivan's comment there that it's similar to how mypy allows
instantiation of `type[Abstract]` -- but that's a thing that I want to
explore disallowing.

Let's see primer
hauntsaninja added a commit to hauntsaninja/mypy that referenced this pull request Nov 3, 2024
This behaviour was intentionally chosen in python#6418

I agree with Ivan's comment there that it's similar to how mypy allows
instantiation of `type[Abstract]` -- but that's a thing that I want to
explore disallowing.

Let's see primer
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants