-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathguess_type.py
More file actions
208 lines (193 loc) · 4.97 KB
/
guess_type.py
File metadata and controls
208 lines (193 loc) · 4.97 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
from typing import Tuple, Optional, List
import re
# strategy heavily inspired by
# https://github.com/Zac-HD/hypothesis/blob/07ff885edaa0c11f480a8639a75101c6fe14844f/hypothesis-python/src/hypothesis/extra/ghostwriter.py#L319
def guess_type_from_argname(name: str) -> Tuple[Optional[str], List[str]]:
"""
If all else fails, we try guessing a strategy based on common argument names.
A "good guess" is _usually correct_, and _a reasonable mistake_ if not.
The logic below is therefore based on a manual reading of the builtins and
some standard-library docs, plus the analysis of about three hundred million
arguments in https://github.com/HypothesisWorks/hypothesis/issues/3311
"""
containers = "deque|list|set|iterator|tuple|iter|iterable"
# not using 'sequence', 'counter' or 'collection' due to likely false alarms
# (container)_(int|float|str|bool)s?
# e.g. list_ints => List[int]
# only check for built-in types to avoid false alarms, e.g. list_create, list_length
if m := re.fullmatch(
rf"(?P<container>{containers})_(?P<elems>int|float|str|bool)s?", name
):
container_type = m.group("container").capitalize()
if container_type == "Iter":
container_type = "Iterable"
return m.group("elems"), [container_type]
# <name>s?_(container)
# e.g. latitude_list => List[float]
# (container)_of_<name>(s)
# e.g. set_of_widths => Set[int]
if m := re.fullmatch(
rf"(?P<elems>\w+?)_?(?P<container>{containers})", name
) or re.fullmatch(rf"(?P<container>{containers})_of_(?P<elems>\w+)", name):
# only do a simple container match
# and don't check all of BOOL_NAMES to not trigger on stuff like "save_list"
elems = m.group("elems")
for names, name_type in (
(("bool", "boolean"), "bool"),
# don't trigger on `real_list`
(FLOAT_NAMES - {"real"}, "float"),
(INTEGER_NAMES, "int"),
(STRING_NAMES | {"string", "str"}, "str"),
):
if elems in names or (elems[-1] == "s" and elems[:-1] in names):
return name_type, [m.group("container").capitalize()]
# Names which imply the value is a boolean
if name.startswith("is_") or name in BOOL_NAMES:
return "bool", []
if (
name.endswith("_size")
or (name.endswith("size") and "_" not in name)
or re.fullmatch(r"n(um)?_[a-z_]*s", name)
or name in INTEGER_NAMES
):
return "int", []
if name in FLOAT_NAMES:
return "float", []
if (
"file" in name
or "path" in name
or name.endswith("_dir")
or name in ("fname", "dir", "dirname", "directory", "folder")
):
# Common names for filesystem paths: these are usually strings, but we
# don't want to make strings more convenient than pathlib.Path.
return None, []
if (
name.endswith("_name")
or (name.endswith("name") and "_" not in name)
or ("string" in name and "as" not in name)
or name.endswith("label")
or name in STRING_NAMES
):
return "str", []
# Last clever idea: maybe we're looking a plural, and know the singular:
# don't trigger on multiple ending "s" to avoid nested calls
if re.fullmatch(r"\w*[^s]s", name):
elems, container = guess_type_from_argname(name[:-1])
if elems is not None and not container:
return elems, ["Sequence"]
return None, []
BOOL_NAMES = {
"keepdims",
"verbose",
"debug",
"force",
"train",
"training",
"trainable",
"bias",
"shuffle",
"show",
"load",
"pretrained",
"save",
"overwrite",
"normalize",
"reverse",
"success",
"enabled",
"strict",
"copy",
"quiet",
"required",
"inplace",
"recursive",
"enable",
"active",
"create",
"validate",
"refresh",
"use_bias",
}
INTEGER_NAMES = {
"width",
"size",
"length",
"limit",
"idx",
"stride",
"epoch",
"epochs",
"depth",
"pid",
"steps",
"iteration",
"iterations",
"vocab_size",
"ttl",
"count",
"offset",
"seed",
"dim",
"total",
"priority",
"port",
"number",
"num",
"int",
}
FLOAT_NAMES = {
"real",
"imag",
"alpha",
"theta",
"beta",
"sigma",
"gamma",
"angle",
"reward",
"learning_rate",
"dropout",
"dropout_rate",
"epsilon",
"eps",
"prob",
"tau",
"temperature",
"lat",
"latitude",
"lon",
"longitude",
"radius",
"tol",
"tolerance",
"rate",
"treshold",
"float",
}
STRING_NAMES = {
"text",
"txt",
"password",
"label",
"prefix",
"suffix",
"desc",
"description",
"str",
"pattern",
"subject",
"reason",
"comment",
"prompt",
"sentence",
"sep",
"host",
"hostname",
"email",
"word",
"slug",
"api_key",
"char",
"character",
}