-
-
Notifications
You must be signed in to change notification settings - Fork 74
Expand file tree
/
Copy path__init__.py
More file actions
140 lines (94 loc) · 3.08 KB
/
Copy path__init__.py
File metadata and controls
140 lines (94 loc) · 3.08 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
import functools
class WithObjClass:
def __init__(self, suppress, fn_list):
self.suppress = suppress
self.fn_list = fn_list
def __enter__(self):
self.fn_list.append("enter")
return self # Return self so methods can be called on the bound variable
def doit_noerr(self):
return 1
def doit_err(self):
raise Exception("Spam", "Eggs")
def __exit__(self, ex_type, ex_val, ex_traceback):
self.fn_list.append("exit: " + str(ex_val))
return self.suppress
class FileWrapper:
"""Context manager where __enter__ returns a different object"""
def __init__(self, content):
self.content = content
def __enter__(self):
# Return a different object with the content
import io
return io.StringIO(self.content)
def __exit__(self, *args):
return False
def for_iter(arg):
retval = []
for item in arg:
retval.append(item)
return retval
def calling_custom_clojure_fn(arg):
return arg.clojure_fn()
def complex_fn(a, b, c: str = 5, *args, d=10, **kwargs):
return {"a": a, "b": b, "c": c, "args": args, "d": d, "kwargs": kwargs}
def defaults_fn(top=".", topdown=True, onerror=None, *, follow_symlinks=False, dir_fd=None):
"""Function with Python defaults for metadata tests."""
return top, topdown, onerror, follow_symlinks, dir_fd
def default_type_fn(dtype=int):
"""Function with a Python object default for metadata tests."""
return dtype
def kw_default_type_fn(*, dtype=int):
"""Function with a keyword-only Python object default for metadata tests."""
return dtype
complex_fn_testcases = {
"complex_fn(1, 2, c=10, d=10, e=10)": complex_fn(1, 2, c=10, d=10, e=10),
"complex_fn(1, 2, 10, 11, 12, d=10, e=10)": complex_fn(
1, 2, 10, 11, 12, d=10, e=10
),
}
class BadStr:
def __repr__(self):
raise ValueError("boom repr")
def __str__(self):
raise ValueError("boom str")
class WeirdStr:
def __repr__(self):
return "x" * 40
class HugeReprModel:
def __init__(self, n_layers=300):
self.n_layers = n_layers
def __repr__(self):
lines = ["GnarlyNet("]
for i in range(self.n_layers):
lines.append(f" (layer{i}): Linear(in_features=4096, out_features=4096, bias=True)")
lines.append(f" (act{i}): GELU(approximate='none')")
lines.append(f" (drop{i}): Dropout(p=0.1, inplace=False)")
lines.append(")")
return "\n".join(lines)
__str__ = __repr__
_bad = BadStr()
_weird = WeirdStr()
_sentinel = object()
_partial = functools.partial(int, 0)
_huge = HugeReprModel(300)
def f_class(x=int):
return x
def f_lambda(x=lambda a: a):
return x
def f_badstr(x=_bad):
return x
def f_weird(x=_weird):
return x
def f_sentinel(x=_sentinel):
return x
def f_partial(x=_partial):
return x
def f_nested_opaque(x=(int, str)):
return x
def f_huge(model=_huge):
return model
def f_kw_huge(*, model=_huge):
return model
def f_mixed(a, b=1, c=int, *, d=_sentinel, e=2):
return (a, b, c, d, e)