Skip to content
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

Flag all comparisons against builtin types in E721 #8491

Merged
merged 5 commits into from
Nov 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@
if isinstance(res, memoryview):
pass
#: Okay
if type(res) is type:
pass
#: E721
if type(res) == type:
pass
#: Okay
def func_histype(a, b, c):
pass
#: E722
Expand Down
96 changes: 89 additions & 7 deletions crates/ruff_linter/src/rules/pycodestyle/rules/type_comparison.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,16 +198,98 @@ fn is_type(expr: &Expr, semantic: &SemanticModel) -> bool {
// Ex) `type(obj) == int`
matches!(
id.as_str(),
"int"
| "str"
| "float"
| "bool"
| "complex"
"bool"
| "bytearray"
| "bytes"
| "list"
| "classmethod"
| "complex"
| "dict"
| "set"
| "enumerate"
| "filter"
| "float"
| "frozenset"
| "int"
| "list"
| "map"
| "memoryview"
| "object"
| "property"
| "range"
| "reversed"
| "set"
| "slice"
| "staticmethod"
| "str"
| "super"
| "tuple"
| "type"
| "zip"
| "ArithmeticError"
| "AssertionError"
| "AttributeError"
| "BaseException"
| "BlockingIOError"
| "BrokenPipeError"
| "BufferError"
| "BytesWarning"
| "ChildProcessError"
| "ConnectionAbortedError"
| "ConnectionError"
| "ConnectionRefusedError"
| "ConnectionResetError"
| "DeprecationWarning"
| "EnvironmentError"
| "EOFError"
| "Exception"
| "FileExistsError"
| "FileNotFoundError"
| "FloatingPointError"
| "FutureWarning"
| "GeneratorExit"
| "ImportError"
| "ImportWarning"
| "IndentationError"
| "IndexError"
| "InterruptedError"
| "IOError"
| "IsADirectoryError"
| "KeyboardInterrupt"
| "KeyError"
| "LookupError"
| "MemoryError"
| "ModuleNotFoundError"
| "NameError"
| "NotADirectoryError"
| "NotImplementedError"
| "OSError"
| "OverflowError"
| "PendingDeprecationWarning"
| "PermissionError"
| "ProcessLookupError"
| "RecursionError"
| "ReferenceError"
| "ResourceWarning"
| "RuntimeError"
| "RuntimeWarning"
| "StopAsyncIteration"
| "StopIteration"
| "SyntaxError"
| "SyntaxWarning"
| "SystemError"
| "SystemExit"
| "TabError"
| "TimeoutError"
| "TypeError"
| "UnboundLocalError"
| "UnicodeDecodeError"
| "UnicodeEncodeError"
| "UnicodeError"
| "UnicodeTranslateError"
| "UnicodeWarning"
| "UserWarning"
| "ValueError"
| "Warning"
| "ZeroDivisionError"
) && semantic.is_builtin(id)
}
_ => false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,22 +149,22 @@ E721.py:41:8: E721 Do not compare types, use `isinstance()`
42 | #:
|

E721.py:101:12: E721 Do not compare types, use `isinstance()`
E721.py:107:12: E721 Do not compare types, use `isinstance()`
|
99 | def asdf(self, value: str | None):
100 | #: E721
101 | if type(value) is str:
105 | def asdf(self, value: str | None):
106 | #: E721
107 | if type(value) is str:
| ^^^^^^^^^^^^^^^^^^ E721
102 | ...
108 | ...
|

E721.py:111:12: E721 Do not compare types, use `isinstance()`
E721.py:117:12: E721 Do not compare types, use `isinstance()`
|
109 | def asdf(self, value: str | None):
110 | #: E721
111 | if type(value) is str:
115 | def asdf(self, value: str | None):
116 | #: E721
117 | if type(value) is str:
| ^^^^^^^^^^^^^^^^^^ E721
112 | ...
118 | ...
|


Original file line number Diff line number Diff line change
Expand Up @@ -119,4 +119,14 @@ E721.py:41:8: E721 Use `is` and `is not` for type comparisons, or `isinstance()`
42 | #:
|

E721.py:59:4: E721 Use `is` and `is not` for type comparisons, or `isinstance()` for isinstance checks
|
57 | pass
58 | #: E721
59 | if type(res) == type:
| ^^^^^^^^^^^^^^^^^ E721
60 | pass
61 | #: Okay
|


Loading