File: apptest_annotations_string.py

package info (click to toggle)
pypy3 7.3.19%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 212,236 kB
  • sloc: python: 2,098,316; ansic: 540,565; sh: 21,462; asm: 14,419; cpp: 4,451; makefile: 4,209; objc: 761; xml: 530; exp: 499; javascript: 314; pascal: 244; lisp: 45; csh: 12; awk: 4
file content (52 lines) | stat: -rw-r--r-- 1,330 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
from __future__ import annotations

import pytest

a: int
b: unknown

def test_toplevel_annotation():
    assert __annotations__['a'] == "int"
    assert __annotations__['b'] == "unknown"


def test_simple_with_target():
    class C:
        a: int = 1
        assert __annotations__["a"] == "int"
        assert a == 1

def test_class_annotation():
    class C:
        a: list[int]
        b: str
        assert "__annotations__" in locals()
    assert C.__annotations__ == {"a": "list[int]", "b": "str"}

def test_func_annotations():
    def f(a: list) -> a ** 39:
        return 12
    assert f.__annotations__ == {"a": "list", "return": "a ** 39"}

def test_async_return_bug():
    async def foo(x) -> Foobar:
        pass
    assert foo.__annotations__ == {"return": "Foobar"}

def test_future_bug():
    source = """
from __future__ import annotations

class A:
    def __init__(
        self,
        appname: str | None = None,
        appauthor: str | None | Literal[False] = None,
        version: str | None = None,
    ) -> None:
        pass
    """
    code = compile(source, "<dummy>", "exec", dont_inherit=1)
    d = {}
    exec(code, d) # used to crash
    assert d["A"].__init__.__annotations__ == {'appname': 'str | None', 'appauthor': 'str | None | Literal[False]', 'version': 'str | None', 'return': 'None'}