Generics¶
Tip
Code for examples on this page can be found in examples/5_generics.
Confarg handles the following built-in generics.
Literal¶
The Literal generic type allows you to limit inputs to a fixed set of options.
For example, this configuration
limits the set of possible values of input to "hello" or "world".
$ uv run literal.py --input hello
Config(input='hello')
$ uv run literal.py --input bye # Error: not in the set of allowed values
...
Annotated¶
The Annotated generic type allows you to attach various metadata to a type.
Confarg can handle Annotated types but ignores any metadata attached to them.
Final¶
The Final generic type signals that the annotated value should not be changed.
However, it can be set to any value, even if it comes with a default value, which can be surprising.
Take this configuration:
The final value can be set to a string that is not the default value:
This is consistent with the fact that a Final value may not have a default value at all.
Tip
A truly predefined and immutable value should probably not be part of the configuration.
Your own generic classes¶
A class of your own may be generic too. Each field reads its type with the class's type parameters bound, as a type checker reads it:
@dataclass
class Config:
"""Configuration with two boxes, each holding its own type."""
size: Box[int]
label: Box[str]
size.item is an int and label.item a str, on the command line, in an environment variable and in a configuration file alike:
$ uv run generic_class.py --size.item 3 --label.item 3
Config(size=Box(item=3), label=Box(item='3'))
$ uv run generic_class.py --size.item x --label.item 3 # Error: a Box[int] holds an int
...
A generic class annotated without its arguments, a bare Box, reads T as a type checker does: its default where it declares one (class Box[T = int], Python 3.13 and later), else Any, which keeps a value as it is written.
A subclass that fixes the argument, class IntBox(Box[int]), reads its item as an int wherever it is used.