[Python] How does one ignore extra arguments passed to a dataclass?

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

  1. Stack

    Stack Membro Participativo

    I'd like to create a config dataclass in order to simplify whitelisting of and access to specific environment variables (typing os.environ['VAR_NAME'] is tedious relative to config.VAR_NAME). I therefore need to ignore unused environment variables in my dataclass's __init__ function, but I don't know how to extract the default __init__ in order to wrap it with, e.g., a function that also includes *_ as one of the arguments.

    import os
    from dataclasses import dataclass

    @dataclass
    class Config:
    VAR_NAME_1: str
    VAR_NAME_2: str

    config = Config(**os.environ)


    Running this gives me TypeError: __init__() got an unexpected keyword argument 'SOME_DEFAULT_ENV_VAR'.

    Continue reading...

Compartilhe esta Página