Skip to content

Refactor type guards in Agent class to use TypeIs instead of TypeGuard #1637

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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

max-muoto
Copy link

@max-muoto max-muoto commented May 2, 2025

In most cases, TypeIs is preferred over TypeGuard as TypeGuard fails to narrow in the negative case. A simple example is how isawaitable use to work, where the negative case would wouldn't narrow the object at all keeping it unioned it with Awaitable.

import inspect
from collections.abc import Awaitable
from typing import reveal_type

async def f(t: Awaitable[int] | int) -> None:
    if inspect.isawaitable(t):
        reveal_type(t)  # Awaitable[Any]
    else:
        reveal_type(t)  # Awaitable[int] | int

@max-muoto max-muoto marked this pull request as ready for review May 2, 2025 22:25
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.

1 participant