Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

FixTypeIs negative narrowing of union of generics#18193

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Conversation

@brianschubert
Copy link
Collaborator

@brianschubertbrianschubert commentedNov 26, 2024
edited
Loading

Fixes#18009,fixes#19282,fixes#17181

Modelling the runtime behavior ofisinstance (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.

@github-actions

This comment has been minimized.

@brianschubertbrianschubert marked this pull request as draftNovember 26, 2024 21:20
@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@brianschubertbrianschubert marked this pull request as ready for reviewJune 12, 2025 13:50
@sterliakov
Copy link
Collaborator

More issues might be closed by this PR:sterliakov/mypy-issues#70 I didn't look deeper, but all four look related enough

@github-actions

This comment has been minimized.

Copy link
Collaborator

@hauntsaninjahauntsaninja left a comment
edited
Loading

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

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

@github-actions
Copy link
Contributor

Diff frommypy_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]

@hauntsaninjahauntsaninja merged commit6886b7a intopython:masterJun 21, 2025
19 checks passed
@brianschubert
Copy link
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 narrowCallable[..., int | Foo[int]] toCallable[..., int], which wouldn't be right.

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

Reviewers

@hauntsaninjahauntsaninjahauntsaninja approved these changes

Assignees

No one assigned

Labels

Projects

None yet

Milestone

No milestone

3 participants

@brianschubert@sterliakov@hauntsaninja

[8]ページ先頭

©2009-2025 Movatter.jp