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
SecretStr cannot be idempotently passed if annotated using Field #5088
Comments
Subclassing This makes me think that it is related to the fact that the model is not expecting a regular from typing_extensions import Annotated
from pydantic import BaseModel, Field, SecretStr
class ApiKey(SecretStr):
min_length = 4
max_length = 4
class Test(BaseModel):
api_key: ApiKey
Test(api_key=ApiKey("abcd")) |
but does this actually apply length validation? I suspect that truth is we won't be able to make many changes to this in V1 at this point without risking breaking existing behaviour, but we should be able to get it right for V2 which is nearing completing at the moment. |
Thanks for your comment. Just gave this a quick test and it appears to validate it correctly.
|
Great, pleased you found a work around. |
Since there is a work around, and it's already working on V2, I'll be closing this issue. 🙏 |
This doesn't seem to work for regex patterns class StringParam(SecretStr):
pattern = r"a"
class Test(BaseModel):
test: StringParam
Test(test="b")
#Test(test=StringParam('**********')) My use case is validating config input with cloud credentials |
Initial Checks
Description
Pydantic previously had a bug where
SecretStr
objects were not able to be idempotently passed into aSecretStr
field on a model.I'm using
SecretStr
to implement an annotated API key type where the length of the key is known, and I found I am unable to passSecretStr
objects into a model if it has been annotated. Only regular strings will work.Example Code
Python, Pydantic & OS Version
Affected Components
.model_dump()
and.model_dump_json()
model_construct()
, pickling, private attributes, ORM modeThe text was updated successfully, but these errors were encountered: