You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
We now infer and validate variance through direct, mutual, and expanding recursive protocol references. A recursive member no longer suppresses the variance mismatch in this protocol:
fromtypingimportProtocol, TypeVarT_co=TypeVar("T_co", covariant=True)
classSink(Protocol[T_co]): # error: should be contravariantdefwrite(self, value: T_co) ->None: ...
defnext(self) ->"Sink[T_co]": ...
We separate constructing variance equations from evaluating them. Recursive references name another parameter’s equation instead of expanding its definition, so even references like P[list[T]] produce a finite problem.
To validate a declaration, we solve mutually dependent parameters together, starting at bivariance and updating until the results stop changing. This prevents a protocol’s declared variance from serving as evidence for its own correctness. Declarations outside that recursive group remain authoritative. Dependencies also respect argument erasure: an alias that ignores its type argument does not create a recursive dependency through that argument. Ordinary type inference continues to honor declared variance, including for unused parameters.
The symbolic terms and fixed-point iteration follow the general approach described in the Rust compiler’s variance-inference documentation, adapted here to validating Python protocol declarations.
This also enables structural variance inference for recursive PEP 695 protocols, including invariance from writable attributes. Recursive type aliases and descriptor setters whose accepted values cannot be represented by a single type remain deferred.
Current numbers
The percentage of diagnostics emitted that were expected errors held steady at 97.79%. The percentage of expected errors that received a diagnostic held steady at 94.33%. The number of fully passing files held steady at 112/136.
Flaky changes detected. This PR summary excludes flaky changes; see the HTML report for details.
Raw diff:
antidote (https://github.com/Finistere/antidote)
+ src/antidote/core/__init__.py:1300:7 error[invalid-protocol] Type variable `T` in protocol `InjectedMethod` should be covariant, but is invariant
comtypes (https://github.com/enthought/comtypes)
+ comtypes/hints.pyi:236:7 error[invalid-protocol] Type variable `_R_Get` in protocol `_Descriptor` should be covariant, but is invariant+ comtypes/hints.pyi:236:7 error[invalid-protocol] Type variable `_T_Inst` in protocol `_Descriptor` should be contravariant, but is invariant
Some benchmarks with significant performance changes were compared across different runtime environments,
which may affect the accuracy of the results.
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.
Summary
We now infer and validate variance through direct, mutual, and expanding recursive protocol references. A recursive member no longer suppresses the variance mismatch in this protocol:
We separate constructing variance equations from evaluating them. Recursive references name another parameter’s equation instead of expanding its definition, so even references like
P[list[T]]produce a finite problem.To validate a declaration, we solve mutually dependent parameters together, starting at bivariance and updating until the results stop changing. This prevents a protocol’s declared variance from serving as evidence for its own correctness. Declarations outside that recursive group remain authoritative. Dependencies also respect argument erasure: an alias that ignores its type argument does not create a recursive dependency through that argument. Ordinary type inference continues to honor declared variance, including for unused parameters.
The symbolic terms and fixed-point iteration follow the general approach described in the Rust compiler’s variance-inference documentation, adapted here to validating Python protocol declarations.
This also enables structural variance inference for recursive PEP 695 protocols, including invariance from writable attributes. Recursive type aliases and descriptor setters whose accepted values cannot be represented by a single type remain deferred.