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

Comments

Projects
None yet
4 participants
@TRManderson
Contributor

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

This comment has been minimized.

Contributor

TRManderson commented Feb 8, 2017

Somewhat related to #2655

@elazarg

This comment has been minimized.

Contributor

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

This comment has been minimized.

Contributor

elazarg commented Feb 8, 2017

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

@TRManderson

This comment has been minimized.

Contributor

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

This comment has been minimized.

Contributor

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

This comment has been minimized.

Contributor

elazarg commented Feb 8, 2017

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

@gvanrossum

This comment has been minimized.

Member

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

This comment has been minimized.

Contributor

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