Skip to content

Commit

Permalink
updated tutorial a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
omry committed Feb 26, 2020
1 parent ea98c79 commit cd2af75
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 22 deletions.
4 changes: 2 additions & 2 deletions website/docs/tutorials/intro.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ title: Tutorials intro
---

### Basic Tutorial
The [Basic Tutorial](/tutorials/basic/1_simple_cli_app.md) covers basic Hydra concepts. New users should familiarize themselves with this tutorial.
The [Basic Tutorial](/tutorials/basic/1_simple_cli_app.md) covers basic Hydra concepts.

### Structured configs
The [Structured Configs Tutorial](/tutorials/structured_config/1_basic.md) Shows how to create strongly typed configurations that can still be composed and overridden from the command line and configuration files.
The [Structured Configs Tutorial](/tutorials/structured_config/1_basic.md) Shows how to create strongly typed configurations.

51 changes: 31 additions & 20 deletions website/docs/tutorials/structured_config/1_basic.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,43 @@ sidebar_label: Introduction to Structured Configs
---
This is an advanced tutorial that assumes that you are comfortable with the concepts introduced in the [Basic Tutorial](/tutorials/basic/1_simple_cli_app.md).

Structured Configs are new in **Hydra 1.0.0**. The key idea is that a regular Python dataclass or instance can be used to define the configuration.
The DictConfig object is very similar to those defined through config files, with one important difference:
Structured Configs are strongly typed.
Structured configs are classes that are used to define your config, enabling two primary features:

This enables two new features:

* **Static type checking**: The resulting object looks much like an instance of the underlying type. You can annotate the config object as the type and use Mypy or other static
* **Static type checking**: The config object looks much like an instance of the underlying type, you can annotate it as that type and use Mypy or other static
type checkers to perform static type analysis on your config objects.
* **Runtime type checking**: The type information in retained at runtime and is used to validate that changes to your object are conforming to underlying type specification.
This is especially useful for catching type errors during the composition of your configuration.

Structured Configs support primitive types, Enums, nesting of other Structured Configs and typed containers like `List` or `Dict`.
This tutorial does not assume complete knowledge of Structured Configs. Visit the <a class="external" href="https://omegaconf.readthedocs.io/en/latest/structured_config.html" target="_blank">documentation</a> later to learn more.
Structured Configs are new in **Hydra 1.0.0**. The key idea is behind them is that can use regular Python dataclasses to describe your configuration structure and types.
The configuration objects created from Structured Configs uses the type information for validating the composition and other mutations of the config.

A partial list of features of Structured Configs:
- Primitive types (int, bool, float, str, Enums)
- Nesting of structured configs
- Containers (List and Dict) with primitives or Structured Configs.
- Optional fields

This tutorial does not assume complete knowledge of Structured Configs. Visit the <a class="external" href="https://omegaconf.readthedocs.io/en/latest/structured_config.html" target="_blank">documentation</a> to learn more.

<div class="alert alert--info" role="alert">
1. The APIs and behaviors described in this tutorial are experimental and may change in a future version<br/>
2. Structured Configs adds a significant API surface area. Please report any issues
2. Structured configs are new, please report any issues<br/>
</div>
<br/>

This tutorial follows a path similar to that of the Basic Tutorial, but with Structured Configs instead of configuration files.
The [Structured config as schema](/tutorials/structured_config/6_schema.md) page shows how use Structured configs as a schema to validate configuration files.

There are two primary ways for using Structured configs.
- In place of configuration files
- As a [schema](/tutorials/structured_config/6_schema.md) validating configuration files

With both methods, you get everything else Hydra has to offer (Config composition, Command line overrides etc).
This tutorial covers both methods, read it in order.

#### Minimal example
Below is a minimal example that registers a structured config in the ConfigStore under the name "config",
and proceeds to use it as the primary config of the application.
Pay special attention to the fact that the type of the cfg object is declared as MySQLConfig.
This enables static type checking of the config object (Which is actually an instance of DictConfig).

```python
from dataclasses import dataclass
Expand Down Expand Up @@ -63,15 +74,8 @@ def my_app(cfg: MySQLConfig) -> None:
if __name__ == "__main__":
my_app()
```

You can also register instances of the dataclasses, allowing easy overriding of default values.
If you are registering 'config' more than once the last one will replace the previous ones.
```python
cfg_store.store(node=MySQLConfig(user="root", password="1234"), name="config")
```


Running this app you would see the expected output:
#### Expected output
The resulting application will offer the same features as any other Hydra application.
```text
$ python my_app.py
Connecting to mysql at localhost:3306, user=root, password=1234
Expand All @@ -86,3 +90,10 @@ Traceback (most recent call last):
...
omegaconf.errors.ValidationError: Error setting 'port = fail' : Value 'fail' could not be converted to Integer
```

#### Using objects in place of classes
You can use instances of the dataclasses when registrering the configs. Allowing easy overriding of default values.
If you are registering 'config' more than once the last one will replace the previous ones.
```python
cfg_store.store(node=MySQLConfig(user="root", password="1234"), name="config")
```

0 comments on commit cd2af75

Please sign in to comment.