Flag all comparisons against builtin types in E721 (#8491)
See #8483. Generalised fix on top of #8485 Based on the output of `print("\n".join(k for k, v in builtins.__dict__.items() if isinstance(v, type)))`
This commit is contained in:
@@ -53,6 +53,12 @@ if isinstance(res, types.MethodType):
|
||||
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
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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 | ...
|
||||
|
|
||||
|
||||
|
||||
|
||||
@@ -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
|
||||
|
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user