-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
Keeping original signatures for functions/methods #1882
Changes from all commits
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 |
---|---|---|
|
@@ -30,7 +30,7 @@ | |
from sphinx.util.nodes import nested_parse_with_titles | ||
from sphinx.util.compat import Directive | ||
from sphinx.util.inspect import getargspec, isdescriptor, safe_getmembers, \ | ||
safe_getattr, object_description, is_builtin_class_method | ||
safe_getattr, object_description, is_builtin_class_method, dumb_signature | ||
from sphinx.util.docstrings import prepare_docstring | ||
|
||
|
||
|
@@ -1064,6 +1064,8 @@ def format_args(self): | |
args = formatargspec(*argspec) | ||
# escape backslashes for reST | ||
args = args.replace('\\', '\\\\') | ||
if self.env.config.autodoc_dumb_docstring: | ||
args = dumb_signature(self.object) | ||
return args | ||
|
||
def document_members(self, all_members=False): | ||
|
@@ -1116,7 +1118,11 @@ def format_args(self): | |
return None | ||
if argspec[0] and argspec[0][0] in ('cls', 'self'): | ||
del argspec[0][0] | ||
return formatargspec(*argspec) | ||
|
||
if self.env.config.autodoc_dumb_docstring: | ||
return dumb_signature(initmeth) | ||
else: | ||
return formatargspec(*argspec) | ||
|
||
def format_signature(self): | ||
if self.doc_as_attr: | ||
|
@@ -1286,6 +1292,8 @@ def format_args(self): | |
args = formatargspec(*argspec) | ||
# escape backslashes for reST | ||
args = args.replace('\\', '\\\\') | ||
if self.env.config.autodoc_dumb_docstring: | ||
args = dumb_signature(self.object) | ||
return args | ||
|
||
def document_members(self, all_members=False): | ||
|
@@ -1520,6 +1528,7 @@ def setup(app): | |
app.add_config_value('autodoc_member_order', 'alphabetic', True) | ||
app.add_config_value('autodoc_default_flags', [], True) | ||
app.add_config_value('autodoc_docstring_signature', True, True) | ||
app.add_config_value('autodoc_dumb_docstring', False, True) | ||
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. Suggestion: This is not so much a “dumb” version but, maybe, a verbatim one? Also, it doesn't apply to the docstring as a whole but to the default values, ie. the argspec/signature. |
||
app.add_config_value('autodoc_mock_imports', [], True) | ||
app.add_event('autodoc-process-docstring') | ||
app.add_event('autodoc-process-signature') | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -158,3 +158,26 @@ def is_builtin_class_method(obj, attr_name): | |
if not hasattr(builtins, safe_getattr(cls, '__name__', '')): | ||
return False | ||
return getattr(builtins, safe_getattr(cls, '__name__', '')) is cls | ||
|
||
|
||
def dumb_signature(functional_object): | ||
src = inspect.getsourcelines(functional_object)[0] | ||
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. I have general concerns about this approach: There are several shortcomings in this hand-rolled parser (see examples below), which might lead to function signatures being completely misparsed. Reimplementing a Python parser with regular expressions is a poor idea anyways; I'd rather tune Also: |
||
|
||
for i, l in enumerate(src): | ||
if 'def' in l: | ||
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. Any decorator with |
||
break | ||
acc = '' | ||
for l in src[i:]: | ||
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. This would be much better done through a |
||
acc += l.strip() | ||
if '):' in acc: | ||
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. Spaces between the function signature and the terminating colon (eg. |
||
break | ||
|
||
r = re.compile(r'\((.*)\)') | ||
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. This breaks for any kind of tuple or function call inside the signature, eg. |
||
s = r.findall(acc) | ||
if not s: | ||
return '()' | ||
s = s[0] | ||
for d in [r'^self,', r'^self', r'^cls,', r'^cls']: | ||
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. This introduces new behaviour where Also, IIUC it would erroneously strip any parameter prefixed with |
||
s = re.sub(d, '', s) | ||
s = s.strip() | ||
return '(' + s + ')' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Your changes to the main Sphinx README make it hard to merge this PR as-is.