Skip to content

Commit

Permalink
Fixed new TypedDict test fixtures for versions 3.10-
Browse files Browse the repository at this point in the history
  • Loading branch information
sg495 committed Mar 17, 2024
1 parent 26de932 commit d87c635
Showing 1 changed file with 45 additions and 32 deletions.
77 changes: 45 additions & 32 deletions test/test_00_validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
import collections.abc as collections_abc
import sys
import typing
from typing_extensions import Literal, TypedDict, Required, NotRequired

import pytest

from typing_validation import validate, validation_aliases
Expand All @@ -15,12 +13,21 @@
_is_typed_dict,
)

if sys.version_info[1] >= 8:
from typing import Literal
else:
from typing_extensions import Literal

if sys.version_info[1] >= 9:
from typing import TypedDict
else:
from typing_extensions import TypedDict

if sys.version_info[1] >= 10:
from types import UnionType
else:
UnionType = None


_basic_types = [
bool,
int,
Expand Down Expand Up @@ -521,17 +528,6 @@ class TD2(TypedDict, total=False):
x: str
w: typing.List[str]


class TD3(TypedDict, total=False):
x: Required[str]
w: typing.List[str]


class TD4(TypedDict):
x: str
w: NotRequired[typing.List[str]]


_typed_dict_cases: typing.Dict[typing.Any, typing.List[typing.Any]] = {}
_typed_dict_cases[TD1b] = [
{"x": 1, "y": 1.5, "z": ["hello", "bye bye"]},
Expand All @@ -550,14 +546,27 @@ class TD4(TypedDict):
{"w": ["hello", "bye bye"]},
{},
]
_typed_dict_cases[TD3] = [
{"x": "hello", "w": ["hello", "bye bye"]},
{"x": "hello"},
]
_typed_dict_cases[TD4] = [
{"x": "hello", "w": ["hello", "bye bye"]},
{"x": "hello"},
]

if sys.version_info[1] >= 11:
from typing import Required, NotRequired

class TD3(TypedDict, total=False):
x: Required[str] # pyright: ignore
w: typing.List[str]


class TD4(TypedDict):
x: str
w: NotRequired[typing.List[str]] # pyright: ignore

_typed_dict_cases[TD3] = [
{"x": "hello", "w": ["hello", "bye bye"]},
{"x": "hello"},
]
_typed_dict_cases[TD4] = [
{"x": "hello", "w": ["hello", "bye bye"]},
{"x": "hello"},
]


@pytest.mark.parametrize("t, vals", _typed_dict_cases.items())
Expand Down Expand Up @@ -589,16 +598,20 @@ def test_typed_dict_cases(t: typing.Any, vals: typing.List[typing.Any]) -> None:
{"w": 0},
{"x": 0},
]
_invalid_typed_dict_cases[TD3] = [
*_invalid_typed_dict_cases[TD2],
{"w": ["hello", "bye bye"]},
{},
]
_invalid_typed_dict_cases[TD4] = [
*_invalid_typed_dict_cases[TD2],
{"w": ["hello", "bye bye"]},
{},
]


if sys.version_info[1] >= 11:

_invalid_typed_dict_cases[TD3] = [
*_invalid_typed_dict_cases[TD2],
{"w": ["hello", "bye bye"]},
{},
]
_invalid_typed_dict_cases[TD4] = [
*_invalid_typed_dict_cases[TD2],
{"w": ["hello", "bye bye"]},
{},
]


@pytest.mark.parametrize("t, vals", _invalid_typed_dict_cases.items())
Expand Down

0 comments on commit d87c635

Please sign in to comment.