Skip to content

[waiting for author] Map class type parameters to superclass when checking class method use #1768

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

Closed
wants to merge 2 commits into from

Conversation

rwbarton
Copy link
Contributor

Fixes #1337.


class A(Generic[T]):
@classmethod
def f(cls, x: T) -> T:
Copy link
Collaborator

Choose a reason for hiding this comment

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

What's the type of cls here?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Ah, the above question probably isn't relevant 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.

It's apparently the full type of A, namely def [T] () -> sm3.A[T1]. That Tis not the same as theTin this method, apparently:a = cls() # type: A[int]` is accepted here. This is maybe a little weird, but not changed by this commit...

@JukkaL
Copy link
Collaborator

JukkaL commented Jun 29, 2016

Hmm I'd like to see a more complete (approximation of) real-world example where this is being used, and I'd like it to type check. Here is a naive try (haven't tried this yet):

from typing import TypeVar, Generic

T = TypeVar('T')

class Maker(Generic[T]):
    @classmethod
    def make(cls) -> T:   
        # this has to be abstract, as T varies totally freely here. but i think that
        # abstract class methods aren't supported. this is close enough.
        raise NotImplementedError

class B(Maker['B']):
    @classmethod
    def make(cls) -> B:
        return B()  # ok

class C(Maker['C']):
    @classmethod
    def make(cls) -> int:   # should be an error
        return 1

def f(o: Maker[T]) -> T:
    return o.make()  # should be okay?

b = f(B())  # type: B  # okay

def g(t: Type[Maker[T]]) -> T:   # should this work?
    return t.make()

bb = g(B)  # type: B  # okay

@gvanrossum gvanrossum changed the title Map class type parameters to superclass when checking class method use [waiting for author] Map class type parameters to superclass when checking class method use Oct 4, 2016
@gvanrossum
Copy link
Member

@rwbarton Do you have time to address Jukka's suggestion above?

@gvanrossum
Copy link
Member

gvanrossum commented Nov 9, 2016

Hey @rwbarton,

It looks like this PR is pretty much stuck. I'm trying to clean up stuck PRs so that the list of PRs is not too long. If you feel like working on this some more, just go ahead and reopen it, or open a new PR -- the corresponding issue will remain open with a reference to this (closed) PR in it, so if someone else feels like tackling this problem they still have this PR as a starting point.

@gvanrossum gvanrossum closed this Nov 9, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants