Skip to content

Commit

Permalink
updated strict mode page
Browse files Browse the repository at this point in the history
  • Loading branch information
omry committed May 24, 2020
1 parent 2065472 commit c9b34c7
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 38 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved
from omegaconf import DictConfig, open_dict, MissingMandatoryValue
import pytest
from omegaconf import DictConfig, MissingMandatoryValue, open_dict

import hydra
import pytest


@hydra.main(config_name="config")
Expand Down
4 changes: 0 additions & 4 deletions hydra/_internal/core_plugins/bash_completion.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,6 @@

log = logging.getLogger(__name__)

# TODO:
# Test with /miniconda3/envs/hydra36/bin/python , seems to be running python for some reason.
# Test handling of errors loading config from command line during completion


class BashCompletion(CompletionPlugin):
def install(self) -> None:
Expand Down
6 changes: 6 additions & 0 deletions hydra/plugins/completion_plugin.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
# Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved

# TODO: Test with /miniconda3/envs/hydra36/bin/python , seems to be running python for some reason.
# TODO: Add tests for completion with +prefix (should suggest config groups that are not listed)
# TODO: Test completion when defaults has a missing mandatory item


import os
import re
import sys
Expand Down
4 changes: 0 additions & 4 deletions tests/test_config_loader.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
# Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved
# TODO: Document command line: +/~, package, defaults manipulation, the works.
# TODO: Proof read upgrades/0.11_to_1.0/strict_mode_flag_deprecated.md
# TODO: Tab completion: Add tests for completion with +prefix (should suggest config groups that are not listed)
# TODO: Tab completion: Test completion when defaults has a missing mandatory item
import re
from dataclasses import dataclass
from typing import Any, List
Expand Down
9 changes: 1 addition & 8 deletions tests/test_examples/test_tutorials_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
does_not_raise,
verify_dir_outputs,
)
from tests.test_examples import run_with_error

chdir_hydra_root()

Expand Down Expand Up @@ -402,10 +401,4 @@ def test_examples_using_the_config_object(tmpdir: Path) -> None:
"hydra.run.dir=" + str(tmpdir),
]

expected = """Missing mandatory value: node.waldo
\tfull_key: node.waldo
\treference_type=Optional[Dict[Any, Any]]
\tobject_type=dict"""

result = run_with_error(cmd)
assert re.search(re.escape(expected), result)
subprocess.check_output(cmd)
30 changes: 10 additions & 20 deletions website/docs/tutorials/basic/running_your_app/config_strict_mode.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,20 @@ id: strict_mode
title: Strict config errors
---


### Overview
`Strict mode` is enabled by default if you specify a config name in `hydra.main`.
It catches references to fields not specified in the config. This helps with two types of mistakes:
Hydra sets the `struct` flag on the config object. This catches references to fields not specified in the config.

This results in two useful behaviors:

Accessing a field that not in the config file throws an exception:
- Accessing a field that not in the config file raises exception:
```python {3}
@hydra.main(config_name='config')
def my_app(cfg : DictConfig) -> None:
print(cfg.db.drover) # typo: cfg.db.driver, raises an exception
```

Overriding a field that is not in the config file via the command line prints an error:
- Overriding a field that is not in the config file via the command line prints an error:
```text {1}
$ python my_app.py db.drover=mariadb
Error merging overrides
Expand All @@ -24,20 +26,8 @@ Key 'drover' in not in struct
object_type=dict
```

### Disabling strict mode
You can disable `Strict mode` by passing `strict=False` to `hydra.main()`
```python
@hydra.main(config_name='config', strict=False)
def my_app(cfg : DictConfig) -> None:
cfg.db.port = 3306 # Okay
```

This is not recommended. Without strict mode:
- Attempting to read a field not in the config object returns None.
- Attempting to override a field not in the config inserts the new field into the config.

If you have a good reason to add a new field to the config object at runtime,
use <a class="external" href="https://omegaconf.readthedocs.io/en/latest/usage.html#struct-flag" target="_blank">OmegaConf's open_dict</a>
to disable this protection within a context.
Note that `strict mode` is referred to as `struct mode` in OmegaConf.
### Adding fields at runtime
- You can disable the struct flag a specific context using `open_dict`.
- You can disable the struct flag permanently for your config using `OmegaConf.set_struct(cfg, False)`.

Learn more about OmegaConf struct flag <a class="external" href="https://omegaconf.readthedocs.io/en/latest/usage.html#struct-flag" target="_blank">here</a>.

0 comments on commit c9b34c7

Please sign in to comment.