Skip to content

Commit 5a6c0ce

Browse files
committed
Return None when loading a null !path
1 parent 4536ed1 commit 5a6c0ce

File tree

5 files changed

+23
-5
lines changed

5 files changed

+23
-5
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22
## Unreleased
3+
## 4.0.1
4+
### Fixes
5+
- Loading a null `!path` will now return `None` instead of raising an Exception
6+
37
## 4.0.0
48
### Breaking changes
59
- The `!env` tag has been removed, environment variables can now be loaded anywhere

CONTRIBUTING.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ Run `python -m unitttest discover` to run the tests.
2121

2222
Run these commands to check the files linting:
2323
```shell script
24-
pylint --load-plugins pylint_quotes configue
25-
pylint --load-plugins pylint_quotes tests --disable=protected-access,too-many-instance-attributes,no-self-use
24+
black . --check -l 120
25+
pylint configue
26+
pylint tests --disable=too-many-instance-attributes,no-self-use,similarities
2627
```

configue/file_loader.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import logging
22
import os
3-
from typing import Any, List, TYPE_CHECKING, Type, Union, cast
3+
from typing import Any, List, Optional, TYPE_CHECKING, Type, Union, cast
44

55
from yaml import Loader, MappingNode, Node, ScalarNode, SequenceNode
66

@@ -71,8 +71,11 @@ def _load_import(self, loader: ConfigueLoader, tag_suffix: str, node: ScalarNode
7171
path = self._load_path(loader, node)
7272
return self._root_loader.load_file(path, tag_suffix[1:])
7373

74-
def _load_path(self, loader: ConfigueLoader, node: ScalarNode) -> str:
75-
path = os.path.expanduser(loader.construct_scalar(node))
74+
def _load_path(self, loader: ConfigueLoader, node: ScalarNode) -> Optional[str]:
75+
raw_path = loader.construct_scalar(node)
76+
if raw_path is None:
77+
return None
78+
path = os.path.expanduser(raw_path)
7679
return os.path.join(os.path.dirname(self._file_path), path)
7780

7881
def _load_cfg(self, loader: ConfigueLoader, node: ScalarNode) -> Any:

tests/test_configue.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,12 @@ def test_load_external_value(self):
116116
result = configue.load(self._get_path("test_file_2.yml"), "const")
117117
self.assertEqual(CONSTANT, result)
118118

119+
def test_null_path(self):
120+
result = configue.load(self._get_path("test_file_2.yml"), "paths")
121+
self.assertIsNone(result["path"])
122+
self.assertEqual(os.path.expanduser("~"), result["path2"])
123+
self.assertIsNone(result["path3"])
124+
119125
@staticmethod
120126
def _get_path(file_name: str) -> str:
121127
return os.path.join(os.path.dirname(__file__), file_name)

tests/test_file_2.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,7 @@ invalid_class:
88
(): tests.external_module.CONSTANT
99

1010
const: !ext tests.external_module.CONSTANT
11+
paths:
12+
path: !path ${ENV_PATH-null}
13+
path2: !path "${ENV_PATH-~}"
14+
path3: !path ${ENV_PATH-~}

0 commit comments

Comments
 (0)