Skip to content

Commit

Permalink
Merge pull request #167 from Fatal1ty/literal-string
Browse files Browse the repository at this point in the history
Add support for LiteralString
  • Loading branch information
Fatal1ty authored Sep 12, 2023
2 parents e39a559 + 17a849e commit 7ad1da0
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 1 deletion.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 4 additions & 0 deletions mashumaro/core/meta/types/pack.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions mashumaro/core/meta/types/unpack.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion tests/test_data_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 = [
Expand Down Expand Up @@ -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":
Expand Down

0 comments on commit 7ad1da0

Please sign in to comment.