Fix false positive bad-override when overriding methods using bounded TypeVars - #4869
Open
kavix wants to merge 1 commit into
Open
Fix false positive bad-override when overriding methods using bounded TypeVars#4869kavix wants to merge 1 commit into
kavix wants to merge 1 commit into
Conversation
kavix
force-pushed
the
fix-bad-override-typevar
branch
from
September 9, 2026 09:46
2d854c0 to
48ded4b
Compare
… TypeVars When checking override compatibility between generic methods, type variables in the child method signature are instantiated with fresh inference variables and matched contravariantly against the parent parameters. When a parameter is a union like `T | None`, `is_subset_eq` previously attempted to check whether the `TypeVar` bound was a subtype of the union before splitting the RHS union. When the RHS union contained an unsolved inference variable, the speculative check succeeded under a snapshot and rolled back all variable assignments, leaving the child type variable unconstrained and causing override validation to fail. To fix this, check `Type::Quantified` against individual members of a RHS union first before falling back to testing whether the `TypeVar` bound or constraints as a whole satisfy the union. This allows inference variables in the RHS union to be properly constrained to the parent type variable, correctly validating method overrides.
kavix
force-pushed
the
fix-bad-override-typevar
branch
from
September 9, 2026 09:48
48ded4b to
526bcca
Compare
|
Diff from mypy_primer, showing the effect of this PR on open source code: ============================================================
SUMMARY
============================================================
Total: +4 new errors, -10 fixed errors
By preset: +3/-6 (default), +3/-6 (strict)
Projects with changes (6):
altair: +0 -1
scikit-build-core: +2 -1
starlette: +0 -1
pytest-autoprofile: +1 -3
core: +1 -3
artigraph: +0 -1
============================================================
FULL DIFF DETAILS
------------------------------------------------------------
altair (https://github.com/vega/altair)
- ERROR altair/vegalite/v6/api.py:1088:43-63: The type of this argument is unknown [unknown-argument-type]
scikit-build-core (https://github.com/scikit-build/scikit-build-core)
- ERROR src/scikit_build_core/metadata/__init__.py:145:16-148:10: Returned type `dict[Unknown, dict[str, str]]` is not assignable to declared return type `T` [bad-return]
+ ERROR src/scikit_build_core/metadata/__init__.py:145:16-148:10: Returned type `dict[str, dict[str, str]]` is not assignable to declared return type `T` [bad-return]
+ ERROR src/scikit_build_core/metadata/__init__.py:146:64-65: Unused `# type: ignore` comment [unused-type-ignore]
starlette (https://github.com/encode/starlette)
- ERROR starlette/concurrency.py:34:43-47: Argument `(ParamSpec(P)) -> T` is not assignable to parameter `func` with type `(**tuple[*@_]) -> @_` in function `anyio.to_thread.run_sync` [bad-argument-type]
pytest-autoprofile (https://gitlab.com/TTsangSC/pytest-autoprofile)
- ERROR tests/test-modules-and-tests/test_subprocess/packages/myprocess.py:89:25-31: `(ParamSpec(PS)) -> T` is not assignable to attribute `func` with type `() -> T` [bad-assignment]
- ERROR src/pytest_autoprofile/_patches.py:721:16-28: Returned type `_ExtraArgWrapper[(ParamSpec(PS)) -> T, SerializedStash] | _ExtraArgWrapper[(ParamSpec(PS)) -> T, AutoProfStash] | _PureWrapper[(ParamSpec(PS)) -> T]` is not assignable to declared return type `_ExtraArgWrapper[Any, SerializedStash] | _ExtraArgWrapper[Any, AutoProfStash] | _PureWrapper[Any]` [bad-return]
+ ERROR src/pytest_autoprofile/_patches.py:721:16-28: Returned type `_PureWrapper[(ParamSpec(PS)) -> T]` is not assignable to declared return type `_ExtraArgWrapper[Any, SerializedStash] | _ExtraArgWrapper[Any, AutoProfStash] | _PureWrapper[Any]` [bad-return]
- ERROR src/pytest_autoprofile/importers.py:1656:16-20: Returned type `(ParamSpec(PS)) -> T` is not assignable to declared return type `() -> T` [bad-return]
core (https://github.com/home-assistant/core)
- ERROR homeassistant/components/asuswrt/helpers.py:42:16-70: Returned type `dict[str | Unknown, Unknown]` is not assignable to declared return type `T` [bad-return]
+ ERROR homeassistant/components/asuswrt/helpers.py:42:16-70: Returned type `dict[str, Any]` is not assignable to declared return type `T` [bad-return]
- ERROR homeassistant/components/asuswrt/helpers.py:42:37-38: The type of this argument is unknown [unknown-argument-type]
- ERROR homeassistant/components/asuswrt/helpers.py:42:40-41: The type of this argument is unknown [unknown-argument-type]
artigraph (https://github.com/artigraph/artigraph)
- ERROR src/arti/internal/type_hints.py:172:46-53: The type of this argument is unknown [unknown-argument-type] |
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.
Summary
Fixes #4847
When checking override compatibility between generic methods, type variables in the child method signature are instantiated with fresh inference variables (
v_T) and matched contravariantly against the parent method's parameters.When a parameter is a union like
T | None,is_subset_eq_no_recursive_checkpreviously attempted to check whether theTypeVar's bound was a subtype of the union before splitting the RHS union. When the RHS union contained an unsolved inference variable, the speculative check succeeded underwith_snapshotand rolled back all variable assignments, leaving the child method's type variable unconstrained and causing override validation to fail with a false-positivebad-override.This PR:
Type::Quantifiedchecks inis_subset_eq_no_recursive_check.(l, Type::Union(u_union)), when checking whether aType::Quantifiedis a subtype of a union, each member of the RHS union is checked first. If none match, a fallback verifies whether theTypeVar's bound or constraints as a whole satisfy the union (preserving the intended behavior for constrained and union-bounded type parameters).(Type::Quantified(q), u)resolution acrossRestriction::Bound,Restriction::Constraints, andRestriction::Unrestricted.Test Plan
TypeVars, constrainedTypeVars, and PEP 695 syntax inpyrefly/lib/test/class_overrides.rs.cargo test test::class_overridescargo test test::generic_restrictionscargo test test_genericpython3 test.py --no-test --no-tensor-shapes --no-conformance --no-jsonschema