diff --git a/Doc/library/typing.rst b/Doc/library/typing.rst index 0fee782121b0af..eaf32826804ff6 100644 --- a/Doc/library/typing.rst +++ b/Doc/library/typing.rst @@ -413,6 +413,8 @@ example:: Note that ``type[C]`` is covariant:: class User: ... + + class BasicUser(User): ... class ProUser(User): ... class TeamUser(User): ... @@ -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]`` + 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]`` @@ -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 `.