[Python] How can use statically typed check a dataclass create with make_dataclass

Discussão em 'Python' iniciado por Stack, Dezembro 20, 2024.

  1. Stack

    Stack Membro Participativo

    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

    Continue reading...

Compartilhe esta Página