Resolve bare Protocol and Generic references in type position - #22005
Open
istoolsfox wants to merge 1 commit into
Open
istoolsfox wants to merge 1 commit into
istoolsfox wants to merge 1 commit into
Conversation
Typeshed declares the Protocol and Generic special forms as X: type[_X], mirroring the runtime, where both are classes on 3.12+. Mypy already uses those declarations when the names appear in expression context, but a bare reference in type position still failed with 'Variable ... is not valid as a type', so e.g. 'x: type[Protocol] = Protocol' was rejected. Resolve a bare reference to Instance(_Protocol)/Instance(_Generic), matching what the declarations say the runtime objects are. If the private classes are not defined in the stubs (before the typeshed sync lands), keep the regular 'not valid as a type' error instead of resolving to Any, so behavior only changes together with the stubs. References with arguments (Protocol[T]) keep failing as before, and class bases are unaffected (handled separately in semanal). The expression-context reproducer from python#21940 is already fixed by the typeshed declaration change itself; this handles the remaining type-position gap. Pyright currently rejects these examples too, so the divergence may be worth a look on the maintainer side. Refs python#21940
Contributor
|
According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅ |
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.
Follow-up to the typeshed declaration changes:
Protocolis now declared astype[_Protocol](python/typeshed#16386), the same shapeGenerichas had since python/typeshed#14583. Both mirror the runtime, where the two names are classes on 3.12+.Mypy already honors those declarations in expression context --
reveal_type(Protocol)givestype[typing._Protocol]with the synced stubs -- but a bare reference in type position still failed:This resolves the bare reference to
Instance(_Protocol)/Instance(_Generic), so the example checks clean once the stub sync lands. Details:Protocol[T]in type position) keep failing as before -- that is class-base syntax, not a type. Empty subscripts (Protocol[()]) also keep failing.Any, so behavior changes only together with the stub declarations -- safe to merge in either order relative to the typeshed sync.typing.Protocolis explicitly treated as not atype#21940 is already fixed by the stub change itself; this closes the remaining type-position gap.The unit-test fixtures declared
Generic = 0/Protocol = 0, which would silently change meaning under the new branch, so the typing fixtures now mirror the real declarations.One divergence worth flagging: pyright 1.1.414 rejects these examples too (
reportInvalidTypeForm), andTypeVar("T", bound=Protocol)gets a nearly-empty upper bound under this change. I went with "the typeshed declaration is the contract"; happy to adjust if bare references in type position should keep being rejected.