forked from cython/cython
/
pyclass_annotations_pep526.py
47 lines (41 loc) · 1.12 KB
/
pyclass_annotations_pep526.py
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
# cython: language_level=3
# mode: run
# tag: pure3.7, pep526, pep484
from __future__ import annotations
try:
from typing import ClassVar
except ImportError: # Py<=3.5
ClassVar = {int: int}
class PyAnnotatedClass:
"""
>>> PyAnnotatedClass.__annotations__["CLASS_VAR"]
'ClassVar[int]'
>>> PyAnnotatedClass.__annotations__["obj"]
'str'
>>> PyAnnotatedClass.__annotations__["literal"]
"'int'"
>>> PyAnnotatedClass.__annotations__["recurse"]
"'PyAnnotatedClass'"
>>> PyAnnotatedClass.__annotations__["default"]
'bool'
>>> PyAnnotatedClass.CLASS_VAR
1
>>> PyAnnotatedClass.default
False
>>> PyAnnotatedClass.obj
Traceback (most recent call last):
...
AttributeError: type object 'PyAnnotatedClass' has no attribute 'obj'
"""
CLASS_VAR: ClassVar[int] = 1
obj: str
literal: "int"
recurse: "PyAnnotatedClass"
default: bool = False
class PyVanillaClass:
"""
>>> PyVanillaClass.__annotations__
Traceback (most recent call last):
...
AttributeError: type object 'PyVanillaClass' has no attribute '__annotations__'
"""