mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-29 21:35:58 +00:00

## Summary Implement [`no-isinstance-type-none`](https://github.com/dosisod/refurb/blob/master/refurb/checks/builtin/no_isinstance_type_none.py) as `isinstance-type-none` (`FURB168`). Auto-fixes calls to `isinstance` to check if an object is `None` to a `None` identity check. For example, ```python isinstance(foo, type(None)) ``` becomes ```python foo is None ``` Related to #1348. ## Test Plan `cargo test` --------- Co-authored-by: Charlie Marsh <charlie.r.marsh@gmail.com>
51 lines
884 B
Python
51 lines
884 B
Python
foo: object
|
|
|
|
# Errors.
|
|
|
|
if isinstance(foo, type(None)):
|
|
pass
|
|
|
|
if isinstance(foo, (type(None))):
|
|
pass
|
|
|
|
if isinstance(foo, (type(None), type(None), type(None))):
|
|
pass
|
|
|
|
if isinstance(foo, None | None):
|
|
pass
|
|
|
|
if isinstance(foo, (None | None)):
|
|
pass
|
|
|
|
if isinstance(foo, None | type(None)):
|
|
pass
|
|
|
|
if isinstance(foo, (None | type(None))):
|
|
pass
|
|
|
|
# A bit contrived, but is both technically valid and equivalent to the above.
|
|
if isinstance(foo, (type(None) | ((((type(None))))) | ((None | type(None))))):
|
|
pass
|
|
|
|
|
|
# Okay.
|
|
|
|
if isinstance(foo, int):
|
|
pass
|
|
|
|
if isinstance(foo, (int)):
|
|
pass
|
|
|
|
if isinstance(foo, (int, str)):
|
|
pass
|
|
|
|
if isinstance(foo, (int, type(None), str)):
|
|
pass
|
|
|
|
# This is a TypeError, which the rule ignores.
|
|
if isinstance(foo, None):
|
|
pass
|
|
|
|
# This is also a TypeError, which the rule ignores.
|
|
if isinstance(foo, (None,)):
|
|
pass
|