-
Notifications
You must be signed in to change notification settings - Fork 148
Expand file tree
/
Copy pathtest_helpers.py
More file actions
156 lines (110 loc) · 3.56 KB
/
test_helpers.py
File metadata and controls
156 lines (110 loc) · 3.56 KB
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
from functools import wraps
from typing import Any
from typing import Callable
from typing import cast
from typing import TypeVar
from pluggy._hooks import varnames
from pluggy._manager import _formatdef
def test_varnames() -> None:
def f(x) -> None:
i = 3 # noqa
class A:
def f(self, y) -> None:
pass
class B:
def __call__(self, z) -> None:
pass
assert varnames(f) == (("x",), ())
assert varnames(A().f) == (("y",), ())
assert varnames(B()) == (("z",), ())
def test_varnames_default() -> None:
def f(x, y=3) -> None:
pass
assert varnames(f) == (("x",), ("y",))
def test_varnames_class() -> None:
class C:
def __init__(self, x) -> None:
pass
class D:
pass
class E:
def __init__(self, x) -> None:
pass
class F:
pass
assert varnames(C) == (("x",), ())
assert varnames(D) == ((), ())
assert varnames(E) == (("x",), ())
assert varnames(F) == ((), ())
def test_varnames_catch_all() -> None:
"""
Adding coverage for special cases. According to the code in varnames(), we should handle
several unusual exception-throwing scenarios.
"""
class NoInitMeta(type):
def __getattribute__(self, item):
if item == "__init__":
raise AttributeError(
"Testing a class where we can't look at the __init__ attr"
)
else:
return object.__getattribute__(self, item)
class NoInit(metaclass=NoInitMeta):
"""A class that throws AttributeError for '__init__'"""
class CantCallMe:
def __getattribute__(self, item):
if item == "__call__":
raise ValueError("Don't look at me")
else:
return object.__getattribute__(self, item)
def __call__(self, *args, **kwargs):
pass
def has_weird_signature():
pass
setattr(
has_weird_signature, "__signature__", "inspect will fail b/c this is a string"
)
assert varnames(NoInit) == ((), ())
assert varnames(CantCallMe()) == ((), ())
assert varnames(has_weird_signature) == ((), ())
def test_varnames_keyword_only() -> None:
def f1(x, *, y) -> None:
pass
def f2(x, *, y=3) -> None:
pass
def f3(x=1, *, y=3) -> None:
pass
assert varnames(f1) == (("x",), ())
assert varnames(f2) == (("x",), ())
assert varnames(f3) == ((), ("x",))
def test_formatdef() -> None:
def function1():
pass
assert _formatdef(function1) == "function1()"
def function2(arg1):
pass
assert _formatdef(function2) == "function2(arg1)"
def function3(arg1, arg2="qwe"):
pass
assert _formatdef(function3) == "function3(arg1, arg2='qwe')"
def function4(arg1, *args, **kwargs):
pass
assert _formatdef(function4) == "function4(arg1, *args, **kwargs)"
def test_varnames_decorator() -> None:
F = TypeVar("F", bound=Callable[..., Any])
def my_decorator(func: F) -> F:
@wraps(func)
def wrapper(*args, **kwargs):
return func(*args, **kwargs)
return cast(F, wrapper)
@my_decorator
def example(a, b=123) -> None:
pass
class Example:
@my_decorator
def example_method(self, x, y=1) -> None:
pass
ex_inst = Example()
assert varnames(example) == (("a",), ("b",))
assert varnames(Example.example_method) == (("x",), ("y",))
assert varnames(ex_inst.example_method) == (("x",), ("y",))