Skip to content

Commit

Permalink
Fix linting errors
Browse files Browse the repository at this point in the history
  • Loading branch information
natemcmaster committed Jun 13, 2021
1 parent 42e7285 commit d2ba3d7
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 10 deletions.
6 changes: 5 additions & 1 deletion src/cattr/converters.py
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,11 @@ def _structure_default(self, obj, cl):
return obj

if is_generic(cl):
fn = make_dict_structure_fn(cl, self, _cattrs_prefer_attrib_converters=self._prefer_attrib_converters)
fn = make_dict_structure_fn(
cl,
self,
_cattrs_prefer_attrib_converters=self._prefer_attrib_converters,
)
self.register_structure_hook(cl, fn)
return fn(obj)

Expand Down
15 changes: 7 additions & 8 deletions src/cattr/gen.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@
import re
import uuid
from dataclasses import is_dataclass
from typing import Any, Optional, Type, TypeVar
from typing import Any, Optional, TYPE_CHECKING, Type, TypeVar

import attr
from attr import NOTHING, resolve_types

from ._compat import adapted_fields, get_args, get_origin, is_bare, is_generic
from .errors import StructureHandlerNotFoundError

if TYPE_CHECKING:
from cattr.converters import Converter


@attr.s(slots=True, frozen=True)
class AttributeOverride(object):
Expand Down Expand Up @@ -132,7 +135,7 @@ def generate_mapping(cl: Type, old_mapping):

def make_dict_structure_fn(
cl: Type,
converter,
converter: "Converter",
_cattrs_forbid_extra_keys: bool = False,
_cattrs_use_linecache: bool = True,
_cattrs_prefer_attrib_converters: bool = False,
Expand Down Expand Up @@ -212,19 +215,15 @@ def make_dict_structure_fn(
f" '{ian}': {struct_handler_name}(o['{kn}'], type_{an}),"
)
else:
lines.append(
f" '{ian}': o['{kn}'],"
)
lines.append(f" '{ian}': o['{kn}'],")
else:
post_lines.append(f" if '{kn}' in o:")
if handler:
post_lines.append(
f" res['{ian}'] = {struct_handler_name}(o['{kn}'], type_{an})"
)
else:
post_lines.append(
f" res['{ian}'] = o['{kn}']"
)
post_lines.append(f" res['{ian}'] = o['{kn}']")

lines.append(" }")
if _cattrs_forbid_extra_keys:
Expand Down
1 change: 1 addition & 0 deletions tests/test_generics.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ def test_raises_if_no_generic_params_supplied(converter):

assert exc.value.type_ is T


def test_unstructure_generic_attrs():
c = GenConverter()

Expand Down
3 changes: 2 additions & 1 deletion tests/test_structure.py
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,7 @@ class Bar(object):

assert exc.value.type_ is Bar


@given(data(), enums_of_primitives())
def test_structuring_enums(data, enum):
"""Test structuring enums by their values."""
Expand All @@ -331,7 +332,7 @@ def test_structuring_unsupported():
converter.structure(1, Converter)

assert exc.value.type_ is Converter

with raises(StructureHandlerNotFoundError) as exc:
converter.structure(1, Union[int, str])

Expand Down

0 comments on commit d2ba3d7

Please sign in to comment.