Skip to content

Commit

Permalink
fixed typing issues
Browse files Browse the repository at this point in the history
  • Loading branch information
anikolaienko committed Jul 18, 2021
1 parent 02fa0ae commit 4321b1a
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 37 deletions.
6 changes: 5 additions & 1 deletion automapper/extensions/default.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@ def __init_method_spec_func__(target_cls: Type[T]) -> Iterable[str]:
If __init__ of the target class accepts `*args` or `**kwargs`
then current spec function won't work properly and another spec_func should be added
"""
return (field for field in target_cls.__init__.__annotations__.keys() if field not in _IGNORED_FIELDS)
return (
field
for field in target_cls.__init__.__annotations__.keys()
if field not in _IGNORED_FIELDS
)


def extend(mapper: Mapper) -> None:
Expand Down
8 changes: 5 additions & 3 deletions automapper/mapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ def add(self, source_cls: Type[S], target_cls: Type[T]) -> None:
)
self._mappings[source_cls] = target_cls

def map(self, obj: object, skip_none_values: bool = False) -> object:
def map(self, obj: object, skip_none_values: bool = False) -> T:
"""Produces output object mapped from source object and custom arguments"""
obj_type = type(obj)
if obj_type not in self._mappings:
Expand Down Expand Up @@ -154,15 +154,17 @@ def _map_subobject(
else:
result = type(obj)( # type: ignore [call-arg]
[
self._map_subobject(x, _visited_stack, skip_none_values=skip_none_values)
self._map_subobject(
x, _visited_stack, skip_none_values=skip_none_values
)
for x in cast(Iterable[Any], obj)
]
)
else:
result = deepcopy(obj)

_visited_stack.remove(obj_id)

return result

def _map_common(
Expand Down
3 changes: 1 addition & 2 deletions automapper/mapper_initializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@


def create_mapper() -> Mapper:
"""Returns a Mapper instance with preloaded extensions
"""
"""Returns a Mapper instance with preloaded extensions"""
mapper = Mapper()
extensions = glob.glob(join(dirname(__file__), __EXTENSIONS_FOLDER__, "*.py"))
for extension in extensions:
Expand Down
28 changes: 14 additions & 14 deletions tests/test_automapper.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
from unittest import TestCase
from typing import Protocol, Type, TypeVar, Iterable, cast
from typing import Protocol, Type, TypeVar, Iterable, Optional, Any, cast

import pytest

from automapper import (
create_mapper,
MappingError,
DuplicatedRegistrationError
)
from automapper import create_mapper, MappingError, DuplicatedRegistrationError


T = TypeVar("T")
Expand All @@ -30,13 +26,13 @@ def fields(cls) -> Iterable[str]:


class AnotherClass:
def __init__(self, text: str, num: int) -> None:
def __init__(self, text: Optional[str], num: int) -> None:
self.text = text
self.num = num


class ClassWithoutInitAttrDef:
def __init__(self, **kwargs) -> None:
def __init__(self, **kwargs: Any) -> None:
self.data = kwargs.copy()

@classmethod
Expand Down Expand Up @@ -82,7 +78,9 @@ def test_add_spec__error_on_adding_same_class_spec(self):
def test_add_spec__adds_to_internal_collection_for_classifier(self):
self.mapper.add_spec(classifier_func, spec_func)
assert classifier_func in self.mapper._classifier_specs
assert ["text", "num"] == self.mapper._classifier_specs[classifier_func](ClassWithoutInitAttrDef)
assert ["text", "num"] == self.mapper._classifier_specs[classifier_func](
ClassWithoutInitAttrDef
)

def test_add_spec__error_on_duplicated_registration(self):
self.mapper.add_spec(classifier_func, spec_func)
Expand All @@ -95,7 +93,7 @@ def test_add__appends_class_to_class_mapping(self):

self.mapper.add_spec(AnotherClass, custom_spec_func)
self.mapper.add(ChildClass, AnotherClass)
result = self.mapper.map(ChildClass(10, "test_message", True))
result: AnotherClass = self.mapper.map(ChildClass(10, "test_message", True))

assert isinstance(result, AnotherClass)
assert result.text == "test_message"
Expand All @@ -119,8 +117,10 @@ def test_to__mapper_works_with_provided_init_extension(self):

def test_map__skip_none_values_from_source_object(self):
self.mapper.add_spec(classifier_func, spec_func)

obj = self.mapper.to(ClassWithoutInitAttrDef).map(AnotherClass(None, 11), skip_none_values=True)

obj = self.mapper.to(ClassWithoutInitAttrDef).map(
AnotherClass(None, 11), skip_none_values=True
)

assert "text" not in obj.data
assert "num" in obj.data
Expand All @@ -130,8 +130,8 @@ def test_map__pass_none_values_from_source_object(self):
self.mapper.add_spec(classifier_func, spec_func)

obj = self.mapper.to(ClassWithoutInitAttrDef).map(AnotherClass(None, 11))

assert "text" in obj.data
assert "num" in obj.data
assert obj.data.get("text") == None
assert obj.data.get("text") is None
assert obj.data.get("num") == 11
20 changes: 7 additions & 13 deletions tests/test_for_complex_obj.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
from unittest import TestCase
from typing import Protocol, TypeVar, Iterable, Optional
from typing import Protocol, TypeVar, Iterable, Optional, Any

import pytest

from automapper import (
create_mapper,
CircularReferenceError
)
from automapper import create_mapper, CircularReferenceError


T = TypeVar("T")
Expand Down Expand Up @@ -35,7 +32,7 @@ def __init__(self, text: str, num: int) -> None:


class ClassWithoutInitAttrDef:
def __init__(self, **kwargs) -> None:
def __init__(self, **kwargs: Any) -> None:
self.data = kwargs.copy()

@classmethod
Expand Down Expand Up @@ -76,15 +73,12 @@ def setUp(self):
self.mapper = create_mapper()

def test_map__complext_obj(self):
complex_obj = ComplexClass(
obj=ChildClass(15, "nested_obj_msg", True),
text="obj_msg"
)
complex_obj = ComplexClass(obj=ChildClass(15, "nested_obj_msg", True), text="obj_msg")
self.mapper.add(ChildClass, AnotherClass)
self.mapper.add(ComplexClass, AnotherComplexClass)

result = self.mapper.map(complex_obj)
result: AnotherComplexClass = self.mapper.map(complex_obj)

assert isinstance(result, AnotherComplexClass)
assert isinstance(result.obj, AnotherClass)
assert result.obj.text == "nested_obj_msg"
Expand All @@ -98,7 +92,7 @@ def test_map__complext_obj_with_circular_ref(self):
self.mapper.add(ComplexObjWithCircularRef, ComplexObjWithCircularRef)
self.mapper.add(WrapperClass, WrapperClass)

result = self.mapper.map(source)
result: ComplexObjWithCircularRef = self.mapper.map(source)
assert result.child.num == 15

# adding circular ref
Expand Down
8 changes: 4 additions & 4 deletions tests/test_for_default_extention.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from unittest import TestCase
from typing import Iterable, Protocol, Type, TypeVar, cast, runtime_checkable
from typing import Iterable, Protocol, Type, TypeVar, Any, cast, runtime_checkable
from collections import namedtuple

from automapper import Mapper
Expand All @@ -10,7 +10,7 @@


class ClassWithoutInitAttrDef:
def __init__(self, **kwargs) -> None:
def __init__(self, **kwargs: Any) -> None:
self.data = kwargs.copy()

@classmethod
Expand Down Expand Up @@ -43,7 +43,7 @@ def test_default_extension__does_not_work_for_kwargs_in_init(self):

def test_custom_spec_for_class__works_for_kwargs_in_init(self):
self.mapper.add_spec(ClassWithoutInitAttrDef, spec_func)
source = namedtuple("SourceObj", ["text", "num"])("text_msg", 11)
source = namedtuple("SourceObj", ["text", "num"])("text_msg", 11) # type: ignore [call-arg]

obj = self.mapper.to(ClassWithoutInitAttrDef).map(source)

Expand All @@ -52,7 +52,7 @@ def test_custom_spec_for_class__works_for_kwargs_in_init(self):

def test_custom_spec_with_classifier__works_for_kwargs_in_init(self):
self.mapper.add_spec(classifier_func, spec_func)
source = namedtuple("SourceObj", ["text", "num"])("text_msg", 11)
source = namedtuple("SourceObj", ["text", "num"])("text_msg", 11) # type: ignore [call-arg]

obj = self.mapper.to(ClassWithoutInitAttrDef).map(source)

Expand Down

0 comments on commit 4321b1a

Please sign in to comment.