From 98b634a7986cfa56dd74fa3cbf7f66299718ee16 Mon Sep 17 00:00:00 2001 From: Jelle Zijlstra Date: Tue, 10 Oct 2023 21:46:08 -0700 Subject: [PATCH] Post-merge cleanup --- Doc/library/stdtypes.rst | 2 +- Doc/library/typing.rst | 3 ++- Lib/test/test_typing.py | 5 +---- Modules/_typingmodule.c | 1 + Objects/unionobject.c | 11 +++++++---- 5 files changed, 12 insertions(+), 10 deletions(-) diff --git a/Doc/library/stdtypes.rst b/Doc/library/stdtypes.rst index ac2cac40ab04ea..017b0e5cc15fd9 100644 --- a/Doc/library/stdtypes.rst +++ b/Doc/library/stdtypes.rst @@ -5224,7 +5224,7 @@ enables cleaner type hinting syntax compared to subscripting :class:`typing.Unio The user-exposed type for the union object can be accessed from :class:`typing.Union` and used for :func:`isinstance` checks:: - >>> import types + >>> import typing >>> isinstance(int | str, typing.Union) True diff --git a/Doc/library/typing.rst b/Doc/library/typing.rst index 79dbad9d938592..822dd8623b5c25 100644 --- a/Doc/library/typing.rst +++ b/Doc/library/typing.rst @@ -27,6 +27,7 @@ This module provides runtime support for type hints. For the original specification of the typing system, see :pep:`484`. For a simplified introduction to type hints, see :pep:`483`. + The function below takes and returns a string and is annotated as follows:: def greeting(name: str) -> str: @@ -1063,7 +1064,7 @@ Special forms These can be used as types in annotations. They all support subscription using ``[]``, but each has a unique syntax. -.. data:: Union +.. class:: Union Union type; ``Union[X, Y]`` is equivalent to ``X | Y`` and means either X or Y. diff --git a/Lib/test/test_typing.py b/Lib/test/test_typing.py index 016285efe6619e..2dcc936cd3301f 100644 --- a/Lib/test/test_typing.py +++ b/Lib/test/test_typing.py @@ -1798,11 +1798,8 @@ def test_cannot_subclass(self): r"type 'typing\.Union' is not an acceptable base type"): class C(Union): pass - with self.assertRaisesRegex(TypeError, CANNOT_SUBCLASS_TYPE): - class D(type(Union)): - pass with self.assertRaisesRegex(TypeError, - r'Cannot subclass typing\.Union\[int, str\]'): + r'Cannot subclass int \| str'): class E(Union[int, str]): pass diff --git a/Modules/_typingmodule.c b/Modules/_typingmodule.c index 92db5a7559d976..0de96969ab0c47 100644 --- a/Modules/_typingmodule.c +++ b/Modules/_typingmodule.c @@ -8,6 +8,7 @@ #include "internal/pycore_interp.h" #include "internal/pycore_typevarobject.h" #include "internal/pycore_unionobject.h" // _PyUnion_Type +#include "pycore_pystate.h" // _PyInterpreterState_GET() #include "clinic/_typingmodule.c.h" /*[clinic input] diff --git a/Objects/unionobject.c b/Objects/unionobject.c index fb970f96959ec9..8794a7027be4b3 100644 --- a/Objects/unionobject.c +++ b/Objects/unionobject.c @@ -340,11 +340,14 @@ union_origin(PyObject *Py_UNUSED(self), void *Py_UNUSED(ignored)) } static PyGetSetDef union_properties[] = { - {"__name__", union_name, NULL, "Name of the type", NULL}, - {"__qualname__", union_name, NULL, "Qualified name of the type", NULL}, - {"__origin__", union_origin, NULL, "Always returns the type", NULL}, + {"__name__", union_name, NULL, + PyDoc_STR("Name of the type"), NULL}, + {"__qualname__", union_name, NULL, + PyDoc_STR("Qualified name of the type"), NULL}, + {"__origin__", union_origin, NULL, + PyDoc_STR("Always returns the type"), NULL}, {"__parameters__", union_parameters, (setter)NULL, - "Type variables in the types.UnionType.", NULL}, + PyDoc_STR("Type variables in the types.UnionType."), NULL}, {0} };