summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_dataclasses.py
diff options
context:
space:
mode:
authorEric V. Smith <ericvsmith@users.noreply.github.com>2017-12-29 18:59:58 (GMT)
committerGitHub <noreply@github.com>2017-12-29 18:59:58 (GMT)
commit03220fdb26c0b6a50ce5ed1fdfbf232094b66db6 (patch)
treebacf97b6e793c4af6f9f5a43ac58edc7281df23f /Lib/test/test_dataclasses.py
parente325608740bee161ca7fefd09463d63099efa1b8 (diff)
downloadcpython-03220fdb26c0b6a50ce5ed1fdfbf232094b66db6.zip
cpython-03220fdb26c0b6a50ce5ed1fdfbf232094b66db6.tar.gz
cpython-03220fdb26c0b6a50ce5ed1fdfbf232094b66db6.tar.bz2
bpo-32427: Expose dataclasses.MISSING object. (#5045)
Diffstat (limited to 'Lib/test/test_dataclasses.py')
-rwxr-xr-xLib/test/test_dataclasses.py52
1 files changed, 49 insertions, 3 deletions
diff --git a/Lib/test/test_dataclasses.py b/Lib/test/test_dataclasses.py
index 7fbea76..ed69563 100755
--- a/Lib/test/test_dataclasses.py
+++ b/Lib/test/test_dataclasses.py
@@ -1,6 +1,6 @@
from dataclasses import (
dataclass, field, FrozenInstanceError, fields, asdict, astuple,
- make_dataclass, replace, InitVar, Field
+ make_dataclass, replace, InitVar, Field, MISSING
)
import pickle
@@ -917,12 +917,12 @@ class TestCase(unittest.TestCase):
param = next(params)
self.assertEqual(param.name, 'k')
self.assertIs (param.annotation, F)
- # Don't test for the default, since it's set to _MISSING
+ # Don't test for the default, since it's set to MISSING
self.assertEqual(param.kind, inspect.Parameter.POSITIONAL_OR_KEYWORD)
param = next(params)
self.assertEqual(param.name, 'l')
self.assertIs (param.annotation, float)
- # Don't test for the default, since it's set to _MISSING
+ # Don't test for the default, since it's set to MISSING
self.assertEqual(param.kind, inspect.Parameter.POSITIONAL_OR_KEYWORD)
self.assertRaises(StopIteration, next, params)
@@ -948,6 +948,52 @@ class TestCase(unittest.TestCase):
validate_class(C)
+ def test_missing_default(self):
+ # Test that MISSING works the same as a default not being
+ # specified.
+ @dataclass
+ class C:
+ x: int=field(default=MISSING)
+ with self.assertRaisesRegex(TypeError,
+ r'__init__\(\) missing 1 required '
+ 'positional argument'):
+ C()
+ self.assertNotIn('x', C.__dict__)
+
+ @dataclass
+ class D:
+ x: int
+ with self.assertRaisesRegex(TypeError,
+ r'__init__\(\) missing 1 required '
+ 'positional argument'):
+ D()
+ self.assertNotIn('x', D.__dict__)
+
+ def test_missing_default_factory(self):
+ # Test that MISSING works the same as a default factory not
+ # being specified (which is really the same as a default not
+ # being specified, too).
+ @dataclass
+ class C:
+ x: int=field(default_factory=MISSING)
+ with self.assertRaisesRegex(TypeError,
+ r'__init__\(\) missing 1 required '
+ 'positional argument'):
+ C()
+ self.assertNotIn('x', C.__dict__)
+
+ @dataclass
+ class D:
+ x: int=field(default=MISSING, default_factory=MISSING)
+ with self.assertRaisesRegex(TypeError,
+ r'__init__\(\) missing 1 required '
+ 'positional argument'):
+ D()
+ self.assertNotIn('x', D.__dict__)
+
+ def test_missing_repr(self):
+ self.assertIn('MISSING_TYPE object', repr(MISSING))
+
def test_dont_include_other_annotations(self):
@dataclass
class C: