Skip to content

Commit

Permalink
pythongh-118895: Call PyType_Ready() on typing.NoDefault
Browse files Browse the repository at this point in the history
  • Loading branch information
JelleZijlstra committed May 10, 2024
1 parent 7ac933e commit 4cb2d40
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 1 deletion.
1 change: 1 addition & 0 deletions Include/internal/pycore_typevarobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ extern int _Py_initialize_generic(PyInterpreterState *);
extern void _Py_clear_generic_types(PyInterpreterState *);

extern PyTypeObject _PyTypeAlias_Type;
extern PyTypeObject _PyNoDefault_Type;
extern PyObject _Py_NoDefaultStruct;

#ifdef __cplusplus
Expand Down
12 changes: 11 additions & 1 deletion Lib/test/test_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -10236,7 +10236,7 @@ def test_pickling(self):
def test_constructor(self):
self.assertIs(NoDefault, type(NoDefault)())
with self.assertRaises(TypeError):
NoDefault(1)
type(NoDefault)(1)

def test_repr(self):
self.assertEqual(repr(NoDefault), 'typing.NoDefault')
Expand All @@ -10245,6 +10245,16 @@ def test_no_call(self):
with self.assertRaises(TypeError):
NoDefault()

def test_no_attributes(self):
with self.assertRaises(AttributeError):
NoDefault.foo = 3
with self.assertRaises(AttributeError):
NoDefault.foo

# TypeError is consistent with the behavior of NoneType
with self.assertRaises(TypeError):
type(NoDefault).foo = 3


class AllTests(BaseTestCase):
"""Tests for __all__."""
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Setting attributes on :data:`typing.NoDefault` now raises
:exc:`AttributeError` instead of :exc:`TypeError`.
3 changes: 3 additions & 0 deletions Modules/_typingmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ _typing_exec(PyObject *m)
if (PyModule_AddObjectRef(m, "TypeAliasType", (PyObject *)&_PyTypeAlias_Type) < 0) {
return -1;
}
if (PyType_Ready(&_PyNoDefault_Type) < 0) {
return -1;
}
if (PyModule_AddObjectRef(m, "NoDefault", (PyObject *)&_Py_NoDefaultStruct) < 0) {
return -1;
}
Expand Down

0 comments on commit 4cb2d40

Please sign in to comment.