From 17a849e84477c784bea5fbf2370e161fd5f2e9e6 Mon Sep 17 00:00:00 2001 From: Alexander Tikhonov Date: Tue, 12 Sep 2023 11:53:26 +0300 Subject: [PATCH] Add support for LiteralString --- README.md | 2 ++ mashumaro/core/meta/types/pack.py | 4 ++++ mashumaro/core/meta/types/unpack.py | 4 ++++ tests/test_data_types.py | 4 +++- 4 files changed, 13 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index d5a92349..c40ac14f 100644 --- a/README.md +++ b/README.md @@ -196,6 +196,7 @@ for special primitives from the [`typing`](https://docs.python.org/3/library/typ * [`NewType`](https://docs.python.org/3/library/typing.html#newtype) * [`Annotated`](https://docs.python.org/3/library/typing.html#typing.Annotated) * [`Literal`](https://docs.python.org/3/library/typing.html#typing.Literal) +* [`LiteralString`](https://docs.python.org/3/library/typing.html#typing.LiteralString) * [`Final`](https://docs.python.org/3/library/typing.html#typing.Final) * [`Self`](https://docs.python.org/3/library/typing.html#typing.Self) * [`Unpack`](https://docs.python.org/3/library/typing.html#typing.Unpack) @@ -252,6 +253,7 @@ for backported types from [`typing-extensions`](https://github.com/python/typing * [`TypedDict`](https://docs.python.org/3/library/typing.html#typing.TypedDict) * [`Annotated`](https://docs.python.org/3/library/typing.html#typing.Annotated) * [`Literal`](https://docs.python.org/3/library/typing.html#typing.Literal) +* [`LiteralString`](https://docs.python.org/3/library/typing.html#typing.LiteralString) * [`Self`](https://docs.python.org/3/library/typing.html#typing.Self) * [`TypeVarTuple`](https://docs.python.org/3/library/typing.html#typing.TypeVarTuple) * [`Unpack`](https://docs.python.org/3/library/typing.html#typing.Unpack) diff --git a/mashumaro/core/meta/types/pack.py b/mashumaro/core/meta/types/pack.py index fc0f614b..4431e90a 100644 --- a/mashumaro/core/meta/types/pack.py +++ b/mashumaro/core/meta/types/pack.py @@ -20,6 +20,8 @@ Union, ) +import typing_extensions + from mashumaro.core.const import PY_39_MIN, PY_311_MIN from mashumaro.core.meta.code.lines import CodeLines from mashumaro.core.meta.helpers import ( @@ -384,6 +386,8 @@ def pack_special_typing_primitive(spec: ValueSpec) -> Optional[Expression]: return PackerRegistry.get(spec.copy(type=spec.type.__supertype__)) elif is_literal(spec.type): return pack_literal(spec) + elif spec.type is typing_extensions.LiteralString: + return PackerRegistry.get(spec.copy(type=str)) elif is_self(spec.type): method_name = spec.builder.get_pack_method_name( format_name=spec.builder.format_name diff --git a/mashumaro/core/meta/types/unpack.py b/mashumaro/core/meta/types/unpack.py index 97d5769c..738e75e0 100644 --- a/mashumaro/core/meta/types/unpack.py +++ b/mashumaro/core/meta/types/unpack.py @@ -26,6 +26,8 @@ Union, ) +import typing_extensions + from mashumaro.core.const import PY_39_MIN, PY_311_MIN from mashumaro.core.helpers import parse_timezone from mashumaro.core.meta.code.lines import CodeLines @@ -673,6 +675,8 @@ def unpack_special_typing_primitive(spec: ValueSpec) -> Optional[Expression]: ) elif is_literal(spec.type): return LiteralUnpackerBuilder().build(spec) + elif spec.type is typing_extensions.LiteralString: + return UnpackerRegistry.get(spec.copy(type=str)) elif is_self(spec.type): method_name = spec.builder.get_unpack_method_name( format_name=spec.builder.format_name diff --git a/tests/test_data_types.py b/tests/test_data_types.py index cda39cf3..7455d617 100644 --- a/tests/test_data_types.py +++ b/tests/test_data_types.py @@ -40,7 +40,7 @@ ) import pytest -from typing_extensions import Final +from typing_extensions import Final, LiteralString from mashumaro import DataClassDictMixin from mashumaro.config import BaseConfig @@ -170,6 +170,7 @@ class Fixture: CUSTOM_SERIALIZE = "_FOOBAR_" GENERIC_SERIALIZABLE_LIST_INT = GenericSerializableList([1, 2, 3]) GENERIC_SERIALIZABLE_LIST_STR = GenericSerializableList(["a", "b", "c"]) + LITERAL_STRING = "foo" inner_values = [ @@ -247,6 +248,7 @@ class Fixture: ["_a", "_b", "_c"], ), (MyDatetimeNewType, Fixture.DATETIME, Fixture.DATETIME_STR), + (LiteralString, Fixture.LITERAL_STRING, Fixture.LITERAL_STRING), ] if os.name == "posix":