You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
fromtypingimportSetsets= [{1, 2}, {2, 3}]
union: Set[int] =set().union(*sets)
# main.py:5: error: # Incompatible types in assignment (expression has type "Set[<nothing>]", # variable has type "Set[int]")# main.py:5: error: # Argument 1 to "union" of "set" has incompatible type "*List[Set[int]]"; # expected "Iterable[<nothing>]"
Apparently, mypy infers the type of set() to be Set[<nothing>]. Ideally, it would work out from the context that set() should have type Set[int]. I would think that an empty set (with no other reference to it) can have any member type, including the one that works in the specific context.
This appears to be a duplicate of #2013. Also I don't think the problem is with the idiom of the union of multiple sets, but just the union on the same line as set instantiation. Here is what I came across:
# error: Incompatible types in assignment (expression has type "Set[str]", variable has type "Set[Optional[str]]")maybe_strs: Set[Optional[str]] =set('abc').union((None,))
# the following type checksmaybe_strs2: Set[Optional[str]] =set('abc')
maybe_strs2.add(None)
edit
I just noticed that using the | operator instead of union type checks fine:
Uh oh!
There was an error while loading. Please reload this page.
The idiom
set().union(*sets)
is used to produce the union of multiple sets:This does not type check with mypy:
Apparently, mypy infers the type of
set()
to beSet[<nothing>]
. Ideally, it would work out from the context thatset()
should have typeSet[int]
. I would think that an empty set (with no other reference to it) can have any member type, including the one that works in the specific context.The following code type-checks:
The text was updated successfully, but these errors were encountered: