Skip to content

Experiment: use upper bound instead of Any for TypeIs[Generic[Any]] in stdlib - #16108

Draft
randolf-scholz wants to merge 1 commit into
python:mainfrom
randolf-scholz:typeis_generic_object
Draft

Experiment: use upper bound instead of Any for TypeIs[Generic[Any]] in stdlib#16108
randolf-scholz wants to merge 1 commit into
python:mainfrom
randolf-scholz:typeis_generic_object

Conversation

@randolf-scholz

Copy link
Copy Markdown
Contributor

Following the discussion in #16051, if callable should return TypeIs[Callable[..., object]], these methods logically should also use object for their covariant and Never for their contravariant arguments rather than Any.

@github-actions

Copy link
Copy Markdown
Contributor

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

psycopg (https://github.com/psycopg/psycopg)
+ psycopg_pool/psycopg_pool/_acompat.py:184: error: Incompatible types in assignment (expression has type "object", variable has type "T | Coroutine[Any, Any, T]")  [assignment]
+ psycopg_pool/psycopg_pool/_acompat.py:185: error: Incompatible return value type (got "T | Coroutine[Any, Any, T]", expected "T")  [return-value]

steam.py (https://github.com/Gobot1234/steam.py)
+ steam/utils.py:894: error: Incompatible return value type (got "object", expected "_T")  [return-value]

aiohttp-devtools (https://github.com/aio-libs/aiohttp-devtools)
+ aiohttp_devtools/runserver/config.py:240: error: Incompatible types in assignment (expression has type "object", variable has type "Application | Awaitable[Application]")  [assignment]

prefect (https://github.com/PrefectHQ/prefect)
+ src/prefect/flow_engine.py:1464: error: Incompatible types in assignment (expression has type "object", variable has type "R | type[NotSet]")  [assignment]
+ src/prefect/concurrency/v1/sync.py:63: error: Argument 1 to "emit_concurrency_acquisition_events" has incompatible type "list[MinimalConcurrencyLimitResponse] | Coroutine[Any, Any, list[MinimalConcurrencyLimitResponse]]"; expected "list[MinimalConcurrencyLimitResponse]"  [arg-type]
+ src/prefect/concurrency/v1/sync.py:72: error: Argument 1 to "emit_concurrency_release_events" has incompatible type "list[MinimalConcurrencyLimitResponse] | Coroutine[Any, Any, list[MinimalConcurrencyLimitResponse]]"; expected "list[MinimalConcurrencyLimitResponse]"  [arg-type]
+ src/prefect/cli/deployment.py:215: error: "object" has no attribute "model_dump"  [attr-defined]

strawberry (https://github.com/strawberry-graphql/strawberry)
+ strawberry/utils/await_maybe.py:13: error: Incompatible return value type (got "object", expected "T")  [return-value]

aiohttp (https://github.com/aio-libs/aiohttp)
+ aiohttp/web.py:295:15: error: Incompatible types in assignment (expression has type "object", variable has type "Application | Awaitable[Application]")  [assignment]

scrapy (https://github.com/scrapy/scrapy)
+ scrapy/utils/defer.py:576: error: Incompatible return value type (got "_T | Awaitable[_T]", expected "Awaitable[_T]")  [return-value]

discord.py (https://github.com/Rapptz/discord.py)
+ discord/utils.py:714: error: Incompatible return value type (got "object", expected "T")  [return-value]

trio (https://github.com/python-trio/trio)
+ src/trio/_core/_tests/test_ki.py:687: error: Argument 1 to "send" of "CoroutineType" has incompatible type "None"; expected "Never"  [arg-type]

anyio (https://github.com/agronholm/anyio)
+ src/anyio/from_thread.py:279: error: Argument 1 to "set_result" of "Future" has incompatible type "object"; expected "T_Retval"  [arg-type]

kopf (https://github.com/nolar/kopf)
+ kopf/_core/intents/piggybacking.py:165: error: Incompatible types in assignment (expression has type "object", variable has type "str | None")  [assignment]

paasta (https://github.com/yelp/paasta)
+ paasta_tools/async_utils.py:162: error: "None" has no attribute "close"  [attr-defined]

@srittau

srittau commented Jul 29, 2026

Copy link
Copy Markdown
Collaborator

Cc @AlexWaygood @JelleZijlstra, since you previously commented on this or changed the annotations. I agree that we should at least be consistent inside typeshed.

@JelleZijlstra

Copy link
Copy Markdown
Member

I think I tried this recently and decided to hold off on these?

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants