Skip to content

Commit 6df4913

Browse files
[3.13] gh-118895: Call PyType_Ready() on typing.NoDefault (GH-118897) (#118914)
(cherry picked from commit 13d7cf9) Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
1 parent 0becae3 commit 6df4913

File tree

4 files changed

+27
-2
lines changed

4 files changed

+27
-2
lines changed

Include/internal/pycore_typevarobject.h

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ extern int _Py_initialize_generic(PyInterpreterState *);
1818
extern void _Py_clear_generic_types(PyInterpreterState *);
1919

2020
extern PyTypeObject _PyTypeAlias_Type;
21+
extern PyTypeObject _PyNoDefault_Type;
2122
extern PyObject _Py_NoDefaultStruct;
2223

2324
#ifdef __cplusplus

Lib/test/test_typing.py

+21-2
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
import weakref
4646
import types
4747

48-
from test.support import captured_stderr, cpython_only, infinite_recursion
48+
from test.support import captured_stderr, cpython_only, infinite_recursion, requires_docstrings
4949
from test.typinganndata import ann_module695, mod_generics_cache, _typed_dict_helper
5050

5151

@@ -10247,15 +10247,34 @@ def test_pickling(self):
1024710247
def test_constructor(self):
1024810248
self.assertIs(NoDefault, type(NoDefault)())
1024910249
with self.assertRaises(TypeError):
10250-
NoDefault(1)
10250+
type(NoDefault)(1)
1025110251

1025210252
def test_repr(self):
1025310253
self.assertEqual(repr(NoDefault), 'typing.NoDefault')
1025410254

10255+
@requires_docstrings
10256+
def test_doc(self):
10257+
self.assertIsInstance(NoDefault.__doc__, str)
10258+
10259+
def test_class(self):
10260+
self.assertIs(NoDefault.__class__, type(NoDefault))
10261+
1025510262
def test_no_call(self):
1025610263
with self.assertRaises(TypeError):
1025710264
NoDefault()
1025810265

10266+
def test_no_attributes(self):
10267+
with self.assertRaises(AttributeError):
10268+
NoDefault.foo = 3
10269+
with self.assertRaises(AttributeError):
10270+
NoDefault.foo
10271+
10272+
# TypeError is consistent with the behavior of NoneType
10273+
with self.assertRaises(TypeError):
10274+
type(NoDefault).foo = 3
10275+
with self.assertRaises(AttributeError):
10276+
type(NoDefault).foo
10277+
1025910278

1026010279
class AllTests(BaseTestCase):
1026110280
"""Tests for __all__."""
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Setting attributes on :data:`typing.NoDefault` now raises
2+
:exc:`AttributeError` instead of :exc:`TypeError`.

Modules/_typingmodule.c

+3
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@ _typing_exec(PyObject *m)
6363
if (PyModule_AddObjectRef(m, "TypeAliasType", (PyObject *)&_PyTypeAlias_Type) < 0) {
6464
return -1;
6565
}
66+
if (PyType_Ready(&_PyNoDefault_Type) < 0) {
67+
return -1;
68+
}
6669
if (PyModule_AddObjectRef(m, "NoDefault", (PyObject *)&_Py_NoDefaultStruct) < 0) {
6770
return -1;
6871
}

0 commit comments

Comments
 (0)