From da22b91ed4a421477c432ad613f95db21f10964c Mon Sep 17 00:00:00 2001 From: Aaron Abbott Date: Tue, 2 Jun 2020 19:08:09 +0000 Subject: [PATCH] update test, use generic --- .../opentelemetry/configuration/__init__.py | 3 ++- .../tests/configuration/test_configuration.py | 21 ++++++++++++------- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/opentelemetry-api/src/opentelemetry/configuration/__init__.py b/opentelemetry-api/src/opentelemetry/configuration/__init__.py index 2790cb61458..bd54b312bde 100644 --- a/opentelemetry-api/src/opentelemetry/configuration/__init__.py +++ b/opentelemetry-api/src/opentelemetry/configuration/__init__.py @@ -95,6 +95,7 @@ from typing import Dict, Optional, Type, TypeVar, Union ConfigValue = Union[str, bool, int, float] +_T = TypeVar("_T", ConfigValue, Optional[ConfigValue]) class Configuration: @@ -148,7 +149,7 @@ def __setattr__(self, key: str, val: ConfigValue) -> None: else: raise AttributeError(key) - def get(self, name: str, default: ConfigValue) -> ConfigValue: + def get(self, name: str, default: _T) -> _T: val = self._config_map.get(name, default) return val diff --git a/opentelemetry-api/tests/configuration/test_configuration.py b/opentelemetry-api/tests/configuration/test_configuration.py index f9054c288a4..32b62e619d5 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): # type: ignore + def test_environment_variables(self): self.assertEqual( Configuration().METER_PROVIDER, "meter_provider" ) # pylint: disable=no-member @@ -62,16 +62,21 @@ def test_property(self): with self.assertRaises(AttributeError): Configuration().TRACER_PROVIDER = "new_tracer_provider" - def test_slots(self): + def test_slots(self) -> None: with self.assertRaises(AttributeError): Configuration().XYZ = "xyz" # pylint: disable=assigning-non-slot - def test_getattr(self): + def test_getattr(self) -> None: + # literal access self.assertIsNone(Configuration().XYZ) - def test_reset(self): + # dynamic access + self.assertIsNone(getattr(Configuration(), "XYZ")) + self.assertIsNone(Configuration().get("XYZ", None)) + + def test_reset(self) -> None: environ_patcher = patch.dict( - "os.environ", # type: ignore + "os.environ", {"OPENTELEMETRY_PYTHON_TRACER_PROVIDER": "tracer_provider"}, ) @@ -96,7 +101,7 @@ def test_reset(self): "OPENTELEMETRY_PYTHON_FALSE": "False", }, ) - def test_boolean(self): + def test_boolean(self) -> None: self.assertIsInstance( Configuration().TRUE, bool ) # pylint: disable=no-member @@ -114,7 +119,7 @@ def test_boolean(self): "OPENTELEMETRY_PYTHON_NON_INTEGER": "-12z3", }, ) - def test_integer(self): + def test_integer(self) -> None: self.assertEqual( Configuration().POSITIVE_INTEGER, 123 ) # pylint: disable=no-member @@ -133,7 +138,7 @@ def test_integer(self): "OPENTELEMETRY_PYTHON_NON_FLOAT": "-12z3.123", }, ) - def test_float(self): + def test_float(self) -> None: self.assertEqual( Configuration().POSITIVE_FLOAT, 123.123 ) # pylint: disable=no-member