There are four key elements in this example:
@dataclassdescribes the application's configuration
ConfigStoremanages the Structured Config
duck typedas a
MySQLConfiginstead of a
- There is a subtle typo in the code below, can you spot it?
In this example, the config node stored in the
ConfigStore replaces the traditional
Duck-typing enables static type checking
Duck-typing the config object as
MySQLConfig enables static type checkers like
mypy to catch
type errors before you run your code:
Structured Configs enable Hydra to catch type errors at runtime
If you forget to run
mypy, Hydra will report the error at runtime:
Hydra will also catch typos, or type errors in the command line:
We will see additional types of runtime errors that Hydra can catch later in this tutorial. Such as:
- Trying to read or write a non existent field in your config object
- Assigning a value that is incompatible with the declared type
- Attempting to modify a frozen config
In the example above
cfg is duck typed as
It is actually an instance of
DictConfig. The duck typing enables static type checking by tools like Mypy or PyCharm.
This reduces development time by catching coding errors before you run your application.
The name Duck typing comes from the phrase "If it walks like a duck, swims like a duck, and quacks like a duck, then it probably is a duck". It can be useful when you care about the methods or attributes of an object, not the actual type of the object.