-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Add suggestions for misspelled module imports #20695
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
Draft
KevinRK29
wants to merge
10
commits into
python:master
Choose a base branch
from
KevinRK29:fuzzy-matching-import-statements
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+220
−0
Draft
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
96528b1
added known list of modules to use for fuzzy matching
KevinRK29 2bf3bb4
added module not found note with suggestions
KevinRK29 dba232a
added test for missing module
KevinRK29 1f601c1
trim packages to 100 packages and add module cache
KevinRK29 2b33bdb
reset known modules cache
KevinRK29 5eed022
gate feature with prefer_simple_messages
KevinRK29 adcd9f4
filter private modules
KevinRK29 53dba77
filter short modules
KevinRK29 1be3750
avoid suggestions to fix internal/private modules
KevinRK29 db58363
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,174 @@ | ||
| """Known Python module names for fuzzy matching import suggestions. | ||
|
|
||
| This module provides a curated list of popular Python package import names | ||
| for suggesting corrections when a user mistypes an import statement. | ||
|
|
||
| Sources: | ||
| - Python standard library (typeshed/stdlib/VERSIONS) | ||
| - Top 100 PyPI packages by downloads (https://github.com/hugovk/top-pypi-packages) | ||
|
|
||
| Note: These are import names, not PyPI package names. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from typing import Final | ||
|
|
||
| from mypy.modulefinder import StdlibVersions | ||
|
|
||
| POPULAR_THIRD_PARTY_MODULES: Final[frozenset[str]] = frozenset( | ||
| { | ||
| # Cloud | ||
| "boto3", | ||
| "botocore", | ||
| "aiobotocore", | ||
| "s3transfer", | ||
| "s3fs", | ||
| "awscli", | ||
| # HTTP / Networking | ||
| "urllib3", | ||
| "requests", | ||
| "certifi", | ||
| "idna", | ||
| "charset_normalizer", | ||
| "httpx", | ||
| "httpcore", | ||
| "aiohttp", | ||
| "yarl", | ||
| "multidict", | ||
| "requests_oauthlib", | ||
| "oauthlib", | ||
| "h11", | ||
| # Typing / Extensions | ||
| "typing_extensions", | ||
| "annotated_types", | ||
| "typing_inspection", | ||
| # Core Utilities | ||
| "setuptools", | ||
| "packaging", | ||
| "pip", | ||
| "wheel", | ||
| "virtualenv", | ||
| "platformdirs", | ||
| "filelock", | ||
| "zipp", | ||
| "importlib_metadata", | ||
| # Data Science / Numerical | ||
| "numpy", | ||
| "pandas", | ||
| "scipy", | ||
| "pyarrow", | ||
| # Serialization / Config | ||
| "yaml", | ||
| "pydantic", | ||
| "pydantic_core", | ||
| "attrs", | ||
| "tomli", | ||
| "jsonschema", | ||
| "jsonschema_specifications", | ||
| "jmespath", | ||
| # Cryptography / Security | ||
| "cryptography", | ||
| "cffi", | ||
| "pycparser", | ||
| "rsa", | ||
| "pyjwt", | ||
| "pyasn1", | ||
| "pyasn1_modules", | ||
| # Date / Time | ||
| "dateutil", | ||
| "pytz", | ||
| "tzdata", | ||
| # Google / gRPC | ||
| "google", | ||
| "grpc", | ||
| "grpc_status", | ||
| "grpc_tools", | ||
| "protobuf", | ||
| "googleapis_common_protos", | ||
| # Testing | ||
| "pytest", | ||
| "pluggy", | ||
| "iniconfig", | ||
| # CLI / Terminal | ||
| "click", | ||
| "colorama", | ||
| "rich", | ||
| "tqdm", | ||
| # Web Frameworks | ||
| "starlette", | ||
| # Templates / Markup | ||
| "jinja2", | ||
| "markupsafe", | ||
| "pygments", | ||
| "markdown_it", | ||
| "mdurl", | ||
| # Async | ||
| "anyio", | ||
| "greenlet", | ||
| "aiosignal", | ||
| "aiohappyeyeballs", | ||
| "frozenlist", | ||
| # Database | ||
| "sqlalchemy", | ||
| # Parsing / XML | ||
| "pyparsing", | ||
| "et_xmlfile", | ||
| # OpenTelemetry | ||
| "opentelemetry", | ||
| # Other Popular Modules | ||
| "six", | ||
| "fsspec", | ||
| "wrapt", | ||
| "propcache", | ||
| "rpds", | ||
| "pathspec", | ||
| "PIL", | ||
| "psutil", | ||
| "referencing", | ||
| "trove_classifiers", | ||
| "openpyxl", | ||
| "dotenv", | ||
| "yandexcloud", | ||
| "cachetools", | ||
| } | ||
| ) | ||
|
|
||
|
|
||
| _known_modules_cache: frozenset[str] | None = None | ||
|
|
||
|
|
||
| def reset_known_modules_cache() -> None: | ||
| global _known_modules_cache | ||
| _known_modules_cache = None | ||
|
|
||
|
|
||
| def get_stdlib_modules( | ||
| stdlib_versions: StdlibVersions, python_version: tuple[int, int] | None = None | ||
| ) -> frozenset[str]: | ||
| modules: set[str] = set() | ||
| for module, (min_ver, max_ver) in stdlib_versions.items(): | ||
| if python_version is not None: | ||
| if python_version < min_ver: | ||
| continue | ||
| if max_ver is not None and python_version > max_ver: | ||
| continue | ||
| top_level = module.split(".")[0] | ||
| # Skip private and very short modules to avoid false positives and noise | ||
| if top_level.startswith("_") or len(top_level) <= 2: | ||
| continue | ||
| modules.add(top_level) | ||
| return frozenset(modules) | ||
|
|
||
|
|
||
| def get_known_modules( | ||
| stdlib_versions: StdlibVersions | None = None, python_version: tuple[int, int] | None = None | ||
| ) -> frozenset[str]: | ||
| global _known_modules_cache | ||
| if _known_modules_cache is not None: | ||
| return _known_modules_cache | ||
| modules: set[str] = set(POPULAR_THIRD_PARTY_MODULES) | ||
| if stdlib_versions is not None: | ||
| modules = modules.union(get_stdlib_modules(stdlib_versions, python_version)) | ||
| _known_modules_cache = frozenset(modules) | ||
| return _known_modules_cache |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about
from numpyy import x-- does it also generate the note? From imports are more common thanimport x.