Update test_positional_only_arg from CPython 3.10.6
@@ -16,11 +16,6 @@ def global_pos_only_and_normal(a, /, b):
def global_pos_only_defaults(a=1, /, b=2):
return a, b
-def global_inner_has_pos_only():
- def f(x: int, /): ...
- return f
-
-
class PositionalOnlyTestCase(unittest.TestCase):
def assertRaisesSyntaxError(self, codestr, regex="invalid syntax"):
@@ -278,16 +273,11 @@ class PositionalOnlyTestCase(unittest.TestCase):
with self.assertRaisesRegex(TypeError, expected):
Example().f(1, b=2)
- def test_mangling(self):
- class X:
- def f(self, *, __a=42):
- return __a
- self.assertEqual(X().f(), 42)
-
def test_module_function(self):
with self.assertRaisesRegex(TypeError, r"f\(\) missing 2 required positional arguments: 'a' and 'b'"):
global_pos_only_f()
+
def test_closures(self):
def f(x,y):
def g(x2,/,y2):
@@ -318,6 +308,29 @@ class PositionalOnlyTestCase(unittest.TestCase):
with self.assertRaisesRegex(TypeError, r"g\(\) takes 2 positional arguments but 3 were given"):
f(1,2)(3,4,5)
+ def test_annotations_in_closures(self):
+
+ def inner_has_pos_only():
+ def f(x: int, /): ...
+ return f
+
+ assert inner_has_pos_only().__annotations__ == {'x': int}
+
+ class Something:
+ def method(self):
+ def f(x: int, /): ...
+ return f
+
+ assert Something().method().__annotations__ == {'x': int}
+
+ def multiple_levels():
+ def inner_has_pos_only():
+ def f(x: int, /): ...
+ return f
+ return inner_has_pos_only()
+
+ assert multiple_levels().__annotations__ == {'x': int}
+
def test_same_keyword_as_positional_with_kwargs(self):
def f(something,/,**kwargs):
return (something, kwargs)
@@ -430,9 +443,6 @@ class PositionalOnlyTestCase(unittest.TestCase):
self.assertEqual(C().method(), sentinel)
- def test_annotations(self):
- assert global_inner_has_pos_only().__annotations__ == {'x': int}
-
# TODO: RUSTPYTHON
@unittest.expectedFailure
def test_annotations_constant_fold(self):
@@ -440,11 +450,11 @@ class PositionalOnlyTestCase(unittest.TestCase):
def f(x: not (int is int), /): ...
# without constant folding we end up with
- # COMPARE_OP(is), UNARY_NOT
- # with constant folding we should expect a COMPARE_OP(is not)
+ # COMPARE_OP(is), IS_OP (0)
+ # with constant folding we should expect a IS_OP (1)
codes = [(i.opname, i.argval) for i in dis.get_instructions(g)]
self.assertNotIn(('UNARY_NOT', None), codes)
- self.assertIn(('COMPARE_OP', 'is not'), codes)
+ self.assertIn(('IS_OP', 1), codes)
if __name__ == "__main__":
