Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix and improve tests for Python != 3.7 #95

Merged
merged 1 commit into from
Aug 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,19 @@ dist: xenial
language: python

python:
- '3.4'
- '3.5'
- '3.6'
- '3.7'
- 'pypy3.5'
- '3.8-dev'
Copy link
Contributor

@carlosalberto carlosalberto Aug 20, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice. However, we really don't need to have 3.8-dev, and specially the nightly one, IHMO (even with the allow_failures flag) ;)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think there's any benefit IHMO. dev can change relatively a lot. I don't think there's any value in having it here honestly (unless we were married to some internal or prototype feature of it).

Anyway, I wouldn't totally be against this, so I will leave others give their opinion ;)

Copy link
Contributor Author

@Jamim Jamim Aug 20, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Python 3.8 is almost there, so it makes sense to have tests for it.

In addition, I believe that it's always better to know about any incompatibilities sooner rather than later.
Especially if running the tests costs nothing.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It does make builds slower. But I have no strong opinion on dev either. I also believe that nightly is too much, things can still change there so if we fix an error in nightly and then nightly changes to make the fix unnecessary, we wasted time.

Copy link
Contributor Author

@Jamim Jamim Aug 21, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @carlosalberto and @Oberon00,
I've removed nightly for now.


matrix:
allow_failures:
- python: '3.8-dev'

install:
- pip install tox-travis

script:
- tox

branches:
only:
- master
6 changes: 4 additions & 2 deletions ext/opentelemetry-ext-wsgi/tests/test_wsgi_middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ def tearDown(self):

def start_response(self, status, response_headers, exc_info=None):
# The span should have started already
self.span_context_manager.__enter__.assert_called()
self.span_context_manager.__enter__.assert_called_with()

self.status = status
self.response_headers = response_headers
Expand All @@ -108,7 +108,9 @@ def validate_response(self, response, error=None):
self.span_context_manager.__exit__.assert_not_called()
self.assertEqual(value, b"*")
except StopIteration:
self.span_context_manager.__exit__.assert_called()
self.span_context_manager.__exit__.assert_called_with(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Too bad we need this change. 🙁 Which Python version requires it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need it for 3.5 and 3.4.
The assert_called method was introduced in 3.6.

None, None, None
)
break

self.assertEqual(self.status, "200 OK")
Expand Down
4 changes: 3 additions & 1 deletion opentelemetry-api/src/opentelemetry/context/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,9 @@ async def main():
__all__ = ['Context']


Context: typing.Optional[BaseRuntimeContext]
Context = ( # pylint: disable=invalid-name
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This makes me wonder if we shouldn't relax the pylint rule for "constants" (maybe even to .*) since there are so many things it demands UPPER_CASE for which are not really constants. Then this could be a single line without the awkward parens.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's done. Please review b05f347.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Related PR: #108

None
) # type: typing.Optional[BaseRuntimeContext]

try:
from .async_context import AsyncRuntimeContext
Expand Down
9 changes: 5 additions & 4 deletions opentelemetry-api/src/opentelemetry/context/async_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# limitations under the License.

from contextvars import ContextVar
import typing
import typing # pylint: disable=unused-import

from . import base_context

Expand All @@ -23,9 +23,10 @@ class Slot(base_context.BaseRuntimeContext.Slot):
def __init__(self, name: str, default: 'object'):
# pylint: disable=super-init-not-called
self.name = name
self.contextvar: 'ContextVar[object]' = ContextVar(name)
self.default: typing.Callable[..., object]
self.default = base_context.wrap_callable(default)
self.contextvar = ContextVar(name) # type: ContextVar[object]
self.default = base_context.wrap_callable(
default
) # type: typing.Callable[..., object]

def clear(self) -> None:
self.contextvar.set(self.default())
Expand Down
4 changes: 2 additions & 2 deletions opentelemetry-api/src/opentelemetry/context/base_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def set(self, value: 'object') -> None:
raise NotImplementedError

