konradhalas / dacite

Simple creation of data classes from dictionaries.
MIT License
1.72k stars 106 forks source link

from_dict genric typings intelissense not working #162

Closed Pacheco95 closed 2 years ago

Pacheco95 commented 2 years ago

In v1.6.0 from_dict is defined as

def from_dict(data_class: Type[T], data: Data, config: Optional[Config] = None) -> T

Intellissense is not working. I think the correct way to define generic functions is like

T = TypeVar("T")

def from_dict(data_class: T, data: Data, config: Optional[Config] = None) -> T

I just edited the source code directly from PyCharm and everything works well

See documentation https://docs.python.org/3/library/typing.html#typing.TypeVar

BurningKarl commented 2 years ago

Not the developer here, but I'm pretty sure that the current type annotations are actually correct. data_class is not an object of type T but the type itself. Consider the examples at https://docs.python.org/3/library/typing.html#typing.Type. Basically,

a = 3         # Has type 'int'
b = int       # Has type 'Type[int]'
c = type(a)   # Also has type 'Type[int]'

If the dataclass we want to instantiate using from_dict is User then we would call the function like this: from_dict(data_class=User, data=data) for some data and expect that it returns an instance of User. So the parameter data_class has type Type[User] and the return type is User.

Could you maybe provide the issues that Intellisense has with your code and the original function definition?

konradhalas commented 2 years ago

Hi @Pacheco95 - thank you for your issue.

I agree with @BurningKarl that it should be Type[T] as it is now.