adaptix icon indicating copy to clipboard operation
adaptix copied to clipboard

Make errors really verbose

Open Tishka17 opened this issue 6 years ago • 1 comments

It is very difficult to understand what going wrong during parsing

Tishka17 avatar Oct 03 '19 10:10 Tishka17

Using factory.load(data, _cls) with missing fields in data and no defaults for fields in _cls will result in TypeError. Would like to see custom Exception like MissingFieldError

Example:

from dataclasses import dataclass
import dataclass_factory


@dataclass
class Book:
    title: str  # <--- No default values 
    price: int

data = {}  # <--- Missing fields

factory = dataclass_factory.Factory()
book: Book = factory.load(data, Book)  # Results in __init__() missing 2 required positional arguments: 'title' and 'price'
serialized = factory.dump(book)

Expected:

factory = dataclass_factory.Factory()
try:
     book: Book = factory.load(data, Book)
     serialized = factory.dump(book)
except MissingFieldError as e:
    print(f"Fields {e.fields} are missing.")

lainisourgod avatar Mar 10 '20 07:03 lainisourgod