Skip to content
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

isinstance on runtime-checkable protocols does not correctly narrow unions inferred by TypeGuard #13753

Open
twoertwein opened this issue Sep 28, 2022 · 0 comments
Labels
bug mypy got something wrong topic-type-narrowing Conditional type narrowing / binder topic-typeguard TypeGuard / PEP 647

Comments

@twoertwein
Copy link

Bug Report

from __future__ import annotations

from collections.abc import Sized
from typing import (
    Iterator,
    Sequence,
)

from typing_extensions import TypeGuard


def is_list_like(x: object) -> TypeGuard[Iterator | Sequence]:  # not the best example but it demonstrates the issue
    ...


def is_nested_list(xs: object) -> bool:
    return (
        is_list_like(xs)
        and isinstance(xs, Sized)  # after this xs is assumed to be Sized but it should be Sized while simultaneously also being an Iterator or a Sequence
        and len(xs) > 0
        and all(is_list_like(x) for x in xs)  # "Sized" has no attribute "__iter__" (not iterable)  [attr-defined]
    )

Your Environment

  • Mypy version used: 0.981 (same for 0.971)
  • Python version used: 3.10.4
@twoertwein twoertwein added the bug mypy got something wrong label Sep 28, 2022
@AlexWaygood AlexWaygood added topic-type-narrowing Conditional type narrowing / binder topic-typeguard TypeGuard / PEP 647 labels Sep 28, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong topic-type-narrowing Conditional type narrowing / binder topic-typeguard TypeGuard / PEP 647
Projects
None yet
Development

No branches or pull requests

2 participants