forked from joke2k/faker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate_stubs.py
229 lines (183 loc) · 8.94 KB
/
generate_stubs.py
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
import inspect
import pathlib
import re
from collections import defaultdict
from typing import Any, DefaultDict, Dict, List, Optional, Set, Tuple, get_type_hints
import black
import isort
import faker.proxy
from faker import Factory
from faker.proxy import Faker
from faker.generator import Generator
from faker.config import AVAILABLE_LOCALES, PROVIDERS
from faker.providers import BaseProvider # noqa: F401
BUILTIN_MODULES_TO_IGNORE = ["builtins"]
GENERIC_MANGLE_TYPES_TO_IGNORE = ["builtin_function_or_method", "mappingproxy"]
MODULES_TO_FULLY_QUALIFY = ["datetime"]
imports: DefaultDict[str, Set[str]] = defaultdict(set)
def get_module_and_member_to_import(cls: Any, locale: str | None = None) -> Tuple[str, str]:
cls_name = getattr(cls, "__name__", getattr(cls, "_name", str(cls)))
module, member = cls.__module__, cls_name
if cls_name is None:
qualified_type = re.findall(r"([a-zA-Z_0-9]+)\.([a-zA-Z_0-9]+)", str(cls))
if len(qualified_type) > 0:
if imports[qualified_type[0][0]] is None or qualified_type[0][1] not in imports[qualified_type[0][0]]:
module, member = qualified_type[0]
else:
unqualified_type = re.findall(r"[^\.a-zA-Z0-9_]([A-Z][a-zA-Z0-9_]+)[^\.a-zA-Z0-9_]", " " + str(cls) + " ")
if len(unqualified_type) > 0 and unqualified_type[0] != "NoneType":
cls_str = str(cls).replace(".en_US", "").replace("faker.", ".")
if "<class '" in cls_str:
cls_str = cls_str.split("'")[1]
if locale is not None:
cls_str = cls_str.replace("." + locale, "")
if imports[cls_str] is None or unqualified_type[0] not in imports[cls_str]:
module, member = cls_str, unqualified_type[0]
if module in MODULES_TO_FULLY_QUALIFY:
member = None
return module, member
seen_funcs: set = set()
seen_vars: set = set()
class UniqueMemberFunctionsAndVariables:
def __init__(self, cls: type, funcs: Dict[str, Any], vars: Dict[str, Any]):
global seen_funcs, seen_vars
self.cls = cls
self.funcs = funcs
for func_name in seen_funcs:
self.funcs.pop(func_name, None)
seen_funcs = seen_funcs.union(self.funcs.keys())
self.vars = vars
for var_name in seen_vars.union(seen_funcs):
self.vars.pop(var_name, None)
seen_vars = seen_vars.union(self.vars.keys())
def get_member_functions_and_variables(cls: Any, include_mangled: bool = False) -> UniqueMemberFunctionsAndVariables:
members = [
(name, value)
for (name, value) in inspect.getmembers_static(cls)
if ((include_mangled and name.startswith("__")) or not name.startswith("_"))
]
funcs: Dict[str, Any] = {}
vars: Dict[str, Any] = {}
for name, value in members:
attr = getattr(cls, name, None)
if attr is not None and (inspect.isfunction(attr) or inspect.ismethod(attr)):
funcs[name] = value
elif inspect.isgetsetdescriptor(attr) or inspect.ismethoddescriptor(attr):
# I haven't implemented logic
# for generating descriptor signatures yet
continue
elif not include_mangled or type(value).__name__ not in GENERIC_MANGLE_TYPES_TO_IGNORE:
vars[name] = value
return UniqueMemberFunctionsAndVariables(cls, funcs, vars)
classes_and_locales_to_use_for_stub: List[Tuple[object, str]] = []
for locale in AVAILABLE_LOCALES:
for provider in PROVIDERS:
if provider == "faker.providers":
continue
prov_cls, _, _ = Factory._find_provider_class(provider, locale)
classes_and_locales_to_use_for_stub.append((prov_cls, locale))
all_members: List[Tuple[UniqueMemberFunctionsAndVariables, str | None]]
all_members = [(get_member_functions_and_variables(cls), locale) for cls, locale in classes_and_locales_to_use_for_stub]
all_members.append((get_member_functions_and_variables(Faker, include_mangled=True), None))
all_members.append((get_member_functions_and_variables(Generator, include_mangled=True), None))
# Use the accumulated seen_funcs and seen_vars to remove all variables that have the same name as a function somewhere
overlapping_var_names = seen_vars.intersection(seen_funcs)
for mbr_funcs_and_vars, _ in all_members:
for var_name_to_remove in overlapping_var_names:
mbr_funcs_and_vars.vars.pop(var_name_to_remove, None)
# list of tuples. First elem of tuple is the signature string,
# second is the comment string,
# third is a boolean which is True if the comment precedes the signature
signatures_with_comments: List[Tuple[str, Optional[str], bool]] = []
def recurse_annotation(annotation: Any, loc: str | None) -> None:
"""determine import modules"""
if (
annotation is not inspect.Parameter.empty
and annotation is not inspect.Signature.empty
and hasattr(annotation, "__module__")
and not annotation.__module__ in BUILTIN_MODULES_TO_IGNORE
):
if hasattr(annotation, "__args__"):
for ann in annotation.__args__:
recurse_annotation(ann, loc)
module, member = get_module_and_member_to_import(annotation, loc)
if module is not None and not (module == "faker.proxy" and member == "Faker"):
if imports[module] is None:
imports[module] = set() if member is None else {member}
elif member is not None:
imports[module].add(member)
for mbr_funcs_and_vars, loc in all_members:
for func_name, func_value in mbr_funcs_and_vars.funcs.items():
deco = ""
if isinstance(func_value, classmethod):
func_value = func_value.__func__
deco = "@classmethod\n"
if isinstance(func_value, staticmethod):
func_value = func_value.__func__
deco = "@staticmethod\n"
# works on forward refs as long as they are imported in THIS module!!
# you'll know if a module is missing when an exception like the following occurs
# NameError: name 'BaseProvider' is not defined
hints = get_type_hints(func_value, localns=locals(), include_extras=True)
sig = inspect.signature(func_value, locals=locals(), eval_str=True)
return_annotation = hints.pop("return") if "return" in hints else sig.return_annotation
recurse_annotation(return_annotation, loc)
new_parms = []
for key, parm_val in sig.parameters.items():
new_parm = parm_val
parm_annotation = hints[key] if key in hints else new_parm.annotation
if parm_val.default is not inspect.Parameter.empty:
new_parm = parm_val.replace(default=...)
recurse_annotation(parm_annotation, loc)
new_parms.append(new_parm)
sig = sig.replace(parameters=new_parms)
sig_str = str(sig)
sig_str = re.sub(r"ForwardRef\('([^']*)'\)", r"\1", sig_str)
sig_str = (
sig_str.replace("Ellipsis", "...")
.replace("NoneType", "None")
.replace("~", "")
.replace("faker.proxy.Faker", "Faker")
)
for module in imports.keys():
if module in MODULES_TO_FULLY_QUALIFY:
continue
sig_str = sig_str.replace(f"{module}.", "")
# uncomment if you want docstrings in the pyi
# comment = inspect.getdoc(func_value) or None
comment = None
rt = (f"{deco}def {func_name}{sig_str}: ...", comment, False)
signatures_with_comments.append(rt)
signatures_with_comments_as_str = []
for sigstr, comment, is_preceding_comment in signatures_with_comments:
if comment is not None and is_preceding_comment:
signatures_with_comments_as_str.append(f"# {comment}\n {sigstr}")
elif comment is not None:
sig_without_final_ellipsis = sigstr.strip(" .")
signatures_with_comments_as_str.append(
sig_without_final_ellipsis + '\n """\n ' + comment.replace("\n", "\n ") + '\n """\n ...'
)
else:
signatures_with_comments_as_str.append(sigstr)
def get_import_str(module: str, members: Optional[Set[str]]) -> str:
if members is None or len(members) == 0:
return f"import {module}"
else:
return f"from {module} import {', '.join(members)}"
imports_block = "\n".join([get_import_str(module, names) for module, names in imports.items()]).strip("\n")
member_signatures_block = " " + "\n ".join(
[sig.replace("\n", "\n ") for sig in signatures_with_comments_as_str]
)
output = f"""
{imports_block}
class Faker:
{member_signatures_block}
"""
output = isort.code(output, config=isort.Config(".isort.cfg"))
output = black.format_file_contents(
output, fast=False, mode=black.Mode(is_pyi=True, target_versions={black.TargetVersion.PY311}, line_length=120)
)
faker_proxy_path = pathlib.Path(inspect.getfile(faker.proxy))
stub_file_path = faker_proxy_path.with_name("proxy.pyi").resolve()
with open(stub_file_path, "w", encoding="utf-8") as fh:
fh.write(output)