From 4d4a5578b271a7dae3447921a264575c6dd7db44 Mon Sep 17 00:00:00 2001 From: Jelle Zijlstra Date: Mon, 28 Apr 2025 06:28:20 -0700 Subject: [PATCH] gh-133037: Add test for shadowing __annotate__ --- Lib/test/test_type_annotations.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/Lib/test/test_type_annotations.py b/Lib/test/test_type_annotations.py index b72d3dbe516974..2c886bb6d362fa 100644 --- a/Lib/test/test_type_annotations.py +++ b/Lib/test/test_type_annotations.py @@ -327,6 +327,25 @@ def check_annotations(self, f): f.__annotations__ = {"z": 43} self.assertIs(f.__annotate__, None) + def test_user_defined_annotate(self): + class X: + a: int + + def __annotate__(format): + return {"a": str} + self.assertEqual(X.__annotate__(annotationlib.Format.VALUE), {"a": str}) + self.assertEqual(annotationlib.get_annotations(X), {"a": str}) + + mod = build_module( + """ + a: int + def __annotate__(format): + return {"a": str} + """ + ) + self.assertEqual(mod.__annotate__(annotationlib.Format.VALUE), {"a": str}) + self.assertEqual(annotationlib.get_annotations(mod), {"a": str}) + class DeferredEvaluationTests(unittest.TestCase): def test_function(self):