Skip to content

Commit

Permalink
Merge 3e7dc2d into 3dc5fc6
Browse files Browse the repository at this point in the history
  • Loading branch information
akaIDIOT committed Nov 23, 2018
2 parents 3dc5fc6 + 3e7dc2d commit 9f3020b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
10 changes: 10 additions & 0 deletions confidence.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from itertools import chain, product
from os import environ, path
import re
import warnings

import yaml

Expand Down Expand Up @@ -81,6 +82,12 @@ def _split_keys(mapping, separator='.'):
# use rest as the new key of value, recursively split that and update value
value = _split_keys({rest: value}, separator)

if key in _COLLIDING_KEYS:
# warn about configured keys colliding with Configuration members
warnings.warn('key {key} collides with member of Configuration type, use get() method to retrieve key '
'{key}'.format(key=key),
UserWarning)

# merge the result so far with the (possibly updated / fixed / split) current key and value
_merge(result, {key: value})

Expand Down Expand Up @@ -200,6 +207,9 @@ def __dir__(self):
return sorted(set(chain(super().__dir__(), self.keys())))


_COLLIDING_KEYS = frozenset(dir(Configuration()))


class NotConfigured(Configuration):
"""
Sentinel value to signal there is no value for a requested key.
Expand Down
17 changes: 16 additions & 1 deletion tests/test_namespace.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from collections import Mapping
from collections.abc import Mapping
from unittest.mock import patch

import pytest

Expand Down Expand Up @@ -46,6 +47,20 @@ def test_not_configured():
assert str(subject.does_nope_exist) == repr(subject.does.nope.exist)


def test_collisions():
with patch('confidence.warnings') as warnings:
subject = Configuration({'key': 'value', 'keys': [1, 2], '_separator': '_'})

for collision in ('keys', '_separator'):
warnings.warn.assert_any_call('key {key} collides with member of Configuration type, use get() method to '
'retrieve key {key}'.format(key=collision),
UserWarning)

assert subject.key == 'value'
assert callable(subject.keys)
assert subject._separator == '.'


def test_dir():
subject = Configuration({'key1': 'value', 'key2': 5, 'namespace.key3': False})

Expand Down

0 comments on commit 9f3020b

Please sign in to comment.