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

fix(compare_singleton_primitives_by_is): compare Name directly instead of QualifiedName #391

Merged
merged 1 commit into from
Oct 17, 2023
Merged
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
19 changes: 12 additions & 7 deletions src/fixit/rules/compare_singleton_primitives_by_is.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.

from typing import FrozenSet, Union
from typing import cast, FrozenSet, Union

import libcst as cst
from libcst.metadata import QualifiedName, QualifiedNameProvider, QualifiedNameSource
Expand Down Expand Up @@ -37,6 +37,8 @@ class CompareSingletonPrimitivesByIs(LintRule):
Valid("False is x"),
Valid("x == 2"),
Valid("2 != x"),
Valid('"True" == "True"'),
Valid('"True" != "False".lower()'),
]
INVALID = [
Invalid(
Expand Down Expand Up @@ -76,6 +78,14 @@ class CompareSingletonPrimitivesByIs(LintRule):
}
)

def is_singleton(self, node: cst.BaseExpression):
qual_name = cast(set, self.get_metadata(QualifiedNameProvider, node, set()))
return (
isinstance(node, cst.Name)
and qual_name
and qual_name < self.QUALIFIED_SINGLETON_PRIMITIVES
)

def visit_Comparison(self, node: cst.Comparison) -> None:
# Initialize the needs_report flag as False to begin with
needs_report = False
Expand All @@ -84,12 +94,7 @@ def visit_Comparison(self, node: cst.Comparison) -> None:
for target in node.comparisons:
operator, right_comp = target.operator, target.comparator
if isinstance(operator, (cst.Equal, cst.NotEqual)) and (
not self.QUALIFIED_SINGLETON_PRIMITIVES.isdisjoint(
self.get_metadata(QualifiedNameProvider, left_comp, set())
)
or not self.QUALIFIED_SINGLETON_PRIMITIVES.isdisjoint(
self.get_metadata(QualifiedNameProvider, right_comp, set())
)
self.is_singleton(left_comp) or self.is_singleton(right_comp)
):
needs_report = True
altered_comparisons.append(
Expand Down