freelancer/pavlova

Error while parsing nested optional dataclasses

Closed this issue · 1 comments

Do you want to request a feature or report a bug?
Bug

What is the current behavior?

Traceback (most recent call last):
  File "/usr/local/lib/python3.7/site-packages/pavlova/__init__.py", line 105, in from_mapping
    path + (field.name,),
  File "/usr/local/lib/python3.7/site-packages/pavlova/__init__.py", line 151, in parse_field
    input_value, field_type, path
  File "/usr/local/lib/python3.7/site-packages/pavlova/parsers.py", line 165, in parse_input
    return self.pavlova.parse_field(input_value, real_field_type, path)
  File "/usr/local/lib/python3.7/site-packages/pavlova/__init__.py", line 132, in parse_field
    input_value, field_type, path
  File "/usr/local/lib/python3.7/site-packages/pavlova/__init__.py", line 89, in from_mapping
    if field.name not in input_mapping:
TypeError: argument of type 'NoneType' is not iterable

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "pav.py", line 19, in <module>
    print(Pavlova().from_mapping({'d': None}, Outer))
  File "/usr/local/lib/python3.7/site-packages/pavlova/__init__.py", line 112, in from_mapping
    field.type,
pavlova.PavlovaParsingError: argument of type 'NoneType' is not iterable

Steps to reproduce

from typing import Optional
from pavlova import Pavlova
from dataclasses import dataclass

@dataclass
class NestedSample:
    item: str

@dataclass
class Outer:
    nested: Optional[NestedSample] = None

# This fails
Pavlova().from_mapping({'nested': None}, Outer)

What is the expected behavior?

# from_mapping should return the following dataclass
Outer(nested=None)

Which versions of Pavlova was affected by this issue
Version 0.1.2

Fixed in v0.1.3