Skip to content

Commit ee624ae

Browse files
author
lihu
committed
Update special dataclass handling
The dataclass generated qualnames were updated in a patch version of python, likely the update that either caused or fixed https://bugs.python.org/issue34776 Fixes tox-dev#123
1 parent 55a6908 commit ee624ae

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

sphinx_autodoc_typehints.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,8 +165,20 @@ def process_signature(app, what: str, name: str, obj, options, signature, return
165165
for param in signature.parameters.values()
166166
]
167167

168-
# The generated dataclass __init__() is weird and needs the second condition
169-
if '<locals>' in obj.__qualname__ and not (what == 'method' and name.endswith('.__init__')):
168+
# The generated dataclass __init__() and class are weird and need extra checks
169+
# This helper function operates on the generated class and methods
170+
# of a dataclass, not an instantiated dataclass object. As such,
171+
# it cannot be replaced by a call to `dataclasses.is_dataclass()`.
172+
def _is_dataclass(name: str, what: str, qualname: str) -> bool:
173+
if what == 'method' and name.endswith('.__init__'):
174+
# generated __init__()
175+
return True
176+
if what == 'class' and qualname.endswith('.__init__'):
177+
# generated class
178+
return True
179+
return False
180+
181+
if '<locals>' in obj.__qualname__ and not _is_dataclass(name, what, obj.__qualname__):
170182
logger.warning(
171183
'Cannot treat a function defined as a local function: "%s" (use @functools.wraps)',
172184
name)

0 commit comments

Comments
 (0)