Skip to content

Commit cd9078e

Browse files
committed
Add support files for test_typing
1 parent 73d18fd commit cd9078e

File tree

4 files changed

+167
-0
lines changed

4 files changed

+167
-0
lines changed

Lib/test/ann_module.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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

Lib/test/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/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/mod_generics_cache.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
"""Module for testing the behavior of generics across different modules."""
2+
3+
import sys
4+
from textwrap import dedent
5+
from typing import TypeVar, Generic, Optional
6+
7+
8+
if sys.version_info[:2] >= (3, 6):
9+
exec(dedent("""
10+
default_a: Optional['A'] = None
11+
default_b: Optional['B'] = None
12+
13+
T = TypeVar('T')
14+
15+
16+
class A(Generic[T]):
17+
some_b: 'B'
18+
19+
20+
class B(Generic[T]):
21+
class A(Generic[T]):
22+
pass
23+
24+
my_inner_a1: 'B.A'
25+
my_inner_a2: A
26+
my_outer_a: 'A' # unless somebody calls get_type_hints with localns=B.__dict__
27+
"""))
28+
else: # This should stay in sync with the syntax above.
29+
__annotations__ = dict(
30+
default_a=Optional['A'],
31+
default_b=Optional['B'],
32+
)
33+
default_a = None
34+
default_b = None
35+
36+
T = TypeVar('T')
37+
38+
39+
class A(Generic[T]):
40+
__annotations__ = dict(
41+
some_b='B'
42+
)
43+
44+
45+
class B(Generic[T]):
46+
class A(Generic[T]):
47+
pass
48+
49+
__annotations__ = dict(
50+
my_inner_a1='B.A',
51+
my_inner_a2=A,
52+
my_outer_a='A' # unless somebody calls get_type_hints with localns=B.__dict__
53+
)

0 commit comments

Comments
 (0)