Skip to content

Commit 13d7cf9

Browse files
gh-118895: Call PyType_Ready() on typing.NoDefault (#118897)
1 parent c444362 commit 13d7cf9

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

@@ -10236,15 +10236,34 @@ def test_pickling(self):
1023610236
def test_constructor(self):
1023710237
self.assertIs(NoDefault, type(NoDefault)())
1023810238
with self.assertRaises(TypeError):
10239-
NoDefault(1)
10239+
type(NoDefault)(1)
1024010240

1024110241
def test_repr(self):
1024210242
self.assertEqual(repr(NoDefault), 'typing.NoDefault')
1024310243

10244+
@requires_docstrings
10245+
def test_doc(self):
10246+
self.assertIsInstance(NoDefault.__doc__, str)
10247+
10248+
def test_class(self):
10249+
self.assertIs(NoDefault.__class__, type(NoDefault))
10250+
1024410251
def test_no_call(self):
1024510252
with self.assertRaises(TypeError):
1024610253
NoDefault()
1024710254

10255+
def test_no_attributes(self):
10256+
with self.assertRaises(AttributeError):
10257+
NoDefault.foo = 3
10258+
with self.assertRaises(AttributeError):
10259+
NoDefault.foo
10260+
10261+
# TypeError is consistent with the behavior of NoneType
10262+
with self.assertRaises(TypeError):
10263+
type(NoDefault).foo = 3
10264+
with self.assertRaises(AttributeError):
10265+
type(NoDefault).foo
10266+
1024810267

1024910268
class AllTests(BaseTestCase):
1025010269
"""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)