Skip to content

Commit 6b86a6a

Browse files
committed
Local methods are only troublesome for methods
Signed-off-by: Bernát Gábor <gaborjbernat@gmail.com>
1 parent d5ed402 commit 6b86a6a

File tree

1 file changed

+11
-11
lines changed

1 file changed

+11
-11
lines changed

src/sphinx_autodoc_typehints/__init__.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -208,23 +208,16 @@ def process_signature(
208208
sph_signature = sphinx_signature(obj)
209209
parameters = [param.replace(annotation=inspect.Parameter.empty) for param in sph_signature.parameters.values()]
210210

211-
# bail if it is a local method as we cannot determine if first argument needs to be deleted or not
212-
def _is_dataclass(qualname: str) -> bool:
213-
# generated dataclass __init__() and class need extra checks, as the function operates on the generated class
214-
# and methods (not an instantiated dataclass object) it cannot be replaced by a call to
215-
# `dataclasses.is_dataclass()` => check manually for either generated __init__ or generated class
216-
return (what == "method" and name.endswith(".__init__")) or (what == "class" and qualname.endswith(".__init__"))
217-
218-
if "<locals>" in obj.__qualname__ and not _is_dataclass(obj.__qualname__):
219-
_LOGGER.warning('Cannot treat a function defined as a local function: "%s" (use @functools.wraps)', name)
220-
return None
221-
222211
# if we have parameters we may need to delete first argument that's not documented, e.g. self
223212
start = 0
224213
if parameters:
225214
if inspect.isclass(original_obj) or (what == "method" and name.endswith(".__init__")):
226215
start = 1
227216
elif what == "method":
217+
# bail if it is a local method as we cannot determine if first argument needs to be deleted or not
218+
if "<locals>" in obj.__qualname__ and not _is_dataclass(name, what, obj.__qualname__):
219+
_LOGGER.warning('Cannot handle as a local function: "%s" (use @functools.wraps)', name)
220+
return None
228221
outer = inspect.getmodule(obj)
229222
for class_name in obj.__qualname__.split(".")[:-1]:
230223
outer = getattr(outer, class_name)
@@ -240,6 +233,13 @@ def _is_dataclass(qualname: str) -> bool:
240233
return stringify_signature(sph_signature).replace("\\", "\\\\"), None
241234

242235

236+
def _is_dataclass(name: str, what: str, qualname: str) -> bool:
237+
# generated dataclass __init__() and class need extra checks, as the function operates on the generated class
238+
# and methods (not an instantiated dataclass object) it cannot be replaced by a call to
239+
# `dataclasses.is_dataclass()` => check manually for either generated __init__ or generated class
240+
return (what == "method" and name.endswith(".__init__")) or (what == "class" and qualname.endswith(".__init__"))
241+
242+
243243
def _future_annotations_imported(obj: Any) -> bool:
244244
_annotations = getattr(inspect.getmodule(obj), "annotations", None)
245245
if _annotations is None:

0 commit comments

Comments
 (0)