Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-119180: Rename SOURCE format to STRING #124620

Merged
merged 2 commits into from
Sep 26, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
reformat
  • Loading branch information
JelleZijlstra committed Sep 26, 2024
commit 92fbfda66fa95137556752f5ab0e421adba49d3f
18 changes: 15 additions & 3 deletions Lib/annotationlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,9 +291,21 @@ def __convert_to_ast(self, other):
return other.__ast_node__
elif isinstance(other, slice):
return ast.Slice(
lower=self.__convert_to_ast(other.start) if other.start is not None else None,
upper=self.__convert_to_ast(other.stop) if other.stop is not None else None,
step=self.__convert_to_ast(other.step) if other.step is not None else None,
lower=(
self.__convert_to_ast(other.start)
if other.start is not None
else None
),
upper=(
self.__convert_to_ast(other.stop)
if other.stop is not None
else None
),
step=(
self.__convert_to_ast(other.step)
if other.step is not None
else None
),
)
else:
return ast.Constant(value=other)
Expand Down
57 changes: 15 additions & 42 deletions Lib/test/test_annotationlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,7 @@ def test_closure(self):
def inner(arg: x):
pass

anno = annotationlib.get_annotations(
inner, format=Format.FORWARDREF
)
anno = annotationlib.get_annotations(inner, format=Format.FORWARDREF)
fwdref = anno["arg"]
self.assertIsInstance(fwdref, annotationlib.ForwardRef)
self.assertEqual(fwdref.__forward_arg__, "x")
Expand All @@ -66,9 +64,7 @@ def inner(arg: x):
x = 1
self.assertEqual(fwdref.evaluate(), x)

anno = annotationlib.get_annotations(
inner, format=Format.FORWARDREF
)
anno = annotationlib.get_annotations(inner, format=Format.FORWARDREF)
self.assertEqual(anno["arg"], x)

def test_function(self):
Expand Down Expand Up @@ -276,7 +272,7 @@ class Gen[T]:
with self.assertRaises(NameError):
ForwardRef("T").evaluate(owner=int)

T, = Gen.__type_params__
(T,) = Gen.__type_params__
self.assertIs(ForwardRef("T").evaluate(type_params=Gen.__type_params__), T)
self.assertIs(ForwardRef("T").evaluate(owner=Gen), T)

Expand All @@ -294,8 +290,7 @@ class Gen[T]:
def test_fwdref_with_module(self):
self.assertIs(ForwardRef("Format", module="annotationlib").evaluate(), Format)
self.assertIs(
ForwardRef("Counter", module="collections").evaluate(),
collections.Counter
ForwardRef("Counter", module="collections").evaluate(), collections.Counter
)
self.assertEqual(
ForwardRef("Counter[int]", module="collections").evaluate(),
Expand Down Expand Up @@ -392,9 +387,7 @@ class C1(metaclass=NoDict):
)
self.assertEqual(annotationlib.get_annotations(NoDict), {"b": str})
self.assertEqual(
annotationlib.get_annotations(
NoDict, format=Format.FORWARDREF
),
annotationlib.get_annotations(NoDict, format=Format.FORWARDREF),
{"b": str},
)
self.assertEqual(
Expand Down Expand Up @@ -446,12 +439,8 @@ def foo():
pass

with self.assertRaises(ValueError):
annotationlib.get_annotations(
foo, format=Format.FORWARDREF, eval_str=True
)
annotationlib.get_annotations(
foo, format=Format.STRING, eval_str=True
)
annotationlib.get_annotations(foo, format=Format.FORWARDREF, eval_str=True)
annotationlib.get_annotations(foo, format=Format.STRING, eval_str=True)

def test_stock_annotations(self):
def foo(a: int, b: str):
Expand Down Expand Up @@ -567,39 +556,27 @@ def test_stock_annotations_in_module(self):
{"a": "int", "b": "str"},
)
self.assertEqual(
annotationlib.get_annotations(
isa.MyClass, format=Format.STRING
),
annotationlib.get_annotations(isa.MyClass, format=Format.STRING),
{"a": "int", "b": "str"},
)
self.assertEqual(
annotationlib.get_annotations(
isa.function, format=Format.STRING
),
annotationlib.get_annotations(isa.function, format=Format.STRING),
{"a": "int", "b": "str", "return": "MyClass"},
)
self.assertEqual(
annotationlib.get_annotations(
isa.function2, format=Format.STRING
),
annotationlib.get_annotations(isa.function2, format=Format.STRING),
{"a": "int", "b": "str", "c": "MyClass", "return": "MyClass"},
)
self.assertEqual(
annotationlib.get_annotations(
isa.function3, format=Format.STRING
),
annotationlib.get_annotations(isa.function3, format=Format.STRING),
{"a": "int", "b": "str", "c": "MyClass"},
)
self.assertEqual(
annotationlib.get_annotations(
annotationlib, format=Format.STRING
),
annotationlib.get_annotations(annotationlib, format=Format.STRING),
{},
)
self.assertEqual(
annotationlib.get_annotations(
isa.UnannotatedClass, format=Format.STRING
),
annotationlib.get_annotations(isa.UnannotatedClass, format=Format.STRING),
{},
)
self.assertEqual(
Expand All @@ -620,9 +597,7 @@ def test_stock_annotations_on_wrapper(self):
{"a": int, "b": str, "return": isa.MyClass},
)
self.assertEqual(
annotationlib.get_annotations(
wrapped, format=Format.FORWARDREF
),
annotationlib.get_annotations(wrapped, format=Format.FORWARDREF),
{"a": int, "b": str, "return": isa.MyClass},
)
self.assertEqual(
Expand Down Expand Up @@ -976,9 +951,7 @@ def evaluate(format, exc=NotImplementedError):
with self.assertRaises(NameError):
annotationlib.call_evaluate_function(evaluate, Format.VALUE)
self.assertEqual(
annotationlib.call_evaluate_function(
evaluate, Format.FORWARDREF
),
annotationlib.call_evaluate_function(evaluate, Format.FORWARDREF),
annotationlib.ForwardRef("undefined"),
)
self.assertEqual(
Expand Down
Loading