-
Notifications
You must be signed in to change notification settings - Fork 16
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
Allow ignoring errors using code comments #54
Comments
Here is an use case: https://github.com/frequenz-floss/frequenz-channels-python/blob/922c7c33a5bebd30d3b0b8196749270773461a65/src/frequenz/channels/_base_classes.py#L190-L199 We want to have the exception specified because it is following an interface (and the code can actually Here is the error I get:
|
Hi @llucax , pydoclint can do function-level violation omission in the flake8 mode. For example, def myFunc( # noqa: DOC101, DOC103
arg1: int,
arg2: str,
) -> None:
"""
Do something
Parameters
----------
arg1 : float
Arg 1
"""
pass The But I mentioned in this part of README that "noqa" omission in the native mode may be added if there are demands for it. That said, I don't think per-argument omission is feasible in pydoclint's current architecture. This is because when pydoclint uses I also checked the parsed node, and I could not find any trace of If we want to parse per-argument |
Hi @jsh9, I see, thanks for the detailed explanation and sorry for not noticing the I think astroid, the parser used by Is there any way to add "arguments" to the As a very hacky solution I could also do: def always_raises_exception(x):
"""Raise a zero division error or type error.o
Args:
x: The argument which could be a number or could not be.
Raises:
ZeroDivisionError: If x is a number.
TypeError: If x is not a number.
"""
x / 0
# pylint: disable=unreachable
raise ZeroDivisionError() # type:ignore
raise TypeError() # type: ignore But it is quite nasty. I think being able to ignore certain type of exceptions in the check is super important, because as shown in the code above, even python operators can raise exceptions, what would be the alternative, wrap the division in a try/except block and create a new If I do this: try:
x / 0
except ZeroDivisionError:
raise Then Then I have to do this, which is even more verbose: try:
x / 0
except ZeroDivisionError as exc:
raise ZeroDivisionError("Cannot divide by zero.") from exc And also adds unnecessary runtime costs, as I'm creating new objects, and makes the stack trace harder to read, as I have 2 I don't think this feature is complete without being able to ignore specific exceptions. |
If you'd like to ignore def myFunc(...): # noqa: DOC104, DOC201
.... Is this the granularity that you are looking for? |
Also, I added a section in the documentation on how to ignore certain violation codes: 8e0ccce |
No, I meant being able to ignore certain exception types for def raises_exceptions(x): # noqa: DOC502(ZeroDivisionError, TypeError), DOC501(AssertError)
"""Raise a zero division error or type error.o
Args:
x: The argument which could be a number or could not be.
Raises:
ZeroDivisionError: If x is a number.
TypeError: If x is not a number.
WeirdException: blah.
"""
if x:
x / 0
elif x > 0:
raise MyException()
else:
raise AssertError() So this should complain about I guess the broad disabling of the whole check is not that bad, but it would be very nice to still catch mistakes even if there are some exceptions that are raised indirectly. |
Thanks! |
Hi @llucax , what you are looking for ( I'll label this as "may tackle in the future" and close this issue for now. But if you have any additional questions or comments, please feel free to add them below. Of if you'd like to re-open it for another reason, please feel free to do so. Thanks! |
Fair enough. |
Hi @jsh9, sorry to comment on this closed issue, but maybe this info will change the perception about it and maybe consider reopening. I just tried using Running on https://github.com/frequenz-floss/frequenz-sdk-python/: $ time flake8 --select=DOC src
...
real 0m10.562s
user 1m1.794s
sys 0m0.530s
$ time pydoclint src
...
real 0m0.189s
user 0m0.156s
sys 0m0.033s
It is still an improvement over Also maybe you should consider updating the README to be more accurate about the differences between running as a |
Hi @llucax , I pulled frequenz-sdk-python (the latest on the main branch: frequenz-floss/frequenz-sdk-python@1186f6f) and tested it myself:
(somehow I didn't get the nice formatting that you had. I'm using macOS with zsh.) There is still a big difference between running with flake8 and with native pydoclint, but somehow not as drastic as on your computer/environment. I'm curious what flake8 version do you have on your end? Mine is 6.0.0. |
$ flake8 --version
6.1.0 (darglint: 1.8.1, mccabe: 0.7.0, pycodestyle: 2.11.0, pydoclint: 0.2.2, pyflakes: 3.1.0) CPython 3.11.4 on Linux BTW, |
OK, I just uninstalled
Twice as slow as running it directly but still sub-second, which is good enough. Thanks and sorry for the noise! |
I guess you are referring to the markdown formatting in his comment. That can be done by writing ```console
$ command
output
``` |
Like most linters, it would be very useful being able to ignore a certain type of error for a particular function/class/module.
With documentation linting is harder to achieve, as annotating docstrings
Darglint allowed this but by annotating the docstrings, but that is not a good option, as the annotations will show up then in the help, which is unhelpful and confusing to users.
The flexibility of the scheme
# noqa: <error> <argument>
is very useful though. For example, to only ignore the checking of one argument, one can do:Or to ignore only one type of exception in
Raises
:I suggest, if possible, adding the
# noqa:
comments directly in the code, for the above examples:Using the equivalent codes from
pydoclint
, of course.The text was updated successfully, but these errors were encountered: