Skip to content

Commit 0bdf828

Browse files
authored
Improve the backport of NoDefault (#388)
1 parent 12a0f28 commit 0bdf828

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

src/test_typing_extensions.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6439,6 +6439,10 @@ def test_pickling(self):
64396439
loaded = pickle.loads(s)
64406440
self.assertIs(NoDefault, loaded)
64416441

6442+
@skip_if_py313_beta_1
6443+
def test_doc(self):
6444+
self.assertIsInstance(NoDefault.__doc__, str)
6445+
64426446
def test_constructor(self):
64436447
self.assertIs(NoDefault, type(NoDefault)())
64446448
with self.assertRaises(TypeError):
@@ -6455,6 +6459,14 @@ def test_no_call(self):
64556459
def test_immutable(self):
64566460
with self.assertRaises(AttributeError):
64576461
NoDefault.foo = 'bar'
6462+
with self.assertRaises(AttributeError):
6463+
NoDefault.foo
6464+
6465+
# TypeError is consistent with the behavior of NoneType
6466+
with self.assertRaises(TypeError):
6467+
type(NoDefault).foo = 3
6468+
with self.assertRaises(AttributeError):
6469+
type(NoDefault).foo
64586470

64596471

64606472
class TypeVarInferVarianceTests(BaseTestCase):

src/typing_extensions.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1440,7 +1440,16 @@ def TypeAlias(self, parameters):
14401440
if hasattr(typing, "NoDefault"):
14411441
NoDefault = typing.NoDefault
14421442
else:
1443-
class NoDefaultType:
1443+
class NoDefaultTypeMeta(type):
1444+
def __setattr__(cls, attr, value):
1445+
# TypeError is consistent with the behavior of NoneType
1446+
raise TypeError(
1447+
f"cannot set {attr!r} attribute of immutable type {cls.__name__!r}"
1448+
)
1449+
1450+
class NoDefaultType(metaclass=NoDefaultTypeMeta):
1451+
"""The type of the NoDefault singleton."""
1452+
14441453
__slots__ = ()
14451454

14461455
def __new__(cls):
@@ -1453,7 +1462,7 @@ def __reduce__(self):
14531462
return "NoDefault"
14541463

14551464
NoDefault = NoDefaultType()
1456-
del NoDefaultType
1465+
del NoDefaultType, NoDefaultTypeMeta
14571466

14581467

14591468
def _set_default(type_param, default):

0 commit comments

Comments
 (0)