8000 Add test, add to inspect · python/cpython@ce98c19 · GitHub
[go: up one dir, main page]

Skip to content

Commit ce98c19

Browse files
committed
Add test, add to inspect
1 parent 5d182fc commit ce98c19

File tree

3 files changed

+42
-5
lines changed

3 files changed

+42
-5
lines changed

Lib/inspect.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
"AGEN_CREATED",
3939
"AGEN_RUNNING",
4040
"AGEN_SUSPENDED",
41+
"AnnotationsFormat",
4142
"ArgInfo",
4243
"Arguments",
4344
"Attribute",
@@ -61,6 +62,7 @@
6162
"ClassFoundException",
6263
"ClosureVars",
6364
"EndOfBlock",
65+
"FORWARDREF",
6466
"FrameInfo",
6567
"FullArgSpec",
6668
"GEN_CLOSED",
@@ -134,9 +136,11 @@
134136
"istraceback",
135137
"markcoroutinefunction",
136138
"signature",
139+
"SOURCE",
137140
"stack",
138141
"trace",
139142
"unwrap",
143+
"VALUE",
140144
"walktree",
141145
]
142146

@@ -173,6 +177,13 @@
173177
TPFLAGS_IS_ABSTRACT = 1 << 20
174178

175179

180+
@enum.global_enum
181+
class AnnotationsFormat(enum.IntEnum):
182+
VALUE = 1
183+
FORWARDREF = 2
184+
SOURCE = 3
185+
186+
176187
def get_annotations(obj, *, globals=None, locals=None, eval_str=False):
177188
"""Compute the annotations dict for an object.
178189

Lib/test/test_inspect/test_inspect.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1592,6 +1592,11 @@ class C(metaclass=M):
15921592
attrs = [a[0] for a in inspect.getmembers(C)]
15931593
self.assertNotIn('missing', attrs)
15941594

1595+
def test_annotation_format(self):
1596+
self.assertIs(inspect.VALUE, inspect.AnnotationsFormat.VALUE)
1597+
self.assertEqual(inspect.VALUE.value, 1)
1598+
self.assertEqual(inspect.VALUE, 1)
1599+
15951600
def test_get_annotations_with_stock_annotations(self):
15961601
def foo(a:int, b:str): pass
15971602
self.assertEqual(inspect.get_annotations(foo), {'a': int, 'b': str})

Lib/test/test_type_annotations.py

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -108,20 +108,24 @@ class D(metaclass=C):
108108
self.assertEqual(D.__annotations__, {})
109109

110110

111+
def build_module(code: str, name: str = "top") -> types.ModuleType:
112+
ns = run_code(code)
113+
mod = types.ModuleType(name)
114+
mod.__dict__.update(ns)
115+
return mod
116+
117+
111118
class TestSetupAnnotations(unittest.TestCase):
112119
def check(self, code: str):
113120
code = textwrap.dedent(code)
114121
for scope in ("module", "class"):
115122
with self.subTest(scope=scope):
116123
if scope == "class":
117124
code = f"class C:\n{textwrap.indent(code, ' ')}"
118-
ns = run_code(code)
119-
if scope == "class":
125+
ns = run_code(code)
120126
annotations = ns["C"].__annotations__
121127
else:
122-
mod = types.ModuleType("mod")
123-
mod.__dict__.update(ns)
124-
annotations = mod.__annotations__
128+
annotations = build_module(code).__annotations__
125129
self.assertEqual(annotations, {"x": int})
126130

127131
def test_top_level(self):
@@ -333,3 +337,20 @@ def test_no_exotic_expressions(self):
333337
check_syntax_error(self, "def func(x: (yield from x)): ...", "yield expression cannot be used within an annotation")
334338
check_syntax_error(self, "def func(x: (y := 3)): ...", "named expression cannot be used within an annotation")
335339
check_syntax_error(self, "def func(x: (await 42)): ...", "await expression cannot be used within an annotation")
340+
341+
def test_generated_annotate(self):
342+
def func(x: int):
343+
pass
344+
class X:
345+
x: int
346+
mod = build_module("x: int")
347+
for obj in (func, X, mod):
348+
with self.subTest(obj=obj):
349+
annotate = obj.__annotate__
350+
self.assertIsInstance(annotate, types.FunctionType)
351+
self.assertEqual(annotate.__name__, f"<annotations of {obj.__name__}>")
352+
with self.assertRaises(AssertionError): # TODO NotImplementedError
353+
annotate(2)
354+
with self.assertRaises(AssertionError): # TODO NotImplementedError
355+
annotate(None)
356+
self.assertEqual(annotate(1), {"x": int})

0 commit comments

Comments
 (0)
0