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

Update the behaviour of the type function for metaclasses #2826

Closed
TRManderson opened this issue Feb 8, 2017 · 8 comments
Closed

Update the behaviour of the type function for metaclasses #2826

TRManderson opened this issue Feb 8, 2017 · 8 comments

Comments

@TRManderson
Copy link
Contributor

@TRManderson TRManderson commented Feb 8, 2017

Calling type on a class A yields Type[A], even if A has a metaclass other than type. This leads to incorrectly typechecking interactions with type(A) when when attempting to use metaclass attributes.

@TRManderson
Copy link
Contributor Author

@TRManderson TRManderson commented Feb 8, 2017

Somewhat related to #2655

@elazarg
Copy link
Contributor

@elazarg elazarg commented Feb 8, 2017

Can you give a concrete example?
The interaction with other features might be interesting. Maybe it should work something like this

class M(Generic(T), Type[T]): pass  # maybe M(type) is a shorthand for this

T1 = TypeVar('T1', bound='A')

class A(metaclass=M):
    @classmethod
    def foo(cls: M[T1]) -> T1:
        return cls()

reveal_type(A)  # E: Revealed type is M[A]

class B(A): pass

reveal_type(B)  # E: Revealed type is M[B]
reveal_type(B.foo()) # E: Revealed type is B

All of this should be how things work right now, except that M[B] is written Type[B]. Where does this fail?

@elazarg
Copy link
Contributor

@elazarg elazarg commented Feb 8, 2017

I agree it should be possible to write M[T] for "a type whose metaclass is M".

@TRManderson
Copy link
Contributor Author

@TRManderson TRManderson commented Feb 8, 2017

class Meta(type):
    x = 1

class A(metaclass=Meta): pass

print(type(A).x) # test2826.py:6: error: Type[] has no attribute "x"

type(A) is Meta which definitely has an attribute x. Do I need to make Meta subclass Type[T] for it to work?

@elazarg
Copy link
Contributor

@elazarg elazarg commented Feb 8, 2017

Thanks.

The culprit in my example seems to be that the type variable T is quantified over type constructors (TypeInfo) and not over types. Perhaps there is a workaround.
(Just thinking out loud).

@elazarg
Copy link
Contributor

@elazarg elazarg commented Feb 8, 2017

I have a fix for this particular issue, but the WiFi here is not working.

@gvanrossum
Copy link
Member

@gvanrossum gvanrossum commented Mar 29, 2017

..., but the WiFi here is not working.

Sounds like an updated version of Fermat's claim about his last theorem. :-)

@elazarg
Copy link
Contributor

@elazarg elazarg commented Mar 29, 2017

:D

gvanrossum added a commit that referenced this issue Mar 29, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

4 participants