Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Commit

Permalink
Use nullcontext; remove noop_context_manager
Browse files Browse the repository at this point in the history
The comment says contextlib.nullcontext requires Python 3.6. The docs
https://docs.python.org/3/library/contextlib.html say 3.7, with
asynchronous context manager support in 3.10. But AFAICS we only use
`nullcontext` in place of a `Scope`, and that only acts as a synchronous
context manager.
  • Loading branch information
David Robertson committed May 27, 2022
1 parent b8da918 commit e6d193a
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 17 deletions.
15 changes: 4 additions & 11 deletions synapse/logging/opentracing.py
Original file line number Diff line number Diff line change
Expand Up @@ -371,13 +371,6 @@ def ensure_active_span_inner_2(*args, **kwargs):
return ensure_active_span_inner_1


@contextlib.contextmanager
def noop_context_manager(*args, **kwargs):
"""Does exactly what it says on the tin"""
# TODO: replace with contextlib.nullcontext once we drop support for Python 3.6
yield


# Setup


Expand Down Expand Up @@ -472,11 +465,11 @@ def start_active_span(
Args:
See opentracing.tracer
Returns:
scope (Scope) or noop_context_manager
scope (Scope) or contextlib.nullcontext
"""

if opentracing is None:
return noop_context_manager() # type: ignore[unreachable]
return contextlib.nullcontext() # type: ignore[unreachable]

if tracer is None:
# use the global tracer by default
Expand Down Expand Up @@ -520,7 +513,7 @@ def start_active_span_follows_from(
tracer: override the opentracing tracer. By default the global tracer is used.
"""
if opentracing is None:
return noop_context_manager() # type: ignore[unreachable]
return contextlib.nullcontext() # type: ignore[unreachable]

references = [opentracing.follows_from(context) for context in contexts]
scope = start_active_span(
Expand Down Expand Up @@ -560,7 +553,7 @@ def start_active_span_from_edu(
references = references or []

if opentracing is None:
return noop_context_manager() # type: ignore[unreachable]
return contextlib.nullcontext() # type: ignore[unreachable]

carrier = json_decoder.decode(edu_content.get("context", "{}")).get(
"opentracing", {}
Expand Down
9 changes: 3 additions & 6 deletions synapse/metrics/background_process_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import logging
import threading
from contextlib import nullcontext
from functools import wraps
from types import TracebackType
from typing import (
Expand Down Expand Up @@ -41,11 +42,7 @@
LoggingContext,
PreserveLoggingContext,
)
from synapse.logging.opentracing import (
SynapseTags,
noop_context_manager,
start_active_span,
)
from synapse.logging.opentracing import SynapseTags, start_active_span
from synapse.metrics._types import Collector

if TYPE_CHECKING:
Expand Down Expand Up @@ -238,7 +235,7 @@ async def run() -> Optional[R]:
f"bgproc.{desc}", tags={SynapseTags.REQUEST_ID: str(context)}
)
else:
ctx = noop_context_manager()
ctx = nullcontext()
with ctx:
return await func(*args, **kwargs)
except Exception:
Expand Down

0 comments on commit e6d193a

Please sign in to comment.