You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
For my use-case I have defined a subclass of the SecretStr type. This functionality worked fine in pydantic V1. Since the migration to V2 the follow error appeared continuously:
pydantic_core._pydantic_core.ValidationError: 1 validation error for [redacted]
shared_secret
Input should be a valid bytes [type=bytes_type, input_value='[redacted]', input_type=str]
I.e. it is counter intuitive that the expected type is "bytes" when the class inherits from the specialized str variant.
This is due to the too restrictive test in pydantic.types._SecretFieldValidator.__get_pydantic_core_schema__ where type is evaluated based on the types defined by pydantic:
self.inner_schema = handler(str if self.field_type is SecretStr else bytes)
error_kind = 'string_type' if self.field_type is SecretStr else 'bytes_type'
Children of SecretStr are neglected here.
Example Code
frompydanticimportSecretStr, ValidationErrorfrompydantic_settingsimportBaseSettingsfromtypingimportUnionfromuuidimportUUIDclassEncryptedBase64Str(SecretStr):
defget_decrypted_value(self, aws_key_id: Union[str, UUID], **kwargs) ->str:
# simplified for demonstration purposesreturnself.get_secret_value()
classSettings(BaseSettings):
some_decryptable_secret: EncryptedBase64Strtry:
Settings(some_decryptable_secret="abc")
exceptValidationErroraserror:
print(error)
exit(0)
raiseException("bug is not reproducible")
Initial Checks
Description
For my use-case I have defined a subclass of the
SecretStr
type. This functionality worked fine in pydantic V1. Since the migration to V2 the follow error appeared continuously:While the type is defined as follows:
I.e. it is counter intuitive that the expected type is "bytes" when the class inherits from the specialized str variant.
This is due to the too restrictive test in
pydantic.types._SecretFieldValidator.__get_pydantic_core_schema__
where type is evaluated based on the types defined by pydantic:Children of SecretStr are neglected here.
Example Code
Python, Pydantic & OS Version
Selected Assignee: @samuelcolvin
The text was updated successfully, but these errors were encountered: