-
-
Notifications
You must be signed in to change notification settings - Fork 107
WARNING: Cannot treat a function defined as a local function #123
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
Comments
I cloned the repo to debug further, but the unit tests seem to be failing out-of-the-gate for me: test_sphinx_output[False]
test_sphinx_output[True]
This occurs for both 3.7.6 and 3.8.1 with tox. |
Looks like this is a breaking change introduced by bpo-34776. The issue tracker mentions it was merged into 3.7 and 3.8, but I do not see mention of it in the 3.7 change-log. |
I just ran into the issue on Python 3.7.6. |
I can repro this but the warnings don't give me much clue about where they originate from or what causes them. Anyone? |
Ok, so this is caused by the |
It's unfortunate that |
I am trying to better understand the changes made in bpo-34776, but reading the dataclasses source is leading to a lot of questions; does anyone know why a string of python is being constructed then def _create_fn(name, args, body, *, globals=None, locals=None,
return_type=MISSING):
# Note that we mutate locals when exec() is called. Caller
# beware! The only callers are internal to this module, so no
# worries about external callers.
if locals is None:
locals = {}
if 'BUILTINS' not in locals:
locals['BUILTINS'] = builtins
return_annotation = ''
if return_type is not MISSING:
locals['_return_type'] = return_type
return_annotation = '->_return_type'
args = ','.join(args)
body = '\n'.join(f' {b}' for b in body)
# Compute the text of the entire function.
txt = f' def {name}({args}){return_annotation}:\n{body}'
local_vars = ', '.join(locals.keys())
txt = f"def __create_fn__({local_vars}):\n{txt}\n return {name}"
ns = {}
exec(txt, globals, ns)
return ns['__create_fn__'](**locals) |
If I ignore this one warning, what will the difference be in my rendered docs between those docs when I am using a Python version which hits this bug and a Python version which does not? I guess from reading that the |
I have seen zero difference, but I do not render |
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
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
For those looking for a temp workaround until #153 gets merged, in your sphinx import logging as pylogging
from sphinx.util import logging
# Workaround for https://github.com/agronholm/sphinx-autodoc-typehints/issues/123
# When this https://github.com/agronholm/sphinx-autodoc-typehints/pull/153
# gets merged, we can remove this
class FilterForIssue123(pylogging.Filter):
def filter(self, record: pylogging.LogRecord) -> bool:
# You probably should make this check more specific by checking
# that dataclass name is in the message, so that you don't filter out
# other meaningful warnings
return not record.getMessage().startswith("Cannot treat a function")
logging.getLogger("sphinx_autodoc_typehints").logger.addFilter(FilterForIssue123())
# End of a workaround I couldn't find a better way to do this with sphinx's |
Of note is that it was necessary to add a logger filter to suppress a warning from incorrect code in sphinx_autodoc_typehints. Workaround FROM: https://github.com/agronholm/sphinx-autodoc-typehints/issues/123#issuecomment-698314873 Probable Real Fix: https://github.com/agronholm/sphinx-autodoc-typehints/pull/153
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
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
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
I have a hacky solution for this originally from https://github.com/pykeen/pykeen/blob/5d838af8c88d143420db06ddf994582c27a6d568/src/pykeen/utils.py#L343-L352 def fix_dataclass_init_docs(cls: Type) -> Type:
"""Fix the ``__init__`` documentation for a :class:`dataclasses.dataclass`.
:param cls: The class whose docstring needs fixing
:returns: The class that was passed so this function can be used as a decorator
.. seealso:: https://github.com/agronholm/sphinx-autodoc-typehints/issues/123
"""
cls.__init__.__qualname__ = f'{cls.__name__}.__init__'
return cls Then you apply it as a decorator to your dataclass (you can always just do this to a class directly, but I like the decorator route) |
A PR for this would be welcome. |
If you point me towards the right place to put it, I will give it a shot |
First try to create a test to reproduce the issue here https://github.com/tox-dev/sphinx-autodoc-typehints/blob/main/tests/test_sphinx_autodoc_typehints.py#L757, then look at what fails in https://github.com/tox-dev/sphinx-autodoc-typehints/blob/main/src/sphinx_autodoc_typehints/__init__.py and fix it there 👌 |
With python 3.8.1 documenting a dataclass leads to this warning:
This only occurs in python 3.8.1, python 3.8.0 does not show this warning.
This is minimally reproducible with 3 files:
conf.py
doc.rst
mod.py
Command used:
Relevant versions:
The text was updated successfully, but these errors were encountered: