dataclass-mapper / dataclass-mapper

Lib for autogenerating mappers between dataclasses
MIT License
26 stars 4 forks source link

Exceptions caused by unsupported generics don't mention field names #28

Open sh-at-cs opened 3 months ago

sh-at-cs commented 3 months ago

Steps to reproduce

Consider:

from dataclasses import dataclass
from typing import Generic, TypeVar

from dataclass_mapper import create_mapper

T = TypeVar("T")

class SomeGeneric(Generic[T]):
    pass

@dataclass
class A:
    u: SomeGeneric[int]

@dataclass
class B:
    u: int

create_mapper(A, B)

As expected, this results in an exception, but the exception text doesn't mention the field name this is about:

TypeError: Field type '__main__.SomeGeneric[int]' is not supported.

Expected behavior

Contrast this with the much more detailed exception text one gets for mapping between non-generic unknown types. E.g. for

...

class SomeType:
    pass

@dataclass
class A:
    u: SomeType

...

The exception text reads

TypeError: 'u' of type 'SomeType' of 'A' cannot be converted to 'u' of type 'int' of 'B'

which is of course much better!

IMHO, the "not supported" exceptions for generics should look like that as well.

Version info

Tested in version 2.0.0a1.

jakobkogler commented 3 months ago

I kinda prefer the original message, because the error is more specific. Not only does the library not know, how to convert between the two types, it doesn't even understand the type in the first place.

But I guess your idea also makes sense. I'll think over it.

sh-at-cs commented 3 months ago

Yeah this is the least important of the batch of issues I raised, I just thought it might make things less confusing for people who are mapping between classes with a lot of fields and are trying to figure out where the error even originates. But on the other hand, they could also just find that out by searching for the type mentioned in the error message :thinking: So it's probably fine if this remains as it is?