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
When a list contains Union[Foo[Any],Foo[T]], there are inconsistent errors when adding the result to a list of Foo[T].
To Reproduce
fromtypingimportList, Union, Generic, TypeVarT=TypeVar("T")
classTree(Generic[T]):
children: "List[Union[T, Tree[T]]]"def__init__(self, children: "List[Union[T, Tree[T]]]") ->None:
self.children=childrendefall_sub_trees(self) ->None:
sub_trees: "List[Tree[T]]"= [self]
reveal_type(sub_trees)
only_child_trees= [vforvinself.childrenifisinstance(v, Tree)]
reveal_type(only_child_trees)
# 3 equivalent statements (ignoring that the first mutates instead of replacing)sub_trees.extend(only_child_trees)
sub_trees=sub_trees+only_child_treessub_trees+=only_child_trees
Expected Behavior
Initially I was expecting the value of only_child_trees to be List[Tree[T`1]]. However, it is possible that T`1 has the type of Tree[str]. This would mean that the true type of only_child_trees is List[Union[Tree[str],Tree[Tree[str]]]]. So the result is accurate.
However, based on that, I would expect that all of the following operations to have consistent behavior.
Actual Behavior
$ mypy typing_example.py
typing_example.py:15: note: Revealed type is "builtins.list[typing_example.Tree[T`1]]"typing_example.py:17: note: Revealed type is "builtins.list[Union[typing_example.Tree[Any], typing_example.Tree[T`1]]]"typing_example.py:21: error: Result type of + incompatible in assignmentFound 1 error in 1 file (checked 1 source file)
mypy doesn't complain about extend() or explicit add then assign. However, it does show an error with +=. It isn't clear to me what the correct behavior is as Any makes things complicated. The fact that += raises an error, but explicit add then assign doesn't seems odd.
I was also able to reproduce this with collections.dequeue, so this is likely general collection issue and not a list specific issue.
Your Environment
Mypy version used: 0.910
Python version used: 3.9
The text was updated successfully, but these errors were encountered:
Mypy's behavior here looks correct to me, at least with the latest version (1.5). The revealed types are as expected, and it doesn't emit any errors in the above code. Pyright agrees with the results.
Bug Report
When a list contains
Union[Foo[Any],Foo[T]]
, there are inconsistent errors when adding the result to a list ofFoo[T]
.To Reproduce
Expected Behavior
Initially I was expecting the value of
only_child_trees
to beList[Tree[T`1]]
. However, it is possible thatT`1
has the type ofTree[str]
. This would mean that the true type ofonly_child_trees
isList[Union[Tree[str],Tree[Tree[str]]]]
. So the result is accurate.However, based on that, I would expect that all of the following operations to have consistent behavior.
Actual Behavior
mypy
doesn't complain aboutextend()
or explicit add then assign. However, it does show an error with+=
. It isn't clear to me what the correct behavior is asAny
makes things complicated. The fact that+=
raises an error, but explicit add then assign doesn't seems odd.I was also able to reproduce this with
collections.dequeue
, so this is likely general collection issue and not alist
specific issue.Your Environment
The text was updated successfully, but these errors were encountered: