From e7bda02f7b0b7de903600e1ddafac30a2c370e84 Mon Sep 17 00:00:00 2001 From: Jelle Zijlstra Date: Sat, 27 Jul 2024 12:11:30 -0700 Subject: [PATCH 1/2] gh-119180: annotationlib: Fix __all__, reformat, other minor fixes --- .pre-commit-config.yaml | 4 +- Lib/annotationlib.py | 30 +++++++++++---- Lib/inspect.py | 4 +- Lib/test/test_annotationlib.py | 67 ++++++++++++++++++++++------------ Lib/typing.py | 2 +- 5 files changed, 71 insertions(+), 36 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index b10be5b6bd9904..c2122a5fd15312 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -23,8 +23,8 @@ repos: rev: 24.4.2 hooks: - id: black - name: Run Black on Tools/jit/ - files: ^Tools/jit/ + name: Run Black on Tools/jit/ and annotationlib + files: ^(Tools/jit/|Lib/annotationlib\.py|Lib/test/test_annotationlib\.py) language_version: python3.12 - repo: https://github.com/pre-commit/pre-commit-hooks diff --git a/Lib/annotationlib.py b/Lib/annotationlib.py index 141e31bbf910e3..781b5fa7cb4f06 100644 --- a/Lib/annotationlib.py +++ b/Lib/annotationlib.py @@ -6,7 +6,14 @@ import sys import types -__all__ = ["Format", "ForwardRef", "call_annotate_function", "get_annotations"] +__all__ = [ + "Format", + "ForwardRef", + "call_annotate_function", + "call_evaluate_function", + "get_annotate_function", + "get_annotations", +] class Format(enum.IntEnum): @@ -421,8 +428,7 @@ def call_evaluate_function(evaluate, format, *, owner=None): return call_annotate_function(evaluate, format, owner=owner, _is_evaluate=True) -def call_annotate_function(annotate, format, *, owner=None, - _is_evaluate=False): +def call_annotate_function(annotate, format, *, owner=None, _is_evaluate=False): """Call an __annotate__ function. __annotate__ functions are normally generated by the compiler to defer the evaluation of annotations. They can be called with any of the format arguments in the Format enum, but @@ -468,8 +474,13 @@ def call_annotate_function(annotate, format, *, owner=None, closure = tuple(new_closure) else: closure = None - func = types.FunctionType(annotate.__code__, globals, closure=closure, - argdefs=annotate.__defaults__, kwdefaults=annotate.__kwdefaults__) + func = types.FunctionType( + annotate.__code__, + globals, + closure=closure, + argdefs=annotate.__defaults__, + kwdefaults=annotate.__kwdefaults__, + ) annos = func(Format.VALUE) if _is_evaluate: return annos if isinstance(annos, str) else repr(annos) @@ -523,8 +534,13 @@ def call_annotate_function(annotate, format, *, owner=None, closure = tuple(new_closure) else: closure = None - func = types.FunctionType(annotate.__code__, globals, closure=closure, - argdefs=annotate.__defaults__, kwdefaults=annotate.__kwdefaults__) + func = types.FunctionType( + annotate.__code__, + globals, + closure=closure, + argdefs=annotate.__defaults__, + kwdefaults=annotate.__kwdefaults__, + ) result = func(Format.VALUE) for obj in globals.stringifiers: obj.__class__ = ForwardRef diff --git a/Lib/inspect.py b/Lib/inspect.py index ba3ecbb87c7026..547256daa2ae9b 100644 --- a/Lib/inspect.py +++ b/Lib/inspect.py @@ -24,8 +24,6 @@ stack(), trace() - get info about frames on the stack or in a traceback signature() - get a Signature object for the callable - - get_annotations() - safely compute an object's annotations """ # This module is in the public domain. No warranties. @@ -142,7 +140,7 @@ import abc -from annotationlib import get_annotations +from annotationlib import get_annotations # re-exported import ast import dis import collections.abc diff --git a/Lib/test/test_annotationlib.py b/Lib/test/test_annotationlib.py index e4dcdb6b58d009..e885c2a5287f69 100644 --- a/Lib/test/test_annotationlib.py +++ b/Lib/test/test_annotationlib.py @@ -8,6 +8,7 @@ from annotationlib import Format, get_annotations, get_annotate_function from typing import Unpack +from test import support from test.test_inspect import inspect_stock_annotations from test.test_inspect import inspect_stringized_annotations from test.test_inspect import inspect_stringized_annotations_2 @@ -287,7 +288,9 @@ class C1(metaclass=NoDict): ) self.assertEqual(annotationlib.get_annotations(NoDict), {"b": str}) self.assertEqual( - annotationlib.get_annotations(NoDict, format=annotationlib.Format.FORWARDREF), + annotationlib.get_annotations( + NoDict, format=annotationlib.Format.FORWARDREF + ), {"b": str}, ) self.assertEqual( @@ -675,12 +678,13 @@ def test_pep695_generic_class_with_future_annotations_and_local_shadowing(self): ) self.assertEqual(B_annotations, {"x": int, "y": str, "z": bytes}) - def test_pep695_generic_class_with_future_annotations_name_clash_with_global_vars(self): + def test_pep695_generic_class_with_future_annotations_name_clash_with_global_vars( + self, + ): ann_module695 = inspect_stringized_annotations_pep695 C_annotations = annotationlib.get_annotations(ann_module695.C, eval_str=True) self.assertEqual( - set(C_annotations.values()), - set(ann_module695.C.__type_params__) + set(C_annotations.values()), set(ann_module695.C.__type_params__) ) def test_pep_695_generic_function_with_future_annotations(self): @@ -697,17 +701,19 @@ def test_pep_695_generic_function_with_future_annotations(self): self.assertIs(generic_func_annotations["z"].__origin__, func_t_params[2]) self.assertIs(generic_func_annotations["zz"].__origin__, func_t_params[2]) - def test_pep_695_generic_function_with_future_annotations_name_clash_with_global_vars(self): + def test_pep_695_generic_function_with_future_annotations_name_clash_with_global_vars( + self, + ): self.assertEqual( set( annotationlib.get_annotations( inspect_stringized_annotations_pep695.generic_function_2, - eval_str=True + eval_str=True, ).values() ), set( inspect_stringized_annotations_pep695.generic_function_2.__type_params__ - ) + ), ) def test_pep_695_generic_method_with_future_annotations(self): @@ -721,23 +727,27 @@ def test_pep_695_generic_method_with_future_annotations(self): } self.assertEqual( generic_method_annotations, - {"x": params["Foo"], "y": params["Bar"], "return": None} + {"x": params["Foo"], "y": params["Bar"], "return": None}, ) - def test_pep_695_generic_method_with_future_annotations_name_clash_with_global_vars(self): + def test_pep_695_generic_method_with_future_annotations_name_clash_with_global_vars( + self, + ): self.assertEqual( set( annotationlib.get_annotations( inspect_stringized_annotations_pep695.D.generic_method_2, - eval_str=True + eval_str=True, ).values() ), set( inspect_stringized_annotations_pep695.D.generic_method_2.__type_params__ - ) + ), ) - def test_pep_695_generic_method_with_future_annotations_name_clash_with_global_and_local_vars(self): + def test_pep_695_generic_method_with_future_annotations_name_clash_with_global_and_local_vars( + self, + ): self.assertEqual( annotationlib.get_annotations( inspect_stringized_annotations_pep695.E, eval_str=True @@ -749,20 +759,20 @@ def test_pep_695_generics_with_future_annotations_nested_in_function(self): results = inspect_stringized_annotations_pep695.nested() self.assertEqual( - set(results.F_annotations.values()), - set(results.F.__type_params__) + set(results.F_annotations.values()), set(results.F.__type_params__) ) self.assertEqual( set(results.F_meth_annotations.values()), - set(results.F.generic_method.__type_params__) + set(results.F.generic_method.__type_params__), ) self.assertNotEqual( - set(results.F_meth_annotations.values()), - set(results.F.__type_params__) + set(results.F_meth_annotations.values()), set(results.F.__type_params__) ) self.assertEqual( - set(results.F_meth_annotations.values()).intersection(results.F.__type_params__), - set() + set(results.F_meth_annotations.values()).intersection( + results.F.__type_params__ + ), + set(), ) self.assertEqual(results.G_annotations, {"x": str}) @@ -783,7 +793,9 @@ def evaluate(format, exc=NotImplementedError): with self.assertRaises(NameError): annotationlib.call_evaluate_function(evaluate, annotationlib.Format.VALUE) self.assertEqual( - annotationlib.call_evaluate_function(evaluate, annotationlib.Format.FORWARDREF), + annotationlib.call_evaluate_function( + evaluate, annotationlib.Format.FORWARDREF + ), annotationlib.ForwardRef("undefined"), ) self.assertEqual( @@ -813,12 +825,14 @@ class Y(metaclass=Meta): self.assertEqual(get_annotate_function(Y)(Format.VALUE), {"b": float}) def test_unannotated_meta(self): - class Meta(type): pass + class Meta(type): + pass class X(metaclass=Meta): a: str - class Y(X): pass + class Y(X): + pass self.assertEqual(get_annotations(Meta), {}) self.assertIs(get_annotate_function(Meta), None) @@ -867,6 +881,13 @@ class D(metaclass=Meta): self.assertEqual(get_annotations(c), c.expected_annotations) annotate_func = get_annotate_function(c) if c.expected_annotations: - self.assertEqual(annotate_func(Format.VALUE), c.expected_annotations) + self.assertEqual( + annotate_func(Format.VALUE), c.expected_annotations + ) else: self.assertIs(annotate_func, None) + + +class TestAnnotationLib(unittest.TestCase): + def test__all__(self): + support.check__all__(self, annotationlib) diff --git a/Lib/typing.py b/Lib/typing.py index 626053d8166160..39a14ae6f83c28 100644 --- a/Lib/typing.py +++ b/Lib/typing.py @@ -1054,7 +1054,7 @@ def evaluate_forward_ref( evaluating the forward reference. This parameter must be provided (though it may be an empty tuple) if *owner* is not given and the forward reference does not already have an owner set. *format* specifies the format of the - annotation and is a member of the annoations.Format enum. + annotation and is a member of the annotationlib.Format enum. """ if type_params is _sentinel: From e9f97143e801e74866530bf3f96d497b9c8e108c Mon Sep 17 00:00:00 2001 From: Jelle Zijlstra Date: Sun, 11 Aug 2024 16:20:02 -0700 Subject: [PATCH 2/2] Back out Black --- .pre-commit-config.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index c2122a5fd15312..b10be5b6bd9904 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -23,8 +23,8 @@ repos: rev: 24.4.2 hooks: - id: black - name: Run Black on Tools/jit/ and annotationlib - files: ^(Tools/jit/|Lib/annotationlib\.py|Lib/test/test_annotationlib\.py) + name: Run Black on Tools/jit/ + files: ^Tools/jit/ language_version: python3.12 - repo: https://github.com/pre-commit/pre-commit-hooks