-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Add option to include docstrings with stubgen #13284
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 10 commits
9882092
6b85586
74bbee8
485dd75
2979837
b272ddf
eab9567
e1812ef
eefc49e
a0e8647
acd3168
ab64284
7a36ff0
97700e7
03c285c
0a61327
b7219a7
bb5b0c8
fa1f529
79d30fc
3fc3447
d232bfe
3f849b9
5d0cd9d
7318dad
7ea6727
4649599
182c9e6
6b9a46f
a7e8209
23a6a0f
3a7695e
7580e22
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -44,6 +44,7 @@ def generate_stub_for_c_module( | |
target: str, | ||
sigs: Optional[Dict[str, str]] = None, | ||
class_sigs: Optional[Dict[str, str]] = None, | ||
include_docstrings: bool = False, | ||
) -> None: | ||
"""Generate stub for C module. | ||
|
||
|
@@ -64,15 +65,30 @@ def generate_stub_for_c_module( | |
items = sorted(module.__dict__.items(), key=lambda x: x[0]) | ||
for name, obj in items: | ||
if is_c_function(obj): | ||
generate_c_function_stub(module, name, obj, functions, imports=imports, sigs=sigs) | ||
generate_c_function_stub( | ||
module, | ||
name, | ||
obj, | ||
functions, | ||
imports=imports, | ||
sigs=sigs, | ||
include_docstrings=include_docstrings, | ||
) | ||
done.add(name) | ||
types: List[str] = [] | ||
for name, obj in items: | ||
if name.startswith("__") and name.endswith("__"): | ||
continue | ||
if is_c_type(obj): | ||
generate_c_type_stub( | ||
module, name, obj, types, imports=imports, sigs=sigs, class_sigs=class_sigs | ||
module, | ||
name, | ||
obj, | ||
types, | ||
imports=imports, | ||
sigs=sigs, | ||
class_sigs=class_sigs, | ||
include_docstrings=include_docstrings, | ||
) | ||
done.add(name) | ||
variables = [] | ||
|
@@ -156,10 +172,11 @@ def generate_c_function_stub( | |
sigs: Optional[Dict[str, str]] = None, | ||
class_name: Optional[str] = None, | ||
class_sigs: Optional[Dict[str, str]] = None, | ||
include_docstrings: bool = False, | ||
) -> None: | ||
"""Generate stub for a single function or method. | ||
|
||
The result (always a single line) will be appended to 'output'. | ||
The result will be appended to 'output'. | ||
If necessary, any required names will be added to 'imports'. | ||
The 'class_name' is used to find signature of __init__ or __new__ in | ||
'class_sigs'. | ||
|
@@ -170,7 +187,7 @@ def generate_c_function_stub( | |
class_sigs = {} | ||
|
||
ret_type = "None" if name == "__init__" and class_name else "Any" | ||
|
||
docstr = None | ||
if ( | ||
name in ("__new__", "__init__") | ||
and name not in sigs | ||
|
@@ -236,13 +253,23 @@ def generate_c_function_stub( | |
|
||
if is_overloaded: | ||
output.append("@overload") | ||
output.append( | ||
"def {function}({args}) -> {ret}: ...".format( | ||
function=name, | ||
args=", ".join(sig), | ||
ret=strip_or_import(signature.ret_type, module, imports), | ||
if include_docstrings and docstr: | ||
output.append( | ||
'def {function}({args}) -> {ret}:\n"""{docstr}"""\n...'.format( | ||
function=name, | ||
args=", ".join(sig), | ||
ret=strip_or_import(signature.ret_type, module, imports), | ||
docstr=docstr, | ||
) | ||
) | ||
else: | ||
output.append( | ||
"def {function}({args}) -> {ret}: ...".format( | ||
function=name, | ||
args=", ".join(sig), | ||
ret=strip_or_import(signature.ret_type, module, imports), | ||
) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Isn't this part completely repeats the other branch? If yes, then please use if foo:
bar
baz instead of if foo:
bar
baz
else:
baz There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To be more precise, in this case you need:
since you always need a type, and docstring should come after (only if the flag is enabled) |
||
) | ||
) | ||
|
||
|
||
def strip_or_import(typ: str, module: ModuleType, imports: List[str]) -> str: | ||
|
@@ -339,6 +366,7 @@ def generate_c_type_stub( | |
imports: List[str], | ||
sigs: Optional[Dict[str, str]] = None, | ||
class_sigs: Optional[Dict[str, str]] = None, | ||
include_docstrings: bool = False, | ||
) -> None: | ||
"""Generate stub for a single class using runtime introspection. | ||
|
||
|
@@ -382,6 +410,7 @@ def generate_c_type_stub( | |
sigs=sigs, | ||
class_name=class_name, | ||
class_sigs=class_sigs, | ||
include_docstrings=include_docstrings, | ||
) | ||
elif is_c_property(value): | ||
done.add(attr) | ||
|
@@ -397,7 +426,14 @@ def generate_c_type_stub( | |
) | ||
elif is_c_type(value): | ||
generate_c_type_stub( | ||
module, attr, value, types, imports=imports, sigs=sigs, class_sigs=class_sigs | ||
module, | ||
attr, | ||
value, | ||
types, | ||
imports=imports, | ||
sigs=sigs, | ||
class_sigs=class_sigs, | ||
include_docstrings=include_docstrings, | ||
) | ||
done.add(attr) | ||
|
||
|
Uh oh!
There was an error while loading. Please reload this page.