Skip to content

Commit

Permalink
refactor: rename typed_dict/named_tuple
Browse files Browse the repository at this point in the history
  • Loading branch information
PrettyWood committed Jan 21, 2021
1 parent d737f07 commit 3fe8ff9
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 12 deletions.
4 changes: 2 additions & 2 deletions pydantic/annotated_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ def create_model_from_namedtuple(namedtuple_cls: Type['NamedTuple'], **kwargs: A
but also with `collections.namedtuple` without any, in which case we consider the type
of all the fields to be `Any`
"""
named_tuple_annotations: Dict[str, Type[Any]] = getattr(
namedtuple_annotations: Dict[str, Type[Any]] = getattr(
namedtuple_cls, '__annotations__', {k: Any for k in namedtuple_cls._fields}
)
field_definitions: Dict[str, Any] = {
field_name: (field_type, Required) for field_name, field_type in named_tuple_annotations.items()
field_name: (field_type, Required) for field_name, field_type in namedtuple_annotations.items()
}
return create_model(f'{namedtuple_cls.__name__}Model', **kwargs, **field_definitions)
8 changes: 4 additions & 4 deletions pydantic/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -543,17 +543,17 @@ def pattern_validator(v: Any) -> Pattern[str]:
NamedTupleT = TypeVar('NamedTupleT', bound=NamedTuple)


def make_named_tuple_validator(namedtuple_cls: Type[NamedTupleT]) -> Callable[[Tuple[Any, ...]], NamedTupleT]:
def make_namedtuple_validator(namedtuple_cls: Type[NamedTupleT]) -> Callable[[Tuple[Any, ...]], NamedTupleT]:
from .annotated_types import create_model_from_namedtuple

NamedTupleModel = create_model_from_namedtuple(namedtuple_cls)

def named_tuple_validator(values: Tuple[Any, ...]) -> NamedTupleT:
def namedtuple_validator(values: Tuple[Any, ...]) -> NamedTupleT:
dict_values: Dict[str, Any] = dict(zip(NamedTupleModel.__annotations__, values))
validated_dict_values: Dict[str, Any] = dict(NamedTupleModel(**dict_values))
return namedtuple_cls(**validated_dict_values)

return named_tuple_validator
return namedtuple_validator


def make_typeddict_validator(
Expand Down Expand Up @@ -661,7 +661,7 @@ def find_validators( # noqa: C901 (ignore complexity)
return
if is_namedtuple(type_):
yield tuple_validator
yield make_named_tuple_validator(type_)
yield make_namedtuple_validator(type_)
return
if is_typeddict(type_):
yield make_typeddict_validator(type_, config)
Expand Down
12 changes: 6 additions & 6 deletions tests/test_annotated_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
from pydantic import BaseModel, ValidationError


def test_named_tuple():
def test_namedtuple():
Position = namedtuple('Pos', 'x y')

class Event(NamedTuple):
Expand Down Expand Up @@ -60,7 +60,7 @@ class Model(BaseModel):


@pytest.mark.skipif(not TypedDict, reason='typing_extensions not installed')
def test_typed_dict():
def test_typeddict():
class TD(TypedDict):
a: int
b: int
Expand All @@ -85,7 +85,7 @@ class Model(BaseModel):


@pytest.mark.skipif(not TypedDict, reason='typing_extensions not installed')
def test_typed_dict_non_total():
def test_typeddict_non_total():
class FullMovie(TypedDict, total=True):
name: str
year: int
Expand Down Expand Up @@ -115,7 +115,7 @@ class Model(BaseModel):


@pytest.mark.skipif(not TypedDict, reason='typing_extensions not installed')
def test_partial_new_typed_dict():
def test_partial_new_typeddict():
class OptionalUser(TypedDict, total=False):
name: str

Expand All @@ -130,7 +130,7 @@ class Model(BaseModel):


@pytest.mark.skipif(not LegacyTypedDict, reason='python 3.9+ is used or typing_extensions is installed')
def test_partial_legacy_typed_dict():
def test_partial_legacy_typeddict():
class OptionalUser(LegacyTypedDict, total=False):
name: str

Expand All @@ -157,7 +157,7 @@ class Model(BaseModel):


@pytest.mark.skipif(not TypedDict, reason='typing_extensions not installed')
def test_typed_dict_extra():
def test_typeddict_extra():
class User(TypedDict):
name: str
age: int
Expand Down

0 comments on commit 3fe8ff9

Please sign in to comment.