Skip to content

Contains does not propagate type info #8445

Closed
@habilain

Description

@habilain

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".

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions