Different `pydantic.constr()` validation behavior in different environments
See original GitHub issueBug
The main bug here is a difference of validation behavior on different environments.
observations
Linux behavior (docker container)
- OS -
docker run -it --rm python:3.6.8 /bin/sh
:
# uname -a
Linux 20bcd240af44 4.9.184-linuxkit #1 SMP Tue Jul 2 22:58:16 UTC 2019 x86_64 GNU/Linux
- Python version
>>> import sys; print(sys.version)
3.6.8 (default, Jun 11 2019, 01:16:11)
[GCC 6.3.0 20170516]
- Pydantic version
>>> import pydantic; print(pydantic.VERSION)
1.2
The following behavior is observed in the environment shown above:
import pydantic
mytype = pydantic.constr()
value = "value"
mytype_value = mytype(value)
class Model(pydantic.BaseModel):
attr: mytype
# this works
Model(attr=value)
# this causes a validation error, shown below
Model(attr=mytype_value)
The error:
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
File "pydantic/main.py", line 274, in pydantic.main.BaseModel.__init__
pydantic.error_wrappers.ValidationError: 1 validation error for Model
attr
Argument 'value' has incorrect type (expected str, got ConstrainedStrValue) (type=type_error)
Darwin behavior (laptop)
- OS
> uname -a
Darwin username 18.7.0 Darwin Kernel Version 18.7.0: Tue Aug 20 16:57:14 PDT 2019; root:xnu-4903.271.2~2/RELEASE_X86_64 x86_64
- Python version
>>> import sys; print(sys.version)
3.6.8 (v3.6.8:3c6b436a57, Dec 24 2018, 02:04:31)
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.57)]
- Pydantic version
>>> import pydantic; print(pydantic.VERSION)
1.2
The following behavior is observed on my laptop:
import pydantic
mytype = pydantic.constr()
value = "value"
mytype_value = mytype(value)
class Model(pydantic.BaseModel):
attr: mytype
# this works
Model(attr=value)
# this also works in this environment, unlike Linux / container environment
Model(attr=mytype_value)
discussion
The behavior on Darwin seems more expected because pydantic.constr()
creates a new type
(I confirmed that isinstance(mytype, type) == True
in both environments) and, the same type is also used as the annotation, so I would expect to be able to use an instance of my type for the input argument.
Issue Analytics
- State:
- Created 4 years ago
- Reactions:1
- Comments:6 (4 by maintainers)
Top Results From Across the Web
Different `pydantic.constr()` validation behavior in ... - GitHub
The main bug here is a difference of validation behavior on different environments. observations. Linux behavior (docker container). OS - docker ...
Read more >Cool Things You Can Do With Pydantic - Medium
Pydantic is a useful library for data parsing and validation. It coerces input types to the declared type (using type hints), accumulates all ......
Read more >Pydantic constr vs Field args - Stack Overflow
constr is a specific type that give validation rules regarding this specific type. You have equivalent for all classic python types. arguments ...
Read more >Field Types - pydantic
If you want to validate the values of an infinite generator you can create a separate model and use it while consuming the...
Read more >Settings management - pydantic
Data validation and settings management using Python type hints.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
looks like this was not changed on
EmailStr
: https://github.com/samuelcolvin/pydantic/blob/e65d11240a6febcd569ad44a61e2374eb00b9ba6/pydantic/networks.py#L280-L282PR welcome.
This will be because pydantic is not compiled on macos, but is on linux.
Might related to #1060 and related issues.