From 7a46b2df1eb507d615c3e2c9baada1794fd04cb8 Mon Sep 17 00:00:00 2001 From: Diego Hurtado Date: Wed, 1 Jul 2020 11:03:19 -0600 Subject: [PATCH] Make it possible to set new values in Configuration (#863) --- .../src/opentelemetry/configuration/__init__.py | 11 +++++------ .../tests/configuration/test_configuration.py | 11 +++++++---- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/opentelemetry-api/src/opentelemetry/configuration/__init__.py b/opentelemetry-api/src/opentelemetry/configuration/__init__.py index 0bd2bce8d6..36c0950c0b 100644 --- a/opentelemetry-api/src/opentelemetry/configuration/__init__.py +++ b/opentelemetry-api/src/opentelemetry/configuration/__init__.py @@ -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: diff --git a/opentelemetry-api/tests/configuration/test_configuration.py b/opentelemetry-api/tests/configuration/test_configuration.py index 32b62e619d..316847a004 100644 --- a/opentelemetry-api/tests/configuration/test_configuration.py +++ b/opentelemetry-api/tests/configuration/test_configuration.py @@ -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 @@ -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