diff options
author | Nick Drozd <nicholasdrozd@gmail.com> | 2023-02-14 16:39:57 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-02-14 22:39:57 +0100 |
commit | e64f0437388298c7f8514a6755c3c27a0d9a35f2 (patch) | |
tree | 115bd308fcad5e5665b37b32455393c529fed738 /pylint/checkers | |
parent | b8f2b5498656b5fe5b94b6c9da90f76677478701 (diff) | |
download | pylint-git-e64f0437388298c7f8514a6755c3c27a0d9a35f2.tar.gz |
Fix invalid type false positive (#8206)
Diffstat (limited to 'pylint/checkers')
-rw-r--r-- | pylint/checkers/typecheck.py | 15 |
1 files changed, 14 insertions, 1 deletions
diff --git a/pylint/checkers/typecheck.py b/pylint/checkers/typecheck.py index bfd415923..8448a0f13 100644 --- a/pylint/checkers/typecheck.py +++ b/pylint/checkers/typecheck.py @@ -48,6 +48,7 @@ from pylint.checkers.utils import ( supports_membership_test, supports_setitem, ) +from pylint.constants import PY310_PLUS from pylint.interfaces import HIGH, INFERENCE from pylint.typing import MessageDefinitionTuple @@ -796,6 +797,10 @@ def _is_c_extension(module_node: InferenceResult) -> bool: def _is_invalid_isinstance_type(arg: nodes.NodeNG) -> bool: # Return True if we are sure that arg is not a type + if PY310_PLUS and isinstance(arg, nodes.BinOp) and arg.op == "|": + return _is_invalid_isinstance_type(arg.left) or _is_invalid_isinstance_type( + arg.right + ) inferred = utils.safe_infer(arg) if not inferred: # Cannot infer it so skip it. @@ -806,6 +811,10 @@ def _is_invalid_isinstance_type(arg: nodes.NodeNG) -> bool: return False if isinstance(inferred, astroid.Instance) and inferred.qname() == BUILTIN_TUPLE: return False + if PY310_PLUS and isinstance(inferred, bases.UnionType): + return _is_invalid_isinstance_type( + inferred.left + ) or _is_invalid_isinstance_type(inferred.right) return True @@ -1398,7 +1407,11 @@ accessed. Python regular expressions are accepted.", second_arg = node.args[1] if _is_invalid_isinstance_type(second_arg): - self.add_message("isinstance-second-argument-not-valid-type", node=node) + self.add_message( + "isinstance-second-argument-not-valid-type", + node=node, + confidence=INFERENCE, + ) # pylint: disable = too-many-branches, too-many-locals, too-many-statements def visit_call(self, node: nodes.Call) -> None: |