From a51eef4471e01ee60d53ee184bc4feabf6ebd855 Mon Sep 17 00:00:00 2001 From: Damien George Date: Tue, 16 Jun 2020 22:49:38 +1000 Subject: [PATCH] tests/basics: Add tests for variable annotations. --- tests/basics/annotate_var.py | 25 +++++++++++++++++++++++++ tests/basics/annotate_var.py.exp | 5 +++++ tests/run-tests | 1 + 3 files changed, 31 insertions(+) create mode 100644 tests/basics/annotate_var.py create mode 100644 tests/basics/annotate_var.py.exp diff --git a/tests/basics/annotate_var.py b/tests/basics/annotate_var.py new file mode 100644 index 0000000000..3f767e4a73 --- /dev/null +++ b/tests/basics/annotate_var.py @@ -0,0 +1,25 @@ +# test PEP 526, varible annotations + +x: int +print("x" in globals()) + +x: int = 1 +print(x) + +t: tuple = 1, 2 +print(t) + +# a pure annotation in a function makes that variable local +def f(): + x: int + try: + print(x) + except NameError: + print("NameError") +f() + +# here, "x" should remain a global +def f(): + x.y: int + print(x) +f() diff --git a/tests/basics/annotate_var.py.exp b/tests/basics/annotate_var.py.exp new file mode 100644 index 0000000000..9b6536e966 --- /dev/null +++ b/tests/basics/annotate_var.py.exp @@ -0,0 +1,5 @@ +False +1 +(1, 2) +NameError +1 diff --git a/tests/run-tests b/tests/run-tests index 74e2f71ac6..f9e4de4b34 100755 --- a/tests/run-tests +++ b/tests/run-tests @@ -414,6 +414,7 @@ def run_tests(pyb, tests, args, base_path="."): skip_tests.update({'basics/%s.py' % t for t in 'gen_yield_from_close generator_name'.split()}) # require raise_varargs, generator name skip_tests.update({'basics/async_%s.py' % t for t in 'with with2 with_break with_return'.split()}) # require async_with skip_tests.update({'basics/%s.py' % t for t in 'try_reraise try_reraise2'.split()}) # require raise_varargs + skip_tests.add('basics/annotate_var.py') # requires checking for unbound local skip_tests.add('basics/del_deref.py') # requires checking for unbound local skip_tests.add('basics/del_local.py') # requires checking for unbound local skip_tests.add('basics/exception_chain.py') # raise from is not supported