Skip to content

Update the behaviour of the type function for metaclasses #2826

Closed

Description

@TRManderson

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.

Activity

  1. TRManderson commented on Feb 8, 2017

    @TRManderson
    ContributorAuthor

    Somewhat related to #2655

  2. elazarg commented on Feb 8, 2017

    @elazarg
    Contributor

    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?

  3. elazarg commented on Feb 8, 2017

    @elazarg
    Contributor

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

  4. TRManderson commented on Feb 8, 2017

    @TRManderson
    ContributorAuthor
    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?

  5. elazarg commented on Feb 8, 2017

    @elazarg
    Contributor

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

  6. elazarg commented on Feb 8, 2017

    @elazarg
    Contributor

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

  7. gvanrossum commented on Mar 29, 2017

    @gvanrossum
    Member

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

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

  8. elazarg commented on Mar 29, 2017

    @elazarg
    Contributor

    :D

  9. added a commit that references this issue on Mar 29, 2017
    3e8a55f
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions