Skip to content

Contains does not propagate type info #8445

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

Closed
habilain opened this issue Feb 26, 2020 · 2 comments
Closed

Contains does not propagate type info #8445

habilain opened this issue Feb 26, 2020 · 2 comments

Comments

@habilain
Copy link

This is probably an easy-ish enhancement. If you check the following code:

from typing import Union, Set

x: Set[int] = {1, 2}
y: Union[int, str] = 1

if y in x:
  x.remove(y)

You get an error: Argument 1 to "remove" of "set" has incompatible type "Union[int, str]"; expected "int". However, obviously this can never happen; inside the conditional, the type of y must be an int because we know that it's contained within a set of Ints. This issue comes up with all typed container types.

It gets a bit more complex if the container type itself is a container of a Union type, but I think the correct rule here is that "y is contained in x implies the type of y is the intersection of the possible types of y and the types x can contain".

@JukkaL
Copy link
Collaborator

JukkaL commented Feb 28, 2020

This sounds like a reasonable improvement.

@JukkaL
Copy link
Collaborator

JukkaL commented Feb 28, 2020

Actually this is a duplicate of #3229.

@JukkaL JukkaL closed this as completed Feb 28, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants