Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

String passed to field: List[str] gets coerced in a very unexpected way #202

Closed
pawelswiecki opened this issue Jun 15, 2018 · 9 comments
Closed

Comments

@pawelswiecki
Copy link

pawelswiecki commented Jun 15, 2018

Using Python 3.6.5 and pydantic==0.10.

from typing import List                                                        
import pydantic                                                                
                                                                               
                                                                               
class User(pydantic.BaseModel):                                                
    name: str                                                                  
    aliases: List[str]                                                         
                                                                               
                                                                               
u1 = User(name='Bob', aliases=['Panther-Man', 'The Janitor'])  # should validate, and it does
print(u1)                                                                      
# User name='Bob' aliases=['Panther-Man', 'The Janitor']                     
                                                                               
u2 = User(name='Bob', aliases='Small Bob')  # should not validate, but it does 
print(u2)                                                                      
# User name='Bob' aliases=['S', 'm', 'a', 'l', 'l', ' ', 'B', 'o', 'b']

String seems to be treated as an Iterable of (sub)strings (technically it's true in Python), which leads to very unexpected behavior, to say the least; I'd call it a bug, even.

I hope it could be changed in the future, but how can I deal with it in the current version of pydantic?

@pawelswiecki pawelswiecki changed the title String gets coerced very unexpectedly String passed to field: List[str] gets coerced very unexpectedly Jun 15, 2018
@pawelswiecki pawelswiecki changed the title String passed to field: List[str] gets coerced very unexpectedly String passed to field: List[str] gets coerced in a very unexpected way Jun 15, 2018
@samuelcolvin
Copy link
Member

I think this is fixed by #200, will be released soon.

@pawelswiecki
Copy link
Author

Great, thank you!

@pawelswiecki
Copy link
Author

@samuelcolvin I'm on pydantic=0.11.1 and I'm still getting

User name='Bob' aliases=['S', 'm', 'a', 'l', 'l', ' ', 'B', 'o', 'b']

instead of validation error.

@samuelcolvin
Copy link
Member

Yes, looks like #200 only works if you use aliases: list not aliases: List[str]. Sorry about that.

@samuelcolvin samuelcolvin reopened this Jul 4, 2018
@pawelswiecki
Copy link
Author

No problem. Thank you for developing this library :)

samuelcolvin added a commit that referenced this issue Jul 4, 2018
@samuelcolvin
Copy link
Member

Should be fixed by #225, would be very useful if you could check that out and confirm it does everything you need.

@pawelswiecki
Copy link
Author

pawelswiecki commented Jul 4, 2018

WOW, thanks for so quick fix.

So, I've pip installed from github at this particular commit and List[str] works as expected. My other use-case (which I did not mention), that is List[SomeStringBasedEnum], also validates as expected!

Thank you again :)

@samuelcolvin
Copy link
Member

Merged, I'll deploy this in a few days.

@pawelswiecki
Copy link
Author

Great, it will simplify my code :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants