Skip to content

Fix false positive bad-override when overriding methods using bounded TypeVars - #4869

Open
kavix wants to merge 1 commit into
facebook:mainfrom
kavix:fix-bad-override-typevar
Open

Fix false positive bad-override when overriding methods using bounded TypeVars#4869
kavix wants to merge 1 commit into
facebook:mainfrom
kavix:fix-bad-override-typevar

Conversation

@kavix

@kavix kavix commented Sep 9, 2026

Copy link
Copy Markdown
Contributor

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_check previously attempted to check whether the TypeVar'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 under with_snapshot and rolled back all variable assignments, leaving the child method's type variable unconstrained and causing override validation to fail with a false-positive bad-override.

This PR:

  1. Removes the speculative pre-union Type::Quantified checks in is_subset_eq_no_recursive_check.
  2. In (l, Type::Union(u_union)), when checking whether a Type::Quantified is a subtype of a union, each member of the RHS union is checked first. If none match, a fallback verifies whether the TypeVar's bound or constraints as a whole satisfy the union (preserving the intended behavior for constrained and union-bounded type parameters).
  3. Unifies post-union (Type::Quantified(q), u) resolution across Restriction::Bound, Restriction::Constraints, and Restriction::Unrestricted.

Test Plan

  • Added integration tests covering module-level bounded TypeVars, constrained TypeVars, and PEP 695 syntax in pyrefly/lib/test/class_overrides.rs.
  • cargo test test::class_overrides
  • cargo test test::generic_restrictions
  • cargo test test_generic
  • python3 test.py --no-test --no-tensor-shapes --no-conformance --no-jsonschema

@meta-cla meta-cla Bot added the cla signed label Sep 9, 2026
@github-actions github-actions Bot added the size/s label Sep 9, 2026
@kavix
kavix force-pushed the fix-bad-override-typevar branch from 2d854c0 to 48ded4b Compare September 9, 2026 09:46
@github-actions github-actions Bot added size/s and removed size/s labels Sep 9, 2026
… 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
kavix force-pushed the fix-bad-override-typevar branch from 48ded4b to 526bcca Compare September 9, 2026 09:48
@github-actions github-actions Bot added size/s and removed size/s labels Sep 9, 2026
@github-actions

github-actions Bot commented Sep 9, 2026

Copy link
Copy Markdown

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]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bad-override false positive for module-level TypeVar

2 participants