[Python] How to proper instantiate TypeVar typing declaration with a bound variable?

Discussão em 'Python' iniciado por Stack, Setembro 16, 2024.

  1. Stack

    Stack Membro Participativo

    Based on my understanding, I have coded an TypeVar declaration in the following format:

    def generate_class(config):
    class ClassA:
    attr1: str = config["hello"]
    attr2: int = process_config(config)
    return classA

    ClassA = generate_class(config)
    ClassAType = TypeVar("ClassAType", bound=ClassA)

    vara_info: ClassAType = execute_var()
    vara_amount: float = process_var(vara_info)


    It used to be able to work properly and VSCode will also be able to map the internal attributes of VarA (as it is a class object) successfully but recently, it is throwing a linting error: "Variable not allowed in type expression".

    What is it that I am doing wrong now? I cannot seem to find an answer online for this formatting error despite it not throwing any error during runtime.

    I have tried researching on using other Var types instead such as NewType but based on documentation, those are not for binding variables to Typing.

    My current python specifications are: python 3.10.6 typing-extensions==4.9.0

    Continue reading...

Compartilhe esta Página