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

Speed up the generation of no-member suggestions #10277

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
19 changes: 16 additions & 3 deletions pylint/checkers/typecheck.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,12 @@ def _(node: nodes.ClassDef | bases.Instance) -> Iterable[str]:
return itertools.chain(values, other_values)


def _string_distance(seq1: str, seq2: str) -> int:
seq2_length = len(seq2)
def _string_distance(seq1: str, seq2: str, seq1_length: int, seq2_length: int) -> int:
if not seq1_length:
return seq2_length

if not seq2_length:
return seq1_length

row = [*list(range(1, seq2_length + 1)), 0]
for seq1_index, seq1_char in enumerate(seq1):
Expand Down Expand Up @@ -182,11 +186,20 @@ def _similar_names(
possible_names: list[tuple[str, int]] = []
names = _node_names(owner)

attr_str = attrname or ""
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
attr_str = attrname or ""
return difflib.get_close_matches(attrname, possible_names, max_choices, distance_threshold)

attr_len = len(attr_str)

for name in names:
if name == attrname:
continue

distance = _string_distance(attrname or "", name)
name_len = len(name)

min_distance = abs(attr_len - name_len)
if min_distance > distance_threshold:
continue

distance = _string_distance(attr_str, name, attr_len, name_len)
if distance <= distance_threshold:
possible_names.append((name, distance))

Expand Down
44 changes: 44 additions & 0 deletions tests/checkers/unittest_typecheck.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,3 +221,47 @@ def decorated():
)
):
self.checker.visit_subscript(subscript)


class TestTypeCheckerStringDistance:
"""Tests for the _string_distance helper in pylint.checkers.typecheck."""

def test_string_distance_identical_strings(self) -> None:
seq1 = "hi"
seq2 = "hi"
assert typecheck._string_distance(seq1, seq2, len(seq1), len(seq2)) == 0

seq1, seq2 = seq2, seq1
assert typecheck._string_distance(seq1, seq2, len(seq1), len(seq2)) == 0

def test_string_distance_empty_string(self) -> None:
seq1 = ""
seq2 = "hi"
assert typecheck._string_distance(seq1, seq2, len(seq1), len(seq2)) == 2

seq1, seq2 = seq2, seq1
assert typecheck._string_distance(seq1, seq2, len(seq1), len(seq2)) == 2

def test_string_distance_edit_distance_one_character(self) -> None:
seq1 = "hi"
seq2 = "he"
assert typecheck._string_distance(seq1, seq2, len(seq1), len(seq2)) == 1

seq1, seq2 = seq2, seq1
assert typecheck._string_distance(seq1, seq2, len(seq1), len(seq2)) == 1

def test_string_distance_edit_distance_multiple_similar_characters(self) -> None:
seq1 = "hello"
seq2 = "yelps"
assert typecheck._string_distance(seq1, seq2, len(seq1), len(seq2)) == 3

seq1, seq2 = seq2, seq1
assert typecheck._string_distance(seq1, seq2, len(seq1), len(seq2)) == 3

def test_string_distance_edit_distance_all_dissimilar_characters(self) -> None:
seq1 = "yellow"
seq2 = "orange"
assert typecheck._string_distance(seq1, seq2, len(seq1), len(seq2)) == 6

seq1, seq2 = seq2, seq1
assert typecheck._string_distance(seq1, seq2, len(seq1), len(seq2)) == 6
Loading