_lock = threading.Lock()
_slots: typing.Dict[str, 'BaseRuntimeContext.Slot'] = {}
_slots = {} # type: typing.Dict[str, 'BaseRuntimeContext.Slot']

@classmethod
def clear(cls) -> None:
Expand Down Expand Up @@ -112,7 +112,7 @@ def with_current_context(

def call_with_current_context(
*args: 'object',
**kwargs: 'object',
**kwargs: 'object'
) -> 'object':
try:
backup_context = self.snapshot()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# limitations under the License.

import threading
import typing
import typing # pylint: disable=unused-import

from . import base_context

Expand All @@ -25,15 +25,16 @@ class Slot(base_context.BaseRuntimeContext.Slot):
def __init__(self, name: str, default: 'object'):
# pylint: disable=super-init-not-called
self.name = name
self.default: typing.Callable[..., object]
self.default = base_context.wrap_callable(default)
self.default = base_context.wrap_callable(
default
) # type: typing.Callable[..., object]

def clear(self) -> None:
setattr(self._thread_local, self.name, self.default())

def get(self) -> 'object':
try:
got: object = getattr(self._thread_local, self.name)
got = getattr(self._thread_local, self.name) # type: object
return got
except AttributeError:
value = self.default()
Expand Down
2 changes: 1 addition & 1 deletion opentelemetry-api/src/opentelemetry/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def my_factory_for_t(api_type: typing.Type[T]) -> typing.Optional[T]:
# code.
# ImplementationFactory = Callable[[Type[_T]], Optional[_T]]

_DEFAULT_FACTORY: Optional[_UntrustedImplFactory[object]] = None
_DEFAULT_FACTORY = None # type: Optional[_UntrustedImplFactory[object]]


def _try_load_impl_from_modname(
Expand Down
17 changes: 11 additions & 6 deletions opentelemetry-api/src/opentelemetry/trace/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -378,9 +378,15 @@ def use_span(self, span: 'Span') -> typing.Iterator[None]:
yield


_TRACER: typing.Optional[Tracer] = None
_TRACER_FACTORY: typing.Optional[
typing.Callable[[typing.Type[Tracer]], typing.Optional[Tracer]]] = None
# Once https://github.com/python/mypy/issues/7092 is resolved,
# the following type definition should be replaced with
# from opentelemetry.loader import ImplementationFactory
ImplementationFactory = typing.Callable[
[typing.Type[Tracer]], typing.Optional[Tracer]
]

_TRACER = None # type: typing.Optional[Tracer]
_TRACER_FACTORY = None # type: typing.Optional[ImplementationFactory]


def tracer() -> Tracer:
Expand All @@ -399,9 +405,8 @@ def tracer() -> Tracer:


def set_preferred_tracer_implementation(
factory: typing.Callable[
[typing.Type[Tracer]], typing.Optional[Tracer]]
) -> None:
factory: ImplementationFactory
) -> None:
"""Set the factory to be used to create the tracer.

See :mod:`opentelemetry.loader` for details.
Expand Down
7 changes: 5 additions & 2 deletions tox.ini
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
[tox]
skipsdist = True
skip_missing_interpreters = True
envlist =
py{34,35,36,37}-test-{api,sdk}
py{34,35,36,37}-test-ext-wsgi
py3{4,5,6,7,8}-test-{api,sdk,ext-wsgi}
pypy35-test-{api,sdk,ext-wsgi}
lint
py37-mypy
docs
Expand All @@ -24,6 +25,7 @@ changedir =
test-ext-wsgi: ext/opentelemetry-ext-wsgi/tests

commands_pre =
pip install -U pip setuptools wheel
test: pip install -e {toxinidir}/opentelemetry-api
test-sdk: pip install -e {toxinidir}/opentelemetry-sdk
ext: pip install -e {toxinidir}/opentelemetry-api
Expand All @@ -36,6 +38,7 @@ commands =
test: python -m unittest discover

[testenv:lint]
basepython: python3.7
deps =
pylint~=2.3
flake8~=3.7
Expand Down