PyDocs

Nav

Development Tools

Type

typing

class · Added in 3.5.2Deprecated 3.9
  • Type(Generic[CT_co])
Deprecated since 3.9. builtins.type now supports subscripting ([]). See PEP 585 and Generic Alias Type.
Deprecated alias to builtins.type.

builtins.type or typing.Type can be used to annotate class objects.
For example, suppose we have the following classes::

    class User: ...  # Abstract base for User classes
    class BasicUser(User): ...
    class ProUser(User): ...
    class TeamUser(User): ...

And a function that takes a class argument that's a subclass of
User and returns an instance of the corresponding class::

    def new_user[U](user_class: type[U]) -> U:
        user = user_class()
        # (Here we could write the user object to a database)
        return user

    joe = new_user(BasicUser)

At this point the type checker knows that joe has type BasicUser.
0 Examples
No examples for typing/Type.
See Also
No see-alsos for typing/Type
0 Notes
No notes for Type