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
Const validation applied before type validation/parsing #1410
Comments
I agree this is wrong. I can't remember exactly why the const validator is applied as a pre-validator. If you'd be interested in working on this, feel free to try moving it to a post-validator and see if any tests fail - that might tell you why it had to be a pre-validator. If we can fix this without backwards incompatible changes, it can go out soon, otherwise it might have to wait until v2. |
Cool, I'll give it a shot! Also just came across this: import pydantic
import os
class Config1(pydantic.BaseSettings):
port: int = pydantic.Field("1234", const=True)
class Config2(pydantic.BaseModel):
port: int = pydantic.Field("1234", const=True)
print(Config1()) # 1234
print(Config2()) # '1234' Pretty sure it's the same problem. |
That's different, it's because |
Aaah. Didn't know this config setting existed. Turns out it didn't break anything! See #1446 |
Question
Output of
python -c "import pydantic.utils; print(pydantic.utils.version_info())"
:Copy-paste in the Python REPL to continue on errors:
Looked at the code, and noticed that
constant_validator
was applied withpre_validators
-- before the type validators -- which explains the behaviour.My question is: is this by design? It looks odd to have the default value be of a different type than the annotation. Additionally, if the value comes from a JSON object instead, then using
const
means the input type is now constrained to the type of the default value. For instance:The text was updated successfully, but these errors were encountered: