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

Type inference by container membership #15855

Closed
kojiromike opened this issue Aug 11, 2023 · 1 comment
Closed

Type inference by container membership #15855

kojiromike opened this issue Aug 11, 2023 · 1 comment
Labels

Comments

@kojiromike
Copy link

Feature

If a is contained by B[T], then a must be of type T. I think mypy should make this inference, but it does not do so today.

from typing import Container


def check(o: object, c: Container[str]) -> str | None:
    if o in c:
        return o  # Incompatible return value type (got "object", expected "Optional[str]")
    return None

In the above case, I think it's clear that if o in c is True, then o must have the generic of c, in this case str.

Pitch

Any opportunity to correctly infer a type improves mypy and typechecking in general. This is just such an opportunity.

@AlexWaygood
Copy link
Member

Duplicate of #10977

@AlexWaygood AlexWaygood marked this as a duplicate of #10977 Aug 11, 2023
@AlexWaygood AlexWaygood closed this as not planned Won't fix, can't repro, duplicate, stale Aug 11, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants