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
# type: str
between attributes breaks attribute documentation
#8652
Comments
Yes, autodoc considers that comment starts with Hence, there are two kind of type annotations on failed code:
We can't determine the correct type of
Are you saying autodoc should build code having both variable annotation and type comments? I don't understand why do you create a module. Please tell me why do you want to annotate types in two ways. |
No, that is more or less my issue. I was just trying to comment out my variable Note, that I posted an excerpt that was slightly different (I updated it in the issue description but in the end with the same issue/result) # #: Type of transaction <--- commented out
# type: str <--- commented out (but interpreted as type hint?)
#: Price of (crypto) currency.
price: float # i wanted to use it like this (in a dataclass, so default values are not critical)
varname: typename = defaultvalue
# i know that it is also to possible to use it like this
varname = defaultvalue # type: typename
# but I thought this was not a type annotation
varname = defaultvalue
# type: typename |
I reproduced what happened in this case. I found
|
I'm debating which is better 1) retry parsing without type_comments, or 2) emit a warning for SyntaxError. |
Yes, but with Is it possible to explicitly set sphinx to ignore type hints in comments? So the parsing with And might this be an error in import ast
ast.parse("# type: str", type_comments=True) I think 2) a warning makes it more transparent for the user. Just having all comments disappear in the generated doc is disconcerting. And silently reparsing it and then having other type hints in comments being ignored, might look ok but is not immediately visible to the user and can then be overlooked for some time. Researching when it happened might take more time, so a visible warning or error might be best. |
In my understanding, what you really want is autodoc can build a document even if the code contains invalid type_comments. So such option is not needed, right? I think your goal is 1) to make our parser more robust even if the code contains invalid type_comments, or 2) to warn on comment parsing failure. |
Yes, you are right. |
I posted a PR #8667 to fix this. Finally, I choose the 1st way because robustness is worthy for our parse. And it is a bit hard to emit a warning on the error. |
Ok. Thank you. |
What do you mean? After #8667 merged, this error will not happen. If you'd like to find the invalid type comment like |
Yes, you are correct. |
Fix #8652: autodoc: variable comments are ignored if invalid type comments found
Thank you for comment. Now I merged #8667. So I'm closing this. Thank you for reporting :-) |
Great. More like thank you for fixing ;-) |
Describe the bug
The comment
# type: str
between docstring-commented (dataclass) attributes breaks the autoclass documentation.To Reproduce
To reproduce I have linked my project. A more brief example may come later.
Steps to reproduce the behavior:
Failing:
Working:
Note that the only difference is in the middle line with the problematic comment:
If I change
type
tostype
or anything else, it won't fail. I assume it somehow parsestype
and tries to interpret it as part of the documentation or some python code? The curious thing is that it is not even a docstring comment#:
, so my only other guess is that it tries to interpret it as typehint (even if it is on separate line)?Expected behavior
It should show the dataclass attributes in the generated documentation.
Your project
Screenshots
Maybe this will show it visually, but in-short, all attributes in dataclasses will not appear if the problematic comment is in the code.
If comment is inserted (only properties, no attributes):
Without the comment (you can see the attributes):
Environment info
alabaster==0.7.12,Babel==2.9.0,certifi==2020.12.5,chardet==4.0.0,docutils==0.16,idna==2.10,imagesize==1.2.0,Jinja2==2.11.2,-e git+git@github.com:Querela/python-krakenexapi.git@f29ec8b41ac57e81317ab4e7ee0761f07e4bd3db#egg=krakenexapi,MarkupSafe==1.1.1,packaging==20.8,Pygments==2.7.3,pyparsing==2.4.7,pytz==2020.5,requests==2.25.1,snowballstemmer==2.0.0,Sphinx==3.4.2,sphinx-rtd-theme==0.5.0,sphinxcontrib-applehelp==1.0.2,sphinxcontrib-devhelp==1.0.2,sphinxcontrib-htmlhelp==1.0.3,sphinxcontrib-jsmath==1.0.1,sphinxcontrib-qthelp==1.0.3,sphinxcontrib-serializinghtml==1.1.4,urllib3==1.26.2,wrapt==1.12.1
NOTE Update
The order of comments was slightly different (but with same end results)
The text was updated successfully, but these errors were encountered: