Skip to content

Commit 06b9b49

Browse files
committed
Add Lib/test/typinganndata folder
cpython version: 3.12
1 parent b5e21ab commit 06b9b49

File tree

10 files changed

+173
-0
lines changed

10 files changed

+173
-0
lines changed

Lib/test/typinganndata/__init__.py

Whitespace-only changes.

Lib/test/typinganndata/ann_module.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
2+
3+
"""
4+
The module for testing variable annotations.
5+
Empty lines above are for good reason (testing for correct line numbers)
6+
"""
7+
8+
from typing import Optional
9+
from functools import wraps
10+
11+
__annotations__[1] = 2
12+
13+
class C:
14+
15+
x = 5; y: Optional['C'] = None
16+
17+
from typing import Tuple
18+
x: int = 5; y: str = x; f: Tuple[int, int]
19+
20+
class M(type):
21+
22+
__annotations__['123'] = 123
23+
o: type = object
24+
25+
(pars): bool = True
26+
27+
class D(C):
28+
j: str = 'hi'; k: str= 'bye'
29+
30+
from types import new_class
31+
h_class = new_class('H', (C,))
32+
j_class = new_class('J')
33+
34+
class F():
35+
z: int = 5
36+
def __init__(self, x):
37+
pass
38+
39+
class Y(F):
40+
def __init__(self):
41+
super(F, self).__init__(123)
42+
43+
class Meta(type):
44+
def __new__(meta, name, bases, namespace):
45+
return super().__new__(meta, name, bases, namespace)
46+
47+
class S(metaclass = Meta):
48+
x: str = 'something'
49+
y: str = 'something else'
50+
51+
def foo(x: int = 10):
52+
def bar(y: List[str]):
53+
x: str = 'yes'
54+
bar()
55+
56+
def dec(func):
57+
@wraps(func)
58+
def wrapper(*args, **kwargs):
59+
return func(*args, **kwargs)
60+
return wrapper
61+
62+
u: int | float

Lib/test/typinganndata/ann_module2.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
"""
2+
Some correct syntax for variable annotation here.
3+
More examples are in test_grammar and test_parser.
4+
"""
5+
6+
from typing import no_type_check, ClassVar
7+
8+
i: int = 1
9+
j: int
10+
x: float = i/10
11+
12+
def f():
13+
class C: ...
14+
return C()
15+
16+
f().new_attr: object = object()
17+
18+
class C:
19+
def __init__(self, x: int) -> None:
20+
self.x = x
21+
22+
c = C(5)
23+
c.new_attr: int = 10
24+
25+
__annotations__ = {}
26+
27+
28+
@no_type_check
29+
class NTC:
30+
def meth(self, param: complex) -> None:
31+
...
32+
33+
class CV:
34+
var: ClassVar['CV']
35+
36+
CV.var = CV()

Lib/test/typinganndata/ann_module3.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
"""
2+
Correct syntax for variable annotation that should fail at runtime
3+
in a certain manner. More examples are in test_grammar and test_parser.
4+
"""
5+
6+
def f_bad_ann():
7+
__annotations__[1] = 2
8+
9+
class C_OK:
10+
def __init__(self, x: int) -> None:
11+
self.x: no_such_name = x # This one is OK as proposed by Guido
12+
13+
class D_bad_ann:
14+
def __init__(self, x: int) -> None:
15+
sfel.y: int = 0
16+
17+
def g_bad_ann():
18+
no_such_name.attr: int = 0

Lib/test/typinganndata/ann_module4.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# This ann_module isn't for test_typing,
2+
# it's for test_module
3+
4+
a:int=3
5+
b:str=4

Lib/test/typinganndata/ann_module5.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Used by test_typing to verify that Final wrapped in ForwardRef works.
2+
3+
from __future__ import annotations
4+
5+
from typing import Final
6+
7+
name: Final[str] = "final"
8+
9+
class MyClass:
10+
value: Final = 3000

Lib/test/typinganndata/ann_module6.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Tests that top-level ClassVar is not allowed
2+
3+
from __future__ import annotations
4+
5+
from typing import ClassVar
6+
7+
wrong: ClassVar[int] = 1

Lib/test/typinganndata/ann_module7.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Tests class have ``__text_signature__``
2+
3+
from __future__ import annotations
4+
5+
DEFAULT_BUFFER_SIZE = 8192
6+
7+
class BufferedReader(object):
8+
"""BufferedReader(raw, buffer_size=DEFAULT_BUFFER_SIZE)\n--\n\n
9+
Create a new buffered reader using the given readable raw IO object.
10+
"""
11+
pass

Lib/test/typinganndata/ann_module8.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Test `@no_type_check`,
2+
# see https://bugs.python.org/issue46571
3+
4+
class NoTypeCheck_Outer:
5+
class Inner:
6+
x: int
7+
8+
9+
def NoTypeCheck_function(arg: int) -> int:
10+
...

Lib/test/typinganndata/ann_module9.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Test ``inspect.formatannotation``
2+
# https://github.com/python/cpython/issues/96073
3+
4+
from typing import Union, List
5+
6+
ann = Union[List[str], int]
7+
8+
# mock typing._type_repr behaviour
9+
class A: ...
10+
11+
A.__module__ = 'testModule.typing'
12+
A.__qualname__ = 'A'
13+
14+
ann1 = Union[List[A], int]

0 commit comments

Comments
 (0)