When i create and use a dataclass the “normal way” I can run and type check my code mypy without problem.
For instance, this code works perfectly fine:
@dataclass
class Person2:
name : str
age : int
height : float
def f2(p : Person2):
print(p)
But if I try to craete a dataclass with make_dataclass, I have a problem.
For instance:
types_list = [('name', str), ('age', int), ('height', float)]
Person = make_dataclass('Person', types_list)
a=Person(name="n",age=2, height=4.3)
def f(p : Person):
print(p)
q.py:10: error: Variable “q.Person” is not valid as a type [valid-type]
Why does this problem occurs and is it possible to fix it
Information about Person
can be gleaned from a static construct like a class
statement, but not from a particular call to a function about which we only know the names and types of its own arguments, not the values of those arguments.
Types constructed from values (rather than just other types) are known as dependent types, and Python’s type-checking does not currently support dependent typing.
2