Closed dirkroorda closed 2 years ago
Hello @dirkroorda, thank you for the report!
It seems that pytkdocs does work but produces output on which mkdocstrings crashes. Whether this is a bug in mkdocstrings or in pytkdocs is beyond me.
Actually it seems that compile
itself (which is called by ast.parse
) has trouble handling such code separately. Maybe the dedent is botching the source but I doubt that? I'll run some tests.
In the meantime you could try to dedent the string yourself as a workaround:
from textwrap import dedent
class Aaa:
# def method(self):
def __init__(self):
print(
dedent(
"""
aaa
"""
)
)
inspect.cleandoc
might handle more cases:
from inspect import cleandoc
class Aaa:
# def method(self):
def __init__(self):
print(
cleandoc(
"""
aaa
"""
)
)
Yes, and another work around is to put such code in an other method and call that method from init.
The puzzling fact is that such code does not lead to problems in non-init methods, so I wonder if mkdocstrings has a separate pass over the code before feeding it to the parser, or does it tweak the output of the parser in some way?
__init__
methods are in fact the only methods that pytkdocs actually parses in search of annotations/docstrings for instances attributes.
That explains why it does not fail the same way on other methods with the same code.
I suspect compile
is not happy to see code starting with indentation (relative to the multiline string!), and am not sure how to make it happy with it :/
Yup I think it won't be easy to fix this, unfortunately. To parse this code, compile
needs the code before it, leading to the indentation it has.
Note however that I'm working on a replacement of pytkdocs that will not suffer from this issue :slightly_smiling_face:
Closing as won't fix, but feel free to further comment on this if you think this should really be fixed.
Gladly applied the dedent
workaround everywhere. Did not know it before, makes my code a bit nicer.
If the
__init__
method of a class has code that extrudes to the left margin an error is produced.Here is a minimal example
If we rename the
__init__
method to anything else, e.g.method
, there is no problem. The expected behaviour is that there is no problem at all.Here is the complete set up:
with this content
aaa.md
mkdocs.yml
The command is
and the output is
It seems that pytkdocs does work but produces output on which mkdocstrings crashes. Whether this is a bug in mkdocstrings or in pytkdocs is beyond me.
System:
pytkdocs
version 0.12.0