pygensuggestions
A backport of the _suggestions module, native to CPython since 3.12, to other versions
of Python.
Quickstart
uv pip install pygensuggestions==2.3.3 # uv
uv pip install git+https://github.com/jonathandung/pygensuggestions.git
# directly from repo
pip install pygensuggestions==2.3.3 # pip
Less common pathways:
conda install -c conda-forge pygensuggestions==2.3.3 # conda: method 1
conda config --add channels conda-forge
conda config --set channel_priority strict
conda install pygensuggestions==2.3.3 # conda: method 2
pipx install pygensuggestions==2.3.3 # pipx
poetry add pygensuggestions@2.3.3 # poetry
pdm add pygensuggestions==2.3.3 # pdm
pipenv install pygensuggestions=2.3.3 # pipenv
Usage
>>> import pygensuggestions # the module
>>> pygensuggestions.suggest( # the primary function
... (
... 'foo',
... 'bar',
... 'baz',
... ), # arg 1: the sized iterable over words from which suggestions are taken
... 'bar', # arg 2: the wrong word
... )
... # notice that the exact word, if present in the sequence, is not returned by default
'baz'
>>> pygensuggestions.suggest(['abcd', 'efgh'], 'zyxd')
... # Returns None, because the target is too far from the candidates
>>> pygensuggestions.suggest(
... {b'red', b'blue', b'yellow'}, b'blew'
... ) # all bytes are also OK, as long as data is homogeneous
b'blue'
Command line usage
$ echo "thousand
> hundred" > moreopts.txt
$ # read candidates from arguments and optionally a newline-delimited file
$ pygensuggestions tousand million atousend @moreopts.txt --outfile res.out
$ echo $?
0
$ cat res.out # print the result
thousand
Interpretation of exit codes
- 0: a suggestion was successfully found
- 1: no suggestion was close enough
- 2: incorrect (combination of) command-line arguments, including if there isn't at
least 1 target and 1 candidate (returned by
argparse.ArgumentParser.error) - 3: the target string exceeds 40 characters and
--strictor-swas passed - 4: the number of candidates is greater than 750 and
--strictor-swas passed
Background
The _suggestions module was implemented in C, at Modules/_suggestions.c, as part of
an attempt to improve user experience by enriching the traceback dump of some errors
regarding nonexistent attribute or module names closely resembling a known module or
attribute, as well as apparently mistyped keywords. It contains a helper internal to
the Python interpreter, as signified by the underscore-prefixed name, called
_generate_suggestions, which takes an exact instance of a list as the first argument
and a string as the second, and returns the string most similar to the target in the
list, or None if a certain threshold determined by a Levenshtein distance-based
metric with weighted move and case costs is not reached, or there are too many strings
in the list.
What does this library do?
This library provides a faithful translation of that sophisticated deterministic engine to pure Python, along with a simple command-line interface to call this core function from the shell. It is incredibly simple and custom wrappers should be built upon it for it to really shine. In this context, "deterministic" is sensitive to the order in which items appear in the sequence.
It also boasts maximum portability. It supports Python 3.8 and above out-of-the-box,
and is implementation-agnostic. It also can take any sized iterable of candidates,
which can be str or bytes as long as it is consistent with the type of the target,
as opposed to typical implementations that only accept sequences.
The two functions used to assist in the implementation, lev_dist and sub_cost, are
exposed in the lib submodule.
Why pygensuggestions?
Indeed, alternatives to this module exist. Their shortfalls are detailed below.
The _suggestions module itself is, of course, a contender. However, it only accepts
strict instances of list for the first argument, and both arguments cannot have
bytes. Though it is the fastest because it is written in C, it is not public and not
available on older Python versions.
difflib is more potent and configurable, but it serves a different purpose and
outputs in a different format. It is not as fast as this for the specific purpose we
target.
While the traceback module does implement this in pure Python as a fallback, it is
again in the form of an unstable, private function, and is not available on all
versions of Python.
I have yet to find modules on the Python Package Index providing comparable functionality to this module. It is not to say they are too simple; in fact, the algorithms used may be overkill for some, or wrapped in unrelated logic. The routine actually used by CPython, the reference implementation of Python, is likely only accessible with such transparency here.
Situations in which suggest returns None
- When you pass too many candidates (>750), since this library mirrors the Python
source. If you must bypass this, set
lib.MAX_CANDIDATE_ITEMStofloat('inf'), suppressing type checker complaints, or passrespect_bounds=False. - When the length of the target string exceeds 40 characters, for the same reason. Set
lib.MAX_STRING_SIZEtofloat('inf')or passrespect_bounds=Falseto alter this behaviour. - More than one-third of characters require modification for any candidate chosen.
License
MIT © 2026 Jonathan Dung
Metadata
Release files for pygensuggestions 2.3.3
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| pygensuggestions-2.3.3.tar.gz | 9.3 kB | Without provenance | Details for pygensuggestions-2.3.3.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| pygensuggestions-2.3.3-py3-none-any.whl | Python 3 | none | any | Details for pygensuggestions-2.3.3-py3-none-any.whl |
Total release size: 23.6 kB
Release files / pygensuggestions-2.3.3.tar.gz
| Download URL | pygensuggestions-2.3.3.tar.gz |
|---|---|
| Size | 9.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
ee5d6158ecbcc81bafe514a12ff958d9c8d0ca323f3ebf898db490a26035a3f5
|
|
BLAKE2b-256 checksum How to use checksums |
e3d34e9fea1c319b997b48e87d2944808ee164ed7a7b3f131d85375f1d7b3b88
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
uv/0.13.9 {"installer":{"name":"uv","version":"0.13.9","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"26.04","id":"resolute","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|
Release files / pygensuggestions-2.3.3-py3-none-any.whl
| Download URL | pygensuggestions-2.3.3-py3-none-any.whl |
|---|---|
| Size | 14.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
52e21a3ae7e5340595080ec8bb6338bc2dbbcf82c6ad9c793344179a9b6bbe77
|
|
BLAKE2b-256 checksum How to use checksums |
064f559e761c7926b4a742483239e850ad1eeb9995843e9cf214682db82654dd
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
uv/0.13.9 {"installer":{"name":"uv","version":"0.13.9","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"26.04","id":"resolute","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|
Security for pygensuggestions 2.3.3
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.