Skip to content

Commit 345fdce

Browse files
gh-133037: Add test for shadowing __annotate__ (#133084)
1 parent 245cd6c commit 345fdce

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

Lib/test/test_type_annotations.py

+19
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,25 @@ def check_annotations(self, f):
327327
f.__annotations__ = {"z": 43}
328328
self.assertIs(f.__annotate__, None)
329329

330+
def test_user_defined_annotate(self):
331+
class X:
332+
a: int
333+
334+
def __annotate__(format):
335+
return {"a": str}
336+
self.assertEqual(X.__annotate__(annotationlib.Format.VALUE), {"a": str})
337+
self.assertEqual(annotationlib.get_annotations(X), {"a": str})
338+
339+
mod = build_module(
340+
"""
341+
a: int
342+
def __annotate__(format):
343+
return {"a": str}
344+
"""
345+
)
346+
self.assertEqual(mod.__annotate__(annotationlib.Format.VALUE), {"a": str})
347+
self.assertEqual(annotationlib.get_annotations(mod), {"a": str})
348+
330349

331350
class DeferredEvaluationTests(unittest.TestCase):
332351
def test_function(self):

0 commit comments

Comments
 (0)