Don't accept str
in FromPyObject
for Vec<&str>
and Vec<String>
#2342
Labels
str
in FromPyObject
for Vec<&str>
and Vec<String>
#2342
As per the title, it would be nice if we didn't accept Python
str
inFromPyObject
forVec<&str>
andVec<String>
. Some background below.I'm not sure how hard this will be (without specialization).
Discussed in #2341
Originally posted by atoav April 27, 2022
In python one can iterate over a
str
as if it was aList[str]
. And while this is occasionaly useful it can also be a major footgun if users pass in a"string"
instead of a["string"]
and suddenly have a function operate on single characters instead of the whole string, as they might (admittedly, errouniously) assumed one that one string.In python I'd prevent this user error by ensuring incoming str are never treated as a list of characters by wrapping a lone
str
in a list as follows:In pyo3 this is an issue as well. Let's say i have a simple function that should take a
Vec<String>
and print out each string in the vec:If I now run this in the python interpreter with a string that is not a list each character is printed out seperately:
Is there any simple way to ensure single
str
are treated as[str]
using pyo3? Even an Error would be acceptable, as long as users don't pass a string and have it iterate over characters without them noticing.The text was updated successfully, but these errors were encountered: