Skip to content

Document and expose GenericMeta #335

Closed

Description

@gvanrossum

In python/typeshed#703 a reasonable request was made to expose GenericMeta. While that PR just adds it to the stub for typing.py, we should think about and carefully document what behavior subclasses of GenericMeta can depend on.

In particular, I don't think it should be allowed to write your own version of Generic using GenericMeta as a metaclass -- type checkers aren't expected to understand that. But overriding __new__ and invoking super().__new__(...) should be allowed. I'm not sure about the other methods -- e.g. can a subclass of GenericMeta override __getitem__, __copy__ or __isinstancecheck__? (Assuming they use super().)

@ilevkivskyi Thoughts?

Activity

  1. ilevkivskyi commented on Nov 29, 2016

    @ilevkivskyi
    Member

    I think this is a good idea. I would prefer to only document __new__ as public. For example __getitem__ could be changed if we decide to allow user defined variadic generics, __instancecheck__ behavior may change (e.g. http://bugs.python.org/issue28339), etc.

    Probably we should also discuss whether we need to document all keyword arguments for GenericMeta.__new__. There are tvars, args, origin, extra, and orig_bases. I think we could say something about first three (they correspond to __parameters__, __args__, and __origin__ and these are used by most things in typing).

  2. ilevkivskyi commented on Nov 29, 2016

    @ilevkivskyi
    Member

    Concerning the behavior of GenericMeta.__new__ with respect to bases we should probably document the following points:

    • no plain Generic in bases
    • Generic[...] should appear in bases only once
    • if Generic[...] is present, then it should list all type variables that appear in other bases
    • type erasure: all bases are stripped to furthest __origin__

    Some of these points are already discussed in PEP 484, but I think it would be reasonable to also mention them in documentation for typing since they are enforced by the runtime implementation.

  3. gvanrossum commented on Nov 29, 2016

    @gvanrossum
    MemberAuthor
  4. added a commit that references this issue on Jan 18, 2017
    093ad88
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions