Author: Colin Valliant <[email protected]>
Branch: pep526
Changeset: r93927:250a545b5734
Date: 2018-02-18 10:42 -0800
http://bitbucket.org/pypy/pypy/changeset/250a545b5734/

Log:    Improve annotation tests.

diff --git a/pypy/interpreter/test/test_annotations.py 
b/pypy/interpreter/test/test_annotations.py
--- a/pypy/interpreter/test/test_annotations.py
+++ b/pypy/interpreter/test/test_annotations.py
@@ -1,13 +1,14 @@
-import py
-
 class AppTestAnnotations:
 
-    def test_simple_annotation(self):
+    def test_toplevel_annotation(self):
         # exec because this needs to be in "top level" scope
         # whereas the docstring-based tests are inside a function
         # (or don't care)
         exec("a: int; assert __annotations__['a'] == int")
 
+    def test_toplevel_invalid(self):
+        exec('try: a: invalid\nexcept NameError: pass\n')
+
     def test_non_simple_annotation(self):
         '''
         class C:
@@ -33,8 +34,8 @@
 
     def test_subscript_target(self):
         '''
-        # this test exists to ensure that these type annotations
-        # don't raise exceptions during compilation
+        # ensure that these type annotations don't raise exceptions
+        # during compilation
         class C:
             a = 1
             a[0]: int
@@ -48,10 +49,9 @@
         '''
         class C:
             a: int
-            b: str = "s"
+            b: str
             assert "__annotations__" in locals()
         assert C.__annotations__ == {"a": int, "b": str}
-        assert C.b == "s"
         '''
 
     def test_unevaluated_name(self):
@@ -60,22 +60,27 @@
             def __init__(self):
                 self.x: invalid_name = 1
                 assert self.x == 1
-                try:
-                    # this is invalid because `y` is undefined
-                    # it should raise a NameError
-                    y[0]: also_invalid
-                except NameError:
-                    ...
         C()
         '''
 
+    def test_nonexistent_target(self):
+        '''
+        try:
+            # this is invalid because `y` is undefined
+            # it should raise a NameError
+            y[0]: invalid
+        except NameError:
+            ...
+        '''
+
     def test_repeated_setup(self):
-        '''
+        # each exec will run another SETUP_ANNOTATIONS
+        # we want to confirm that this doesn't blow away
+        # the previous __annotations__
         d = {}
         exec('a: int', d)
         exec('b: int', d)
         exec('assert __annotations__ == {"a": int, "b": int}', d)
-        '''
 
     def test_function_no___annotations__(self):
         '''
@@ -84,25 +89,25 @@
         '''
 
     def test_unboundlocal(self):
-        # this test and the one below it are adapted from PEP 526
+        # a simple variable annotation implies its target is a local
         '''
         a: int
         try:
             print(a)
         except UnboundLocalError:
-            pass
-        except:
-            assert False
+            return
+        assert False
         '''
 
-    def test_nameerror(self):
-        # there's no annotation here, but it's present for contrast with
-        # the test above
+    def test_reassigned___annotations__(self):
         '''
-        try:
-            print(a)
-        except NameError:
-            pass
-        except:
-            raise
+        class C:
+            __annotations__ = None
+            try:
+                a: int
+                raise
+            except TypeError:
+                pass
+            except:
+                assert False
         '''
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to