| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import pytest |
| 4 |
|
| 5 |
a: int |
| 6 |
b: unknown |
| 7 |
|
| 8 |
def test_toplevel_annotation(): |
| 9 |
assert __annotations__['a'] == "int" |
| 10 |
assert __annotations__['b'] == "unknown" |
| 11 |
|
| 12 |
|
| 13 |
def test_simple_with_target(): |
| 14 |
class C: |
| 15 |
a: int = 1 |
| 16 |
assert __annotations__["a"] == "int" |
| 17 |
assert a == 1 |
| 18 |
|
| 19 |
def test_class_annotation(): |
| 20 |
class C: |
| 21 |
a: list[int] |
| 22 |
b: str |
| 23 |
assert "__annotations__" in locals() |
| 24 |
assert C.__annotations__ == {"a": "list[int]", "b": "str"} |
| 25 |
|
| 26 |
def test_func_annotations(): |
| 27 |
def f(a: list) -> a ** 39: |
| 28 |
return 12 |
| 29 |
assert f.__annotations__ == {"a": "list", "return": "a ** 39"} |
| 30 |
|
| 31 |
def test_async_return_bug(): |
| 32 |
async def foo(x) -> Foobar: |
| 33 |
pass |
| 34 |
assert foo.__annotations__ == {"return": "Foobar"} |
| 35 |
|
| 36 |
def test_future_bug(): |
| 37 |
source = """ |
| 38 |
from __future__ import annotations |
| 39 |
|
| 40 |
class A: |
| 41 |
def __init__( |
| 42 |
self, |
| 43 |
appname: str | None = None, |
| 44 |
appauthor: str | None | Literal[False] = None, |
| 45 |
version: str | None = None, |
| 46 |
) -> None: |
| 47 |
pass |
| 48 |
""" |
| 49 |
code = compile(source, "<dummy>", "exec", dont_inherit=1) |
| 50 |
d = {} |
| 51 |
exec(code, d) |
| 52 |
assert d["A"].__init__.__annotations__ == {'appname': 'str | None', 'appauthor': 'str | None | Literal[False]', 'version': 'str | None', 'return': 'None'} |