Skip to content

Commit

Permalink
🐛 Make EncodedStr a dataclass (#7396)
Browse files Browse the repository at this point in the history
Co-authored-by: Adrian Garcia Badaracco <1755071+adriangb@users.noreply.github.com>
Co-authored-by: Hasan Ramezani <hasan.r67@gmail.com>
  • Loading branch information
3 people committed Sep 20, 2023
1 parent e4393ae commit 3b297ad
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions pydantic/types.py
Expand Up @@ -1600,6 +1600,7 @@ def __hash__(self) -> int:
return hash(self.encoder)


@_dataclasses.dataclass(**_internal_dataclass.slots_true)
class EncodedStr(EncodedBytes):
"""A str type that is encoded and decoded using the specified encoder.
Expand Down Expand Up @@ -1659,7 +1660,7 @@ def __get_pydantic_core_schema__(
) -> core_schema.CoreSchema:
return core_schema.general_after_validator_function(
function=self.decode_str,
schema=super().__get_pydantic_core_schema__(source=source, handler=handler),
schema=super(EncodedStr, self).__get_pydantic_core_schema__(source=source, handler=handler), # noqa: UP008
serialization=core_schema.plain_serializer_function_ser_schema(function=self.encode_str),
)

Expand All @@ -1683,7 +1684,10 @@ def encode_str(self, value: str) -> str:
Returns:
The encoded data.
"""
return super().encode(value=value.encode()).decode()
return super(EncodedStr, self).encode(value=value.encode()).decode() # noqa: UP008

def __hash__(self) -> int:
return hash(self.encoder)


Base64Bytes = Annotated[bytes, EncodedBytes(encoder=Base64Encoder)]
Expand Down

0 comments on commit 3b297ad

Please sign in to comment.