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

Subclass of SecretStr expects bytes as input #6729

Closed
1 task done
AlexVndnblcke opened this issue Jul 18, 2023 · 0 comments · Fixed by #6730
Closed
1 task done

Subclass of SecretStr expects bytes as input #6729

AlexVndnblcke opened this issue Jul 18, 2023 · 0 comments · Fixed by #6730
Assignees
Labels
bug V2 Bug related to Pydantic V2 unconfirmed Bug not yet confirmed as valid/applicable

Comments

@AlexVndnblcke
Copy link
Contributor

AlexVndnblcke commented Jul 18, 2023

Initial Checks

  • I confirm that I'm using Pydantic V2

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:

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]

While the type is defined as follows:

class EncryptedBase64Str(SecretStr):
    def get_decrypted_value(self, aws_key_id: Union[str, UUID], **kwargs) -> str:
        decrypt_with_key(self.get_secret_value(), key=aws_key_id)

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

from pydantic import SecretStr, ValidationError
from pydantic_settings import BaseSettings
from typing import Union
from uuid import UUID


class EncryptedBase64Str(SecretStr):
    def get_decrypted_value(self, aws_key_id: Union[str, UUID], **kwargs) -> str:
        # simplified for demonstration purposes
        return self.get_secret_value()


class Settings(BaseSettings):
    some_decryptable_secret: EncryptedBase64Str

try:
    Settings(some_decryptable_secret="abc")
except ValidationError as error:
    print(error)
    exit(0)

raise Exception("bug is not reproducible")

Python, Pydantic & OS Version

pydantic version: 2.0.3
        pydantic-core version: 2.3.0 release build profile
                 install path: /Users/alexandervandenbulcke/Library/Caches/pypoetry/virtualenvs/jarvis-api-5-3ao05Y-py3.9/lib/python3.9/site-packages/pydantic
               python version: 3.9.16 (main, Dec  7 2022, 10:02:13)  [Clang 14.0.0 (clang-1400.0.29.202)]
                     platform: macOS-12.5.1-arm64-arm-64bit
     optional deps. installed: ['typing-extensions']

Selected Assignee: @samuelcolvin

@AlexVndnblcke AlexVndnblcke added bug V2 Bug related to Pydantic V2 unconfirmed Bug not yet confirmed as valid/applicable labels Jul 18, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug V2 Bug related to Pydantic V2 unconfirmed Bug not yet confirmed as valid/applicable
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants