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
{{ message }}
Repository navigation
List[Union[...]] is not always satisfied by valid inputs. #3351
Presumably the inferred List[int] ought to be able to satisfy List[Union[int, float]], right?
No, List is invariant, and therefore List[int] is not a subtype (not compatible with) List[Union[int, float]] (the latter actually is just List[float], see PEP 484). The reason is that lists are mutable and therefore it is not safe to pass something of type List[int], where List[float] (or List[Union[int, float]]) is expected.
See the docs for such situations. In your case I would recommend annotating your function as:
defmean(numbers: Sequence[float]) ->float:
(since you don't mutate numbers.)
Reacted by Joe Rickerby, gebbissimo, Brijesh Sahoo, Sofie Van Landeghem, Sunniva Indrehus, Moritz Wolter and OleksiiKutsenko
Reacted by Tobi
Reacted by Joe Rickerby, Brijesh Sahoo and Addison Klinke
Sorry for the necroBump but I'm not able to have a list from Literals (as the exemple here) and I guess it's the same topic.
I Feel that something is possible but I cannot make the typing system satisfied with that
For exemple :
MyListType = Literal['dogs', 'cats', 'crocodiles', 'ducks', 'bird', 'pony', 'fish'] # Tried with Union[Literal,Literal...] as well
my_list : List[MyListType] = ['cats', 'crocodiles', 'ducks'] # Tried with Sequence as well and putting all elements in the MyListType
I guess it's comming from Type "str" cannot be assigned to type "MyListType" which should...
Presumably the inferred List[int] ought to be able to satisfy List[Union[int, float]], right?
No, List is invariant, and therefore List[int] is not a subtype (not compatible with) List[Union[int, float]] (the latter actually is just List[float], see PEP 484). The reason is that lists are mutable and therefore it is not safe to pass something of type List[int], where List[float] (or List[Union[int, float]]) is expected.
To explain why this is unsafe, to help people like me who have never heard the term invariant before, say that you have the following code:
you will end up with ints containing [1, 2, 3, 4.5] even though in the type definition it says that ints should only contain integers! And that is what they meant when they said passing the list into the method was "unsafe".
Reacted by Firuza Mamedova, Kyle Bebak, Maciej Matczak, Yojan Shrestha, Tobias Deiminger, Akhilesh Halageri, Stefano Pellegrini, Helen Hastings, Higor Carmanini, Tianqi Xu and 3 more
Reacted by Higor Carmanini, Bi0max and JackismyShephard
Activity
ilevkivskyi commentedon May 10, 2017
No,
Listis invariant, and thereforeList[int]is not a subtype (not compatible with)List[Union[int, float]](the latter actually is justList[float], see PEP 484). The reason is that lists are mutable and therefore it is not safe to pass something of typeList[int], whereList[float](orList[Union[int, float]]) is expected.See the docs for such situations. In your case I would recommend annotating your function as:
(since you don't mutate
numbers.)lovelydinosaur commentedon May 10, 2017
Thanks for the clarification. 😄
(& apologies for the noise!)
gvanrossum commentedon May 10, 2017
This should also be called out much earlier in the mypy docs. It's a very common "bug" report.
ilevkivskyi commentedon May 10, 2017
I think what Jukka proposed in #3352 (including link to docs in error messages) will solve the problem.
ierezell commentedon Sep 30, 2020
Sorry for the necroBump but I'm not able to have a list from Literals (as the exemple here) and I guess it's the same topic.
I Feel that something is possible but I cannot make the typing system satisfied with that
For exemple :
I guess it's comming from
Type "str" cannot be assigned to type "MyListType"which should...gvanrossum commentedon Sep 30, 2020
@ierezell I don't get an error for that input. Please see Gitter for more help.
Use `Sequence` instead of `List` for `path` param
Use `Sequence` instead of `List` for `path` param (#659)
Godsmith commentedon Nov 3, 2021
To explain why this is unsafe, to help people like me who have never heard the term invariant before, say that you have the following code:
If you then do
you will end up with
intscontaining[1, 2, 3, 4.5]even though in the type definition it says thatintsshould only contain integers! And that is what they meant when they said passing the list into the method was "unsafe".Update converter.py
Issue 644 (#645)
Fix urlpatterns use Sequence instead of List (#19)
Issue 644 (#645)