-
Notifications
You must be signed in to change notification settings - Fork 751
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
fix E721 types regex #1041
fix E721 types regex #1041
Conversation
Any `types.*Type*` matches incorrectly as a `type(...)` comparison; the regex `COMPARE_TYPE_REGEX` seems a bit too complicated for what should be a simple comparison case. Ref: https://github.com/PyCQA/pycodestyle/blob/main/pycodestyle.py#L147-L148 This reproduces the case in PyCQA#830
* `type(a) is type(b)` should still fail * same for `type(a) != type(b) or type(a) == type(ccc)` * We cannot assume `res == types.IntType` is wrong as the identity of the objects is not known at check time, either way it shouldn't be a E721 as it doesn't involve type(...) function as described in PEP8
@@ -80,3 +78,8 @@ def func_histype(a, b, c): | |||
pass | |||
except Exception: | |||
pass | |||
#: Okay | |||
from . import custom_types as types | |||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These test files aren't ever executed so we don't need a real module backing them here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks, removed custom_types.py
in 5df259b
reverting this for now so I can get a release out -- this caused significant churn -- I have a proposal in #1084 to make this more palatable when reintroducing it |
Revert "Merge pull request #1041 from asfaltboy/issue-830-e721-types-regex-incorrect"
tl;dr The PEP8 example for the issue only includes the
type(a) == type(b)
comparison, it does not include the== types.SomeType
issue, the latter should not be handled under this error check.This check was introduced to trunk following issue #47, and I believe it incorrectly borrowed the example from the issue literally. The E721 check was since reported on a few false-positive issues and then expanded a number of times to handle those around types (see 034b9f4 and 0327e55).
I believe the behaviour should change because object comparison cannot be confirmed a "type comparison" in the same sense; not without having additional information, which is not possible with regex alone (maybe with AST analysis).
This check also breaks many ligimitate cases (such as having your own types module with an Enum class for instance, and comparing a value to the enum member).
If the community wants to keep it, I strongly advise it be made a separate warning level advisory (W###) rather than overloading E721, so we can handle any logic around it separately.
For reference, I learned more about the pattern, and tested for the cases using regexr:
fixes issue #830