Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 11 additions & 7 deletions Doc/library/typing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,8 @@ example::
Note that ``type[C]`` is covariant::

class User: ...

class BasicUser(User): ...
class ProUser(User): ...
class TeamUser(User): ...

Expand All @@ -421,8 +423,9 @@ Note that ``type[C]`` is covariant::
return user_class()

make_new_user(User) # OK
make_new_user(ProUser) # Also OK: ``type[ProUser]`` is a subtype of ``type[User]``
make_new_user(TeamUser) # Still fine
make_new_user(BasicUser) # Also OK: ``type[BasicUser]`` is a subtype of ``type[User]``
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This now demonstrates the exact same thing three times. I think we only need one. We can keep one of the three subclasses in this test, and move the remaining two class definitions to the next test.

make_new_user(ProUser) # Still fine
make_new_user(TeamUser) # Also fine
make_new_user(User()) # Error: expected ``type[User]`` but got ``User``
make_new_user(int) # Error: ``type[int]`` is not a subtype of ``type[User]``

Expand All @@ -432,11 +435,12 @@ For example::

def new_non_team_user(user_class: type[BasicUser | ProUser]): ...

new_non_team_user(BasicUser) # OK
new_non_team_user(ProUser) # OK
new_non_team_user(TeamUser) # Error: ``type[TeamUser]`` is not a subtype
# of ``type[BasicUser | ProUser]``
new_non_team_user(User) # Also an error
new_non_team_user(BasicUser) # OK
new_non_team_user(ProUser) # OK

new_non_team_user(TeamUser) # Error: ``type[TeamUser]`` is not a subtype
# of ``type[BasicUser | ProUser]``
new_non_team_user(User) # Also an error

``type[Any]`` is equivalent to :class:`type`, which is the root of Python's
:ref:`metaclass hierarchy <metaclasses>`.
Expand Down
Loading