Python: regenerate stale type stubs and fix typing imports in stub generator#7927
Merged
Conversation
… stub generator The `.pyi` stubs had drifted from their source modules — most visibly, `markers.pyi` declared `SearchResult` but omitted the `Markup*` classes added to `markers.py` two days after the stubs were last generated. Regenerating also surfaced a generator bug: `generate_stubs.py` hardcoded `from typing import Any, ClassVar, List, Optional` (plus optional `TypeVar, Generic`) and never imported other typing names (`Dict`, `Callable`, `Type`, `Iterable`, `IO`, or `Generic` used as a base class) that appear in annotations, producing stubs with unresolved names. - Detect typing names referenced in annotations and class bases, and import them - Exclude names shadowed by local definitions so collisions like `J.Literal`, `Py.TypeAlias`, and `JavaType.Union` are not wrongly imported from `typing` - Regenerate all stub files from current sources
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Motivation
The auto-generated
.pyitype stubs inrewrite-pythonhad drifted from their source modules. The most visible symptom:markers.pyideclaredSearchResultbut omitted allMarkup*classes (Markup,MarkupWarn,MarkupError,MarkupInfo,MarkupDebug), which were added tomarkers.pytwo days after the stubs were last regenerated. Regenerating showed 8 stub files were stale (nothing checks stub freshness, which is how they drifted).Regenerating also surfaced a pre-existing bug in
scripts/generate_stubs.py: it hardcodedfrom typing import Any, ClassVar, List, Optional(plus optionalTypeVar, Generic) and never imported othertypingnames actually used in annotations —Dict,Callable,Type,Iterable,IO, orGenericused as a base class — so the generated stubs referenced names that were never imported.Summary
collect_typing_names, which scans annotation contexts (fields, parameters, return types) and class base expressions (forGeneric[...]/Protocol[...]) for names intyping.__all__, and appends them to the header import — keepingAny, ClassVar, List, Optionalfirst andTypeVar, Genericlast to minimize diff churn.collect_defined_namesso a name shadowed by a local class/function/import (e.g.J.Literal,Py.TypeAlias,JavaType.Union— all collide withtyping.__all__) is not wrongly imported fromtyping. Names imported fromtypingin the source are preserved (they are exactly what the stub must re-import).markers.pyi,parser.pyi,tree.pyi,java/tree.pyi,java/support_types.pyi,python/markers.pyi,python/support_types.pyi,python/tree.pyi.Test plan
ast.parse).typingname referenced (inLoadcontext) in each stub is resolvable — either imported or defined locally as a class — verified via an AST check across all stubs.python scripts/generate_stubs.pyrun produces zero further changes.