rapidfuzz / Levenshtein

The Levenshtein Python C extension module contains functions for fast computation of Levenshtein distance and string similarity
https://rapidfuzz.github.io/Levenshtein
GNU General Public License v2.0
276 stars 16 forks source link

Missing stubs for some functions #25

Closed atomflunder closed 2 years ago

atomflunder commented 2 years ago

Hello, I recently tried to upgrade Levenshtein to Version 0.19.1 and I noticed that using some functions will throw an error in the IDE using Pylance or Mypy.
Example using distance:

Levenshtein.distance("test", "another test")

The error message:

"distance" is not a known member of module

This is (I think) because the __init__.pyi file has stubs for some but not all available functions.
From what I can tell these are missing from the stub file:

distance, ratio, hamming, jaro, and jaro_winkler.

Would it be possible to add those, or are you recommending to just shut up Pylance by adding a # type: ignore comment on each line? Would you be open for a pull request to be submitted? Thanks in advance for answering!

maxbachmann commented 2 years ago

Thanks for reporting. I tried to add the type stubs. However I am unsure why it fails the type stub test for jaro and jaro_winkler: https://github.com/maxbachmann/Levenshtein/runs/7311103290?check_suite_focus=true. There should are type hints for them in rapidfuzz.

atomflunder commented 2 years ago

I am not quite sure either what the cause of the error might be. Maybe a stupid idea, but does the action maybe download an older version of the packages from pypi? If not, I think we could manually add the stubs to the __init__.pyi file, like the other functions already in there to please Mypy, however that does sound like a headache to maintain.

Like:

from typing Sequence, Hashable, Callable

...

def distance(
    s1: Sequence[Hashable],
    s2: Sequence[Hashable],
    processor: Optional[Callable] = None,
    score_cutoff: Optional[float] = None,
) -> int: ...
def ratio(
    s1: Sequence[Hashable],
    s2: Sequence[Hashable],
    processor: Optional[Callable] = None,
    score_cutoff: Optional[float] = None,
) -> float: ...
def hamming(
    s1: Sequence[Hashable],
    s2: Sequence[Hashable],
    processor: Optional[Callable] = None,
    score_cutoff: Optional[float] = None,
) -> int: ...
def jaro(
    s1: Sequence[Hashable],
    s2: Sequence[Hashable],
    processor: Optional[Callable] = None,
    score_cutoff: Optional[float] = None,
) -> float: ...
def jaro_winkler(
    s1: Sequence[Hashable],
    s2: Sequence[Hashable],
    processor: Optional[Callable] = None,
    score_cutoff: Optional[float] = None,
) -> float: ...

Again, probably not the ideal way to go about things. In any case, thanks for responding so quickly.

maxbachmann commented 2 years ago

I am not quite sure either what the cause of the error might be. Maybe a stupid idea, but does the action maybe download an older version of the packages from pypi?

no this appears to be the newest version

If not, I think we could manually add the stubs to the init.pyi file, like the other functions already in there to please Mypy, however that does sound like a headache to maintain.

Since those are not going to change a lot I guess this would be fine. Do you want to open a PR with these stubs?

atomflunder commented 2 years ago

Yes, I can do that.

maxbachmann commented 2 years ago

This is part of v0.19.2