Skip to content

Commit

Permalink
Merge branch 'master' into forward-ref
Browse files Browse the repository at this point in the history
  • Loading branch information
Fatal1ty committed Aug 30, 2023
2 parents 3f1f8bf + a86eaf8 commit 7cb3692
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 34 deletions.
10 changes: 2 additions & 8 deletions mashumaro/config.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
from typing import Any, Callable, Dict, List, Optional, Type, Union
from typing import Any, Callable, Dict, List, Literal, Optional, Type, Union

from mashumaro.core.const import PEP_586_COMPATIBLE, Sentinel
from mashumaro.core.const import Sentinel
from mashumaro.dialect import Dialect
from mashumaro.types import Discriminator, SerializationStrategy

if PEP_586_COMPATIBLE:
from typing import Literal # type: ignore
else:
from typing_extensions import Literal # type: ignore


__all__ = [
"BaseConfig",
"TO_DICT_ADD_BY_ALIAS_FLAG",
Expand Down
4 changes: 0 additions & 4 deletions mashumaro/core/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,10 @@
"PY_38",
"PY_39",
"PY_310",
"PY_38_MIN",
"PY_39_MIN",
"PY_310_MIN",
"PY_311_MIN",
"PEP_585_COMPATIBLE",
"PEP_586_COMPATIBLE",
"Sentinel",
]

Expand All @@ -24,10 +22,8 @@
PY_311_MIN = PY_311 or PY_312_MIN
PY_310_MIN = PY_310 or PY_311_MIN
PY_39_MIN = PY_39 or PY_310_MIN
PY_38_MIN = PY_38 or PY_39_MIN

PEP_585_COMPATIBLE = PY_39_MIN # Type Hinting Generics In Standard Collections
PEP_586_COMPATIBLE = PY_38_MIN # Literal Types


class Sentinel(enum.Enum):
Expand Down
6 changes: 1 addition & 5 deletions mashumaro/core/meta/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
from mashumaro.core.const import (
PEP_585_COMPATIBLE,
PY_38,
PY_38_MIN,
PY_39,
PY_39_MIN,
PY_310_MIN,
Expand Down Expand Up @@ -424,10 +423,7 @@ def is_final(typ: Type) -> bool:


def is_init_var(typ: Type) -> bool:
if PY_38_MIN:
return isinstance(typ, dataclasses.InitVar)
else:
raise NotImplementedError
return isinstance(typ, dataclasses.InitVar)


def get_class_that_defines_method(
Expand Down
24 changes: 7 additions & 17 deletions tests/test_meta.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,14 @@
import collections
import collections.abc
import typing
from dataclasses import dataclass
from dataclasses import InitVar, dataclass
from datetime import datetime

import pytest
import typing_extensions

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

# noinspection PyProtectedMember
Expand Down Expand Up @@ -88,10 +83,9 @@ def test_is_generic_unsupported_python(mocker):
is_generic(int)


def test_is_init_var_unsupported_python(mocker):
mocker.patch("mashumaro.core.meta.helpers.PY_38_MIN", False)
with pytest.raises(NotImplementedError):
is_init_var(int)
def test_is_init_var():
assert is_init_var(InitVar[int])
assert not is_init_var(int)


def test_is_literal_unsupported_python(mocker):
Expand Down Expand Up @@ -542,12 +536,8 @@ def test_get_literal_values():


def test_type_name_literal():
if PY_38_MIN:
module = typing
else:
module = typing_extensions
assert type_name(
getattr(module, "Literal")[
getattr(typing, "Literal")[
1,
"a",
b"\x00",
Expand All @@ -564,7 +554,7 @@ def test_type_name_literal():
typing_extensions.Literal[typing_extensions.Literal["b", "c"]],
]
) == (
f"{module.__name__}.Literal[1, 'a', b'\\x00', True, False, None, "
f"typing.Literal[1, 'a', b'\\x00', True, False, None, "
"tests.entities.MyEnum.a, tests.entities.MyStrEnum.a, "
"tests.entities.MyNativeStrEnum.a, tests.entities.MyIntEnum.a, "
"tests.entities.MyFlag.a, tests.entities.MyIntFlag.a, 2, 3, 'b', 'c']"
Expand Down

0 comments on commit 7cb3692

Please sign in to comment.