From 8dcda649cfde62f9247e108561f045445fb62f06 Mon Sep 17 00:00:00 2001 From: Johannes Liebermann Date: Fri, 18 Oct 2019 22:46:29 +0200 Subject: [PATCH 1/5] Don't return False in __exit__ Returning a literal causes a mypy error when combined with the `typing.Optional[bool]` type hint. Furthermore, exception handling is the same when returning `False` and when returning `None` (the exception is re-raised). Therefore, it's simpler to remove the return statement and change the type hint to `None`. --- opentelemetry-api/src/opentelemetry/trace/__init__.py | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/opentelemetry-api/src/opentelemetry/trace/__init__.py b/opentelemetry-api/src/opentelemetry/trace/__init__.py index fac9f55da74..fff5d556b92 100644 --- a/opentelemetry-api/src/opentelemetry/trace/__init__.py +++ b/opentelemetry-api/src/opentelemetry/trace/__init__.py @@ -245,13 +245,9 @@ def __exit__( exc_type: typing.Optional[typing.Type[BaseException]], exc_val: typing.Optional[BaseException], exc_tb: typing.Optional[python_types.TracebackType], - ) -> typing.Optional[bool]: - """Ends context manager and calls `end` on the `Span`. - - Returns False. - """ + ) -> None: + """Ends context manager and calls `end` on the `Span`.""" self.end() - return False class TraceOptions(int): From d4d1333ebc8c0155ff9c0af003e26734eb390988 Mon Sep 17 00:00:00 2001 From: Johannes Liebermann Date: Mon, 21 Oct 2019 13:07:10 +0200 Subject: [PATCH 2/5] Correctly initialize nested tuple Tuples of length 1 should be initialized with a trailing comma to be properly interpreted. --- opentelemetry-api/tests/metrics/test_metrics.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/opentelemetry-api/tests/metrics/test_metrics.py b/opentelemetry-api/tests/metrics/test_metrics.py index 14667f62eaa..97ac92fcdef 100644 --- a/opentelemetry-api/tests/metrics/test_metrics.py +++ b/opentelemetry-api/tests/metrics/test_metrics.py @@ -24,7 +24,7 @@ def setUp(self): def test_record_batch(self): counter = metrics.Counter() - self.meter.record_batch(("values"), ((counter, 1))) + self.meter.record_batch(("values"), ((counter, 1),)) def test_create_metric(self): metric = self.meter.create_metric("", "", "", float, metrics.Counter) From 88f428c4b73823ddc1a83ccd5af1a3c95e8217f8 Mon Sep 17 00:00:00 2001 From: Johannes Liebermann Date: Mon, 21 Oct 2019 13:24:27 +0200 Subject: [PATCH 3/5] Pass correct type to use_context() in test --- .../distributedcontext/test_distributed_context.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/opentelemetry-api/tests/distributedcontext/test_distributed_context.py b/opentelemetry-api/tests/distributedcontext/test_distributed_context.py index 67a60048399..c730603b162 100644 --- a/opentelemetry-api/tests/distributedcontext/test_distributed_context.py +++ b/opentelemetry-api/tests/distributedcontext/test_distributed_context.py @@ -99,6 +99,14 @@ def test_get_current_context(self): self.assertIsNone(self.manager.get_current_context()) def test_use_context(self): - expected = object() + expected = distributedcontext.DistributedContext( + ( + distributedcontext.Entry( + distributedcontext.EntryMetadata(0), + distributedcontext.EntryKey("0"), + distributedcontext.EntryValue(""), + ), + ) + ) with self.manager.use_context(expected) as output: self.assertIs(output, expected) From 8dd416099ef0728f800f0a0c823e65b7eb90a7aa Mon Sep 17 00:00:00 2001 From: Johannes Liebermann Date: Mon, 21 Oct 2019 13:25:04 +0200 Subject: [PATCH 4/5] Add type annotations for test helper functions Since we have `disallow_untyped_calls = True` in our mypy config for tests, we must add type annotations to any function that is called from a test. --- opentelemetry-api/tests/test_loader.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/opentelemetry-api/tests/test_loader.py b/opentelemetry-api/tests/test_loader.py index 942479ab7dc..970b6159630 100644 --- a/opentelemetry-api/tests/test_loader.py +++ b/opentelemetry-api/tests/test_loader.py @@ -16,6 +16,7 @@ import sys import unittest from importlib import reload +from typing import Any, Callable from opentelemetry import trace from opentelemetry.util import loader @@ -59,7 +60,7 @@ def test_preferred_impl(self): # NOTE: We use do_* + *_ methods because subtest wouldn't run setUp, # which we require here. - def do_test_preferred_impl(self, setter): + def do_test_preferred_impl(self, setter: Callable[[Any], Any]) -> None: setter(get_opentelemetry_implementation) tracer = trace.tracer() self.assertIs(tracer, DUMMY_TRACER) @@ -81,7 +82,7 @@ def test_try_set_again(self): ) self.assertIn("already loaded", str(einfo.exception)) - def do_test_get_envvar(self, envvar_suffix): + def do_test_get_envvar(self, envvar_suffix: str) -> None: global DUMMY_TRACER # pylint:disable=global-statement # Test is not runnable with this! From bbab1481c5a507294f44d1885866941ae52be779 Mon Sep 17 00:00:00 2001 From: Johannes Liebermann Date: Mon, 21 Oct 2019 13:41:22 +0200 Subject: [PATCH 5/5] Bump minimal mypy version to 0.740 --- tox.ini | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tox.ini b/tox.ini index 0db2364f197..19816d3d9c1 100644 --- a/tox.ini +++ b/tox.ini @@ -14,7 +14,7 @@ python = [testenv] deps = - mypy,mypyinstalled: mypy~=0.711 + mypy,mypyinstalled: mypy~=0.740 setenv = mypy: MYPYPATH={toxinidir}/opentelemetry-api/src/