Quantcast
Channel: Active questions tagged config - Stack Overflow
Viewing all articles
Browse latest Browse all 5054

Mypy Config Class

$
0
0

I am currently doing some work where I want to load a config from yaml into a class so that class attributes get specified. Depending on the work I am doing I have a different config class.

In the class I pre-specify the expected arguments as follows and then load data in from the file using BaseConfig::load_config(). This checks to see if the class has the corresponding attribute before using setattr() to assign the value.

class AutoencoderConfig(BaseConfig):    def __init__(self, config_path: Path) -> None:        super().__init__(config_path)        self.NTRAIN: Optional[int] = None        self.NVALIDATION: Optional[int] = None        # ... more below        self.load_config()

When I am referring to attributes of the AutoencoderConfig in other scripts, I might say something like: config.NTRAIN. While this works just fine, Mypy does not like this and gives the following error:

path/to/file.py:linenumber: error: Unsupported operand types for / ("None" and "int")

The error arises because at the time of the check, the values from the yaml file have not been loaded yet, and the types are specified with self.NTRAIN: Optional[int] = None.

Is there anyway I can avoid this without having to place: # type: ignore at the end of every line that I refer to the config class?

Are there any best-practices for using a config class like this?


Viewing all articles
Browse latest Browse all 5054

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>