Skip to content

Commit

Permalink
Inline create_instance function
Browse files Browse the repository at this point in the history
  • Loading branch information
konradhalas committed Jan 10, 2023
1 parent 732546f commit c831d57
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 34 deletions.
7 changes: 4 additions & 3 deletions dacite/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
from dacite.data import Data
from dacite.dataclasses import (
get_default_value_for_field,
create_instance,
DefaultValueNotFoundError,
get_fields,
is_frozen,
Expand Down Expand Up @@ -79,8 +78,10 @@ def from_dict(data_class: Type[T], data: Data, config: Optional[Config] = None)
init_values[field.name] = value
elif not is_frozen(data_class):
post_init_values[field.name] = value

return create_instance(data_class=data_class, init_values=init_values, post_init_values=post_init_values)
instance = data_class(**init_values)
for key, value in post_init_values.items():
setattr(instance, key, value)
return instance


def _build_value(type_: Type, data: Any, config: Config) -> Any:
Expand Down
8 changes: 0 additions & 8 deletions dacite/dataclasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
from typing import Type, Any, TypeVar, List

from dacite.cache import cache
from dacite.data import Data
from dacite.types import is_optional

T = TypeVar("T", bound=Any)
Expand All @@ -23,13 +22,6 @@ def get_default_value_for_field(field: Field, type_: Type) -> Any:
raise DefaultValueNotFoundError()


def create_instance(data_class: Type[T], init_values: Data, post_init_values: Data) -> T:
instance = data_class(**init_values)
for key, value in post_init_values.items():
setattr(instance, key, value)
return instance


@cache
def get_fields(data_class: Type[T]) -> List[Field]:
fields = getattr(data_class, _FIELDS)
Expand Down
24 changes: 1 addition & 23 deletions tests/test_dataclasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import pytest

from dacite.dataclasses import get_default_value_for_field, create_instance, DefaultValueNotFoundError, is_frozen
from dacite.dataclasses import get_default_value_for_field, DefaultValueNotFoundError, is_frozen


def test_get_default_value_for_field_with_default_value():
Expand Down Expand Up @@ -53,28 +53,6 @@ class X:
get_default_value_for_field(field=dataclass_field, type_=dataclass_field.type)


def test_create_instance_with_simple_data_class():
@dataclass
class X:
i: int

instance = create_instance(data_class=X, init_values={"i": 1}, post_init_values={})

assert instance == X(i=1)


def test_create_instance_with_post_init_values():
@dataclass
class X:
i: int
j: int = field(init=False)

instance = create_instance(data_class=X, init_values={"i": 1}, post_init_values={"j": 2})

assert instance.i == 1
assert instance.j == 2


def test_is_frozen_with_frozen_dataclass():
@dataclass(frozen=True)
class X:
Expand Down

0 comments on commit c831d57

Please sign in to comment.