Skip to content

Commit

Permalink
Make it possible to set new values in Configuration (#863)
Browse files Browse the repository at this point in the history
  • Loading branch information
ocelotl authored Jul 1, 2020
1 parent 170c2c7 commit 7a46b2d
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
11 changes: 5 additions & 6 deletions opentelemetry-api/src/opentelemetry/configuration/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,19 +142,18 @@ def __new__(cls) -> "Configuration":
def __getattr__(self, name: str) -> Optional[ConfigValue]:
return self._config_map.get(name)

def __setattr__(self, key: str, val: ConfigValue) -> None:
if key == "_config_map":
super().__setattr__(key, val)
def __setattr__(self, name: str, value: ConfigValue) -> None:
if name not in self._config_map.keys():
self._config_map[name] = value
else:
raise AttributeError(key)
raise AttributeError(name)

def get(self, name: str, default: _T) -> _T:
"""Use this typed method for dynamic access instead of `getattr`
:rtype: str or bool or int or float or None
"""
val = self._config_map.get(name, default)
return val
return self._config_map.get(name, default)

@classmethod
def _reset(cls) -> None:
Expand Down
11 changes: 7 additions & 4 deletions opentelemetry-api/tests/configuration/test_configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def test_singleton(self) -> None:
"OPENTELEMETRY_PTHON_TRACEX_PROVIDER": "tracex_provider",
},
)
def test_environment_variables(self):
def test_environment_variables(self) -> None:
self.assertEqual(
Configuration().METER_PROVIDER, "meter_provider"
) # pylint: disable=no-member
Expand All @@ -58,13 +58,16 @@ def test_environment_variables(self):
"os.environ", # type: ignore
{"OPENTELEMETRY_PYTHON_TRACER_PROVIDER": "tracer_provider"},
)
def test_property(self):
def test_property(self) -> None:
with self.assertRaises(AttributeError):
Configuration().TRACER_PROVIDER = "new_tracer_provider"

def test_slots(self) -> None:
def test_set_once(self) -> None:

Configuration().XYZ = "xyz"

with self.assertRaises(AttributeError):
Configuration().XYZ = "xyz" # pylint: disable=assigning-non-slot
Configuration().XYZ = "abc" # pylint: disable=assigning-non-slot

def test_getattr(self) -> None:
# literal access
Expand Down

0 comments on commit 7a46b2d

Please sign in to comment.