Skip to content

Commit

Permalink
Merge pull request #218 from Fatal1ty/mappingproxy
Browse files Browse the repository at this point in the history
Add support for MappingProxyType
  • Loading branch information
Fatal1ty authored Apr 28, 2024
2 parents 2b7c64b + 9f8f659 commit c076075
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ for special primitives from the [`typing`](https://docs.python.org/3/library/typ
for standard interpreter types from [`types`](https://docs.python.org/3/library/types.html#standard-interpreter-types) module:
* [`NoneType`](https://docs.python.org/3/library/types.html#types.NoneType)
* [`UnionType`](https://docs.python.org/3/library/types.html#types.UnionType)
* [`MappingProxyType`](https://docs.python.org/3/library/types.html#types.MappingProxyType)

for enumerations based on classes from the standard [`enum`](https://docs.python.org/3/library/enum.html) module:
* [`Enum`](https://docs.python.org/3/library/enum.html#enum.Enum)
Expand Down
6 changes: 6 additions & 0 deletions mashumaro/core/meta/types/unpack.py
Original file line number Diff line number Diff line change
Expand Up @@ -1230,6 +1230,12 @@ def inner_expr(
)
elif is_typed_dict(spec.origin_type):
return unpack_typed_dict(spec)
elif issubclass(spec.origin_type, types.MappingProxyType):
spec.builder.ensure_module_imported(types)
return (
f'types.MappingProxyType({{{inner_expr(0, "key")}: {inner_expr(1)}'
f" for key, value in {spec.expression}.items()}})"
)
elif ensure_generic_mapping(spec, args, typing.Mapping):
return (
f'{{{inner_expr(0, "key")}: {inner_expr(1)} '
Expand Down
10 changes: 9 additions & 1 deletion tests/test_data_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
WindowsPath,
)
from queue import Queue
from types import MappingProxyType
from typing import (
Any,
AnyStr,
Expand Down Expand Up @@ -123,6 +124,7 @@ class Fixture:
DICT = {"a": 1, "b": 2}
ORDERED_DICT = collections.OrderedDict(a=1, b=2)
DEFAULT_DICT = collections.defaultdict(int, a=1, b=2)
MAPPING_PROXY = MappingProxyType(DICT)
DEFAULT_NONE_DICT = collections.defaultdict(None, a=1, b=2)
COUNTER: Counter[str] = collections.Counter(a=1, b=2)
BYTES = b"123"
Expand Down Expand Up @@ -334,7 +336,13 @@ class Fixture:
)

if PY_39_MIN:
inner_values.append((ZoneInfo, ZoneInfo("Europe/Moscow"), "Europe/Moscow"))
inner_values.extend(
(
(ZoneInfo, ZoneInfo("Europe/Moscow"), "Europe/Moscow"),
(MappingProxyType[str, int], Fixture.MAPPING_PROXY, Fixture.DICT),
(MappingProxyType, Fixture.MAPPING_PROXY, Fixture.DICT),
)
)


hashable_inner_values = [
Expand Down
18 changes: 17 additions & 1 deletion tests/test_meta.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import collections
import collections.abc
import types
import typing
from dataclasses import InitVar, dataclass
from datetime import datetime
Expand All @@ -8,7 +9,12 @@
import typing_extensions

from mashumaro import DataClassDictMixin
from mashumaro.core.const import PEP_585_COMPATIBLE, PY_38, PY_310_MIN
from mashumaro.core.const import (
PEP_585_COMPATIBLE,
PY_38,
PY_39_MIN,
PY_310_MIN,
)
from mashumaro.core.meta.code.builder import CodeBuilder

# noinspection PyProtectedMember
Expand Down Expand Up @@ -229,6 +235,11 @@ def test_type_name():
)
assert type_name(typing.Optional[NoneType]) == "NoneType"

if PY_39_MIN:
assert (
type_name(types.MappingProxyType[int, int])
== "mappingproxy[int, int]"
)
if PY_310_MIN:
assert type_name(int | None) == "typing.Optional[int]"
assert type_name(None | int) == "typing.Optional[int]"
Expand Down Expand Up @@ -340,6 +351,11 @@ def test_type_name_short():
)
assert type_name(typing.Optional[NoneType], short=True) == "NoneType"

if PY_39_MIN:
assert (
type_name(types.MappingProxyType[int, int], short=True)
== "mappingproxy[int, int]"
)
if PY_310_MIN:
assert type_name(int | None, short=True) == "Optional[int]"
assert type_name(None | int, short=True) == "Optional[int]"
Expand Down

0 comments on commit c076075

Please sign in to comment.