Skip to content

Commit

Permalink
reformat
Browse files Browse the repository at this point in the history
  • Loading branch information
JelleZijlstra committed Sep 26, 2024
1 parent 2ac6337 commit 92fbfda
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 45 deletions.
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

0 comments on commit 92fbfda

Please sign in to comment.