Skip to content

Commit 3594e32

Browse files
johananlc24t
authored andcommitted
Fix mypy errors (#229)
In particular, the following errors are fixed in this commit: * 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`. * Correctly initialize nested tuple Tuples of length 1 should be initialized with a trailing comma to be properly interpreted. * Pass correct type to use_context() in test * 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. Addditionally, bump minimal mypy version to 0.740 to consistently reproduce these errors.
1 parent 235d74f commit 3594e32

File tree

5 files changed

+16
-11
lines changed

5 files changed

+16
-11
lines changed

opentelemetry-api/src/opentelemetry/trace/__init__.py

+2-6
Original file line numberDiff line numberDiff line change
@@ -245,13 +245,9 @@ def __exit__(
245245
exc_type: typing.Optional[typing.Type[BaseException]],
246246
exc_val: typing.Optional[BaseException],
247247
exc_tb: typing.Optional[python_types.TracebackType],
248-
) -> typing.Optional[bool]:
249-
"""Ends context manager and calls `end` on the `Span`.
250-
251-
Returns False.
252-
"""
248+
) -> None:
249+
"""Ends context manager and calls `end` on the `Span`."""
253250
self.end()
254-
return False
255251

256252

257253
class TraceOptions(int):

opentelemetry-api/tests/distributedcontext/test_distributed_context.py

+9-1
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,14 @@ def test_get_current_context(self):
9999
self.assertIsNone(self.manager.get_current_context())
100100

101101
def test_use_context(self):
102-
expected = object()
102+
expected = distributedcontext.DistributedContext(
103+
(
104+
distributedcontext.Entry(
105+
distributedcontext.EntryMetadata(0),
106+
distributedcontext.EntryKey("0"),
107+
distributedcontext.EntryValue(""),
108+
),
109+
)
110+
)
103111
with self.manager.use_context(expected) as output:
104112
self.assertIs(output, expected)

opentelemetry-api/tests/metrics/test_metrics.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def setUp(self):
2424

2525
def test_record_batch(self):
2626
counter = metrics.Counter()
27-
self.meter.record_batch(("values"), ((counter, 1)))
27+
self.meter.record_batch(("values"), ((counter, 1),))
2828

2929
def test_create_metric(self):
3030
metric = self.meter.create_metric("", "", "", float, metrics.Counter)

opentelemetry-api/tests/test_loader.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import sys
1717
import unittest
1818
from importlib import reload
19+
from typing import Any, Callable
1920

2021
from opentelemetry import trace
2122
from opentelemetry.util import loader
@@ -59,7 +60,7 @@ def test_preferred_impl(self):
5960

6061
# NOTE: We use do_* + *_<arg> methods because subtest wouldn't run setUp,
6162
# which we require here.
62-
def do_test_preferred_impl(self, setter):
63+
def do_test_preferred_impl(self, setter: Callable[[Any], Any]) -> None:
6364
setter(get_opentelemetry_implementation)
6465
tracer = trace.tracer()
6566
self.assertIs(tracer, DUMMY_TRACER)
@@ -81,7 +82,7 @@ def test_try_set_again(self):
8182
)
8283
self.assertIn("already loaded", str(einfo.exception))
8384

84-
def do_test_get_envvar(self, envvar_suffix):
85+
def do_test_get_envvar(self, envvar_suffix: str) -> None:
8586
global DUMMY_TRACER # pylint:disable=global-statement
8687

8788
# Test is not runnable with this!

tox.ini

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ python =
1414

1515
[testenv]
1616
deps =
17-
mypy,mypyinstalled: mypy~=0.711
17+
mypy,mypyinstalled: mypy~=0.740
1818

1919
setenv =
2020
mypy: MYPYPATH={toxinidir}/opentelemetry-api/src/

0 commit comments

Comments
 (0)