Uh oh!
There was an error while loading. Please reload this page.
Fix TypeIs negative narrowing of union of generics - #18193
Merged
hauntsaninja merged 7 commits intoJun 21, 2025
Conversation
This comment has been minimized.
This comment has been minimized.
brianschubert
marked this pull request as draft
November 26, 2024 21:20
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
brianschubert
marked this pull request as ready for review
June 12, 2025 13:50
sterliakov
commented
Jun 12, 2025
Collaborator
More issues might be closed by this PR: sterliakov/mypy-issues#70 I didn't look deeper, but all four look related enough |
This comment has been minimized.
This comment has been minimized.
hauntsaninja
approved these changes
Jun 20, 2025
Collaborator
There was a problem hiding this comment.
Thanks for the PR! I made some small additions. We have a new hit in starlette, but I think this is correct?
Edit: see also #19320
Contributor
Diff from mypy_primer, showing the effect of this PR on open source code: pytest (https://github.com/pytest-dev/pytest)
+ testing/test_monkeypatch.py:420: error: Unused "type: ignore" comment [unused-ignore]
starlette (https://github.com/encode/starlette)
+ starlette/middleware/errors.py:178: error: Argument 1 to "run_in_threadpool" has incompatible type "Callable[[Request, Exception], Response | Awaitable[Response]] | Callable[[WebSocket, Exception], Awaitable[None]]"; expected "Callable[[Request, Exception], Response]" [arg-type]+ starlette/_exception_handler.py:61: error: Argument 1 to "run_in_threadpool" has incompatible type "Callable[[Request, Exception], Response | Awaitable[Response]] | Callable[[WebSocket, Exception], Awaitable[None]]"; expected "Callable[[Request | WebSocket, Exception], Any]" [arg-type]+ starlette/routing.py:68: error: Incompatible types in assignment (expression has type "Callable[..., Awaitable[Any]] | partial[Coroutine[Any, Any, Awaitable[Response] | Response]]", variable has type "Callable[[Request], Awaitable[Response]]") [assignment]+ starlette/routing.py:68: error: Too few arguments for "run_in_threadpool" [call-arg] |
brianschubert
commented
Jun 21, 2025
CollaboratorAuthor
Just confirming (a little late), the starlette hit looks correct to me too. I think it boils down to something like this: fromcollections.abcimportCallablefromtypingimportAnyfromtyping_extensionsimportTypeIsclassFoo[T]: passdefis_foo_factory(x: object) ->TypeIs[Callable[..., Foo[Any]]]: ...
# ===== Before =====deff(x: Callable[..., int|Foo[int]] |Callable[..., Foo[str]]) ->None:
ifis_foo_factory(x):
reveal_type(x) # N: Revealed type is "def (*Any, **Any) -> SCRATCH.Foo[Any]"else:
reveal_type(x) # E: Statement is unreachable # ===== After =====deff(x: Callable[..., int|Foo[int]] |Callable[..., Foo[str]]) ->None:
ifis_foo_factory(x):
reveal_type(x) # N: Revealed type is "def (*Any, **Any) -> SCRATCH.Foo[Any]"else:
reveal_type(x) # N: Revealed type is "def (*Any, **Any) -> builtins.int | SCRATCH.Foo[builtins.int]"which all looks right to me. From what I can tell, the logic in starlette is expecting the else branch to also narrow |
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 freeto 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.
Fixes#18009, fixes#19282, fixes#17181
Modelling the runtime behavior of
isinstance(which erases generic type arguments) isn't applicable toTypeIs. This PR adds a flag so that we can skip that logic deep insideconditional_types_with_intersection.It's a little awkward having to pass a flag down through so many call levels, but I can't think of a better way.