Skip to content

Check TypeVar restrictions when the expected type is gradual - #4506

Open
alexander-beedie wants to merge 1 commit into
facebook:mainfrom
alexander-beedie:restrictions-under-gradual-hint
Open

Check TypeVar restrictions when the expected type is gradual#4506
alexander-beedie wants to merge 1 commit into
facebook:mainfrom
alexander-beedie:restrictions-under-gradual-hint

Conversation

@alexander-beedie

@alexander-beedie alexander-beedie commented Aug 11, 2026

Copy link
Copy Markdown
Contributor

Summary

Fixes #3525.

Overview

A gradual annotation on a constructor call silently disabled bound and constraint checking:

class Container[T: B]:
    def __init__(self, x: T) -> None: ...

def f(x: A) -> Container[Any]:
    return Container(x)  # accepted, but `A` *doesn't* satisfy `T: B`

Remove the annotation and the error is caught...

Explanation

Applying the hint compares Container[?T] against Container[Any], which pushes Any into ?T; this was then treated that as a solution and locked in, so x: T degraded to x: Any and accepted anything 👀

Solution

An Any from the expected type says nothing, so it is discarded, leaving the parameter free for argument matching to check normally (note that an Any from an actual argument is still a real solution).

Test Plan

  • All existing unit tests pass, several new tests were added.

  • Multiple new matches relating to this are now found in the mypy_primer corpus; I've verified a sample of them, and they appeared to be correct (agreeing with pyright, which flags the same lines).

@meta-codesync

meta-codesync Bot commented Aug 11, 2026

Copy link
Copy Markdown
Contributor

This pull request has been imported. If you are a Meta employee, you can view this in D115555748. (Because this pull request was imported automatically, there will not be any future comments.)

@github-actions

Copy link
Copy Markdown

Diff from mypy_primer, showing the effect of this PR on open source code:

egglog-python (https://github.com/egraphs-good/egglog-python)
+ ERROR python/egglog/egraph.py:2358:30-58: `int` is not assignable to upper bound `ComparableAddSub` of type variable `DAG_COST` [bad-specialization]

schemathesis (https://github.com/schemathesis/schemathesis)
+ ERROR src/schemathesis/specs/openapi/operations.py:95:54-103:10: `None` is not assignable to upper bound `ResponsesContainer[Unknown]` of type variable `R` [bad-specialization]

static-frame (https://github.com/static-frame/static-frame)
+ ERROR static_frame/core/index.py:602:34-608:10: `Self@Index` is not assignable to any of constraints `Frame`, `Series`, `Bus`, `Quilt`, `Yarn` of type variable `TContainerAny` [bad-specialization]
+ ERROR static_frame/core/index_hierarchy.py:1359:34-1365:10: `Self@IndexHierarchy` is not assignable to any of constraints `Frame`, `Series`, `Bus`, `Quilt`, `Yarn` of type variable `TContainerAny` [bad-specialization]

pandas (https://github.com/pandas-dev/pandas)
+ ERROR pandas/tests/frame/methods/test_to_csv.py:1420:25-48: Argument `NaTType | Timestamp` is not assignable to parameter `left` with type `Timestamp` in function `pandas._libs.interval.Interval.__init__` [bad-argument-type]
+ ERROR pandas/tests/frame/methods/test_to_csv.py:1421:25-48: Argument `NaTType | Timestamp` is not assignable to parameter `right` with type `Timestamp` in function `pandas._libs.interval.Interval.__init__` [bad-argument-type]

meson (https://github.com/mesonbuild/meson)
+ ERROR mesonbuild/mparser.py:725:36-70: `None` is not assignable to any of constraints `int`, `str`, `bool` of type variable `TV_TokenTypes` [bad-specialization]
+ ERROR mesonbuild/mparser.py:751:33-178: `None` is not assignable to any of constraints `int`, `str`, `bool` of type variable `TV_TokenTypes` [bad-specialization]
+ ERROR unittests/versiontests.py:302:48-49: Argument `Literal[3]` is not assignable to parameter `min` with type `@_ | None` in function `mesonbuild.utils.universal.Range.__init__` [bad-argument-type]
+ ERROR unittests/versiontests.py:303:48-50: Argument `Literal[12]` is not assignable to parameter `max` with type `@_ | None` in function `mesonbuild.utils.universal.Range.__init__` [bad-argument-type]
+ ERROR unittests/versiontests.py:307:48-49: Argument `Literal[5]` is not assignable to parameter `min` with type `@_ | None` in function `mesonbuild.utils.universal.Range.__init__` [bad-argument-type]
+ ERROR unittests/versiontests.py:307:68-70: Argument `Literal[10]` is not assignable to parameter `max` with type `@_ | None` in function `mesonbuild.utils.universal.Range.__init__` [bad-argument-type]
+ ERROR unittests/versiontests.py:310:49-51: Argument `Literal[11]` is not assignable to parameter `min` with type `@_ | None` in function `mesonbuild.utils.universal.Range.__init__` [bad-argument-type]
+ ERROR unittests/versiontests.py:311:49-50: Argument `Literal[4]` is not assignable to parameter `max` with type `@_ | None` in function `mesonbuild.utils.universal.Range.__init__` [bad-argument-type]
+ ERROR unittests/versiontests.py:314:50-51: Argument `Literal[7]` is not assignable to parameter `min` with type `@_ | None` in function `mesonbuild.utils.universal.Range.__init__` [bad-argument-type]
+ ERROR unittests/versiontests.py:315:50-51: Argument `Literal[8]` is not assignable to parameter `max` with type `@_ | None` in function `mesonbuild.utils.universal.Range.__init__` [bad-argument-type]
+ ERROR unittests/versiontests.py:318:50-51: Argument `Literal[5]` is not assignable to parameter `min` with type `@_ | None` in function `mesonbuild.utils.universal.Range.__init__` [bad-argument-type]
+ ERROR unittests/versiontests.py:319:50-52: Argument `Literal[10]` is not assignable to parameter `max` with type `@_ | None` in function `mesonbuild.utils.universal.Range.__init__` [bad-argument-type]
+ ERROR unittests/versiontests.py:322:54-55: Argument `Literal[5]` is not assignable to parameter `min` with type `@_ | None` in function `mesonbuild.utils.universal.Range.__init__` [bad-argument-type]
+ ERROR unittests/versiontests.py:327:49-50: Argument `Literal[5]` is not assignable to parameter `min` with type `@_ | None` in function `mesonbuild.utils.universal.Range.__init__` [bad-argument-type]

core (https://github.com/home-assistant/core)
+ ERROR homeassistant/components/miele/sensor.py:209:29-34: Type of lambda parameter `value` is unknown [implicit-any-lambda]
+ ERROR homeassistant/components/miele/sensor.py:237:29-34: Type of lambda parameter `value` is unknown [implicit-any-lambda]
+ ERROR homeassistant/components/miele/sensor.py:246:29-34: Type of lambda parameter `value` is unknown [implicit-any-lambda]
+ ERROR homeassistant/components/miele/sensor.py:274:29-34: Type of lambda parameter `value` is unknown [implicit-any-lambda]
+ ERROR homeassistant/components/miele/sensor.py:301:29-34: Type of lambda parameter `value` is unknown [implicit-any-lambda]
+ ERROR homeassistant/components/miele/sensor.py:319:29-34: Type of lambda parameter `value` is unknown [implicit-any-lambda]
+ ERROR homeassistant/components/miele/sensor.py:340:24-29: Type of lambda parameter `value` is unknown [implicit-any-lambda]
+ ERROR homeassistant/components/miele/sensor.py:359:29-34: Type of lambda parameter `value` is unknown [implicit-any-lambda]
+ ERROR homeassistant/components/miele/sensor.py:377:24-29: Type of lambda parameter `value` is unknown [implicit-any-lambda]
+ ERROR homeassistant/components/miele/sensor.py:396:29-34: Type of lambda parameter `value` is unknown [implicit-any-lambda]
+ ERROR homeassistant/components/miele/sensor.py:422:29-34: Type of lambda parameter `value` is unknown [implicit-any-lambda]
+ ERROR homeassistant/components/miele/sensor.py:422:54-80: The type of this argument is unknown [unknown-argument-type]
+ ERROR homeassistant/components/miele/sensor.py:448:29-34: Type of lambda parameter `value` is unknown [implicit-any-lambda]
+ ERROR homeassistant/components/miele/sensor.py:448:54-78: The type of this argument is unknown [unknown-argument-type]
+ ERROR homeassistant/components/miele/sensor.py:476:29-34: Type of lambda parameter `value` is unknown [implicit-any-lambda]
+ ERROR homeassistant/components/miele/sensor.py:476:54-76: The type of this argument is unknown [unknown-argument-type]
+ ERROR homeassistant/components/miele/sensor.py:506:29-34: Type of lambda parameter `value` is unknown [implicit-any-lambda]
+ ERROR homeassistant/components/miele/sensor.py:507:17-43: The type of this argument is unknown [unknown-argument-type]
+ ERROR homeassistant/components/miele/sensor.py:507:45-67: The type of this argument is unknown [unknown-argument-type]
+ ERROR homeassistant/components/miele/sensor.py:532:29-34: Type of lambda parameter `value` is unknown [implicit-any-lambda]
+ ERROR homeassistant/components/miele/sensor.py:533:17-41: The type of this argument is unknown [unknown-argument-type]
+ ERROR homeassistant/components/miele/sensor.py:533:43-65: The type of this argument is unknown [unknown-argument-type]
+ ERROR homeassistant/components/miele/sensor.py:565:29-34: Type of lambda parameter `value` is unknown [implicit-any-lambda]
+ ERROR homeassistant/components/miele/sensor.py:565:57-81: The type of this argument is unknown [unknown-argument-type]
+ ERROR homeassistant/components/miele/sensor.py:583:29-34: Type of lambda parameter `value` is unknown [implicit-any-lambda]
+ ERROR homeassistant/components/miele/sensor.py:583:57-81: The type of this argument is unknown [unknown-argument-type]
+ ERROR homeassistant/components/miele/sensor.py:600:29-34: Type of lambda parameter `value` is unknown [implicit-any-lambda]
+ ERROR homeassistant/components/miele/sensor.py:600:57-81: The type of this argument is unknown [unknown-argument-type]
+ ERROR homeassistant/components/miele/sensor.py:616:29-34: Type of lambda parameter `value` is unknown [implicit-any-lambda]
+ ERROR homeassistant/components/miele/sensor.py:617:17-52: The type of this argument is unknown [unknown-argument-type]
+ ERROR homeassistant/components/miele/sensor.py:638:29-34: Type of lambda parameter `value` is unknown [implicit-any-lambda]
+ ERROR homeassistant/components/miele/sensor.py:639:17-47: The type of this argument is unknown [unknown-argument-type]
+ ERROR homeassistant/components/miele/sensor.py:656:29-34: Type of lambda parameter `value` is unknown [implicit-any-lambda]
+ ERROR homeassistant/components/miele/sensor.py:657:17-45: The type of this argument is unknown [unknown-argument-type]

xarray (https://github.com/pydata/xarray)
+ ERROR xarray/core/groupby.py:502:35-72: `T_DataWithCoords` is not assignable to any of constraints `DataArray`, `Dataset` of type variable `T_Xarray` [bad-specialization]

graphql-core (https://github.com/graphql-python/graphql-core)
+ ERROR tests/type/test_validation.py:77:23-30: `GraphQLNamedType` is not assignable to upper bound `GraphQLEnumType | GraphQLInputObjectType | GraphQLInterfaceType | GraphQLList | GraphQLObjectType | GraphQLScalarType | GraphQLUnionType` of type variable `GNT_co` [bad-specialization]
+ ERROR src/graphql/utilities/map_schema_config.py:160:34-59: `GraphQLList[Unknown] | GraphQLNamedType | GraphQLNonNull[Unknown]` is not assignable to upper bound `GraphQLEnumType | GraphQLInputObjectType | GraphQLInterfaceType | GraphQLList | GraphQLObjectType | GraphQLScalarType | GraphQLUnionType` of type variable `GNT_co` [bad-specialization]

django-modern-rest (https://github.com/wemake-services/django-modern-rest)
+ ERROR dmr/internal/io.py:60:17-36: `AsyncIterable[Any]` is not assignable to upper bound `_SupportsAclose` of type variable `_SupportsAcloseT` [bad-specialization]

@rchen152 rchen152 self-assigned this Aug 11, 2026
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.

False Negative [bad-specialization] with generator expression

2 participants