Skip to content

Commit

Permalink
Refactor code using pyupgrade for Python 3.6 (open-telemetry#770)
Browse files Browse the repository at this point in the history
* Refactor code using pyupgrade for Python 3.6

This diff is the result of applying the following command to the
project:

```shell
find . -type f -name "*.py" -exec pyupgrade --py36-plus '{}' +
```

* Simplify yield from
  • Loading branch information
adamantike authored Oct 24, 2021
1 parent 3ff06da commit 07c52aa
Show file tree
Hide file tree
Showing 14 changed files with 22 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,7 @@ def setUp(self):
def test_request_attributes(self):
self.scope["query_string"] = b"foo=bar"
headers = []
headers.append(("host".encode("utf8"), "test".encode("utf8")))
headers.append((b"host", b"test"))
self.scope["headers"] = headers

attrs = otel_asgi.collect_request_attributes(self.scope)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -235,11 +235,11 @@ def truncate_arg_value(value, max_len=1024):
# Do not trace `Key Management Service` or `Secure Token Service` API calls
# over concerns of security leaks.
if aws_service not in {"kms", "sts"}:
tags = dict(
(name, value)
tags = {
name: value
for (name, value) in zip(args_names, args)
if name in args_traced
)
}
tags = flatten_dict(tags)

for param_key, value in tags.items():
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ def assert_span(
expected[span_attributes_request_id] = request_id

self.assertSpanHasAttributes(span, expected)
self.assertEqual("{}.{}".format(service, operation), span.name)
self.assertEqual(f"{service}.{operation}", span.name)
return span

@mock_ec2
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ async def test_trace_response_headers(self):
)
self.assertEqual(
response.headers["traceresponse"],
"00-{0}-{1}-01".format(
"00-{}-{}-01".format(
format_trace_id(span.get_span_context().trace_id),
format_span_id(span.get_span_context().span_id),
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,10 +174,7 @@ def _intercept_server_stream(
rpc_info.request = request_or_iterator

try:
result = invoker(request_or_iterator, metadata)

for response in result:
yield response
yield from invoker(request_or_iterator, metadata)
except grpc.RpcError as err:
span.set_status(Status(StatusCode.ERROR))
span.set_attribute(
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from tests.protobuf import test_server_pb2 as test__server__pb2


class GRPCTestServerStub(object):
class GRPCTestServerStub:
"""Missing associated documentation comment in .proto file"""

def __init__(self, channel):
Expand Down Expand Up @@ -34,7 +34,7 @@ def __init__(self, channel):
)


class GRPCTestServerServicer(object):
class GRPCTestServerServicer:
"""Missing associated documentation comment in .proto file"""

def SimpleMethod(self, request, context):
Expand Down Expand Up @@ -92,7 +92,7 @@ def add_GRPCTestServerServicer_to_server(servicer, server):


# This class is part of an EXPERIMENTAL API.
class GRPCTestServer(object):
class GRPCTestServer:
"""Missing associated documentation comment in .proto file"""

@staticmethod
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -316,8 +316,7 @@ def __call__(self, environ, start_response):
def _end_span_after_iterating(iterable, span, tracer, token):
try:
with trace.use_span(span):
for yielded in iterable:
yield yielded
yield from iterable
finally:
close = getattr(iterable, "close", None)
if close:
Expand Down
2 changes: 1 addition & 1 deletion scripts/generate_instrumentation_bootstrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def main():
source = astor.to_source(tree)

with open(
os.path.join(scripts_path, "license_header.txt"), "r", encoding="utf-8"
os.path.join(scripts_path, "license_header.txt"), encoding="utf-8"
) as header_file:
header = header_file.read()
source = _template.format(header=header, source=source)
Expand Down
1 change: 0 additions & 1 deletion scripts/generate_setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ def main():
root_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
with open(
os.path.join(root_path, _template_dir, _template_name),
"r",
encoding="utf-8",
) as template:
setuppy_tmpl = Template(template.read())
Expand Down
6 changes: 3 additions & 3 deletions tests/opentelemetry-docker-tests/tests/celery/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@

REDIS_HOST = os.getenv("REDIS_HOST", "localhost")
REDIS_PORT = int(os.getenv("REDIS_PORT ", "6379"))
REDIS_URL = "redis://{host}:{port}".format(host=REDIS_HOST, port=REDIS_PORT)
BROKER_URL = "{redis}/{db}".format(redis=REDIS_URL, db=0)
BACKEND_URL = "{redis}/{db}".format(redis=REDIS_URL, db=1)
REDIS_URL = f"redis://{REDIS_HOST}:{REDIS_PORT}"
BROKER_URL = f"{REDIS_URL}/0"
BACKEND_URL = f"{REDIS_URL}/1"


@pytest.fixture(scope="session")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,16 @@ def fn_task():
assert run_span.parent.span_id == async_span.context.span_id
assert run_span.context.trace_id == async_span.context.trace_id

assert async_span.instrumentation_info.name == "apply_async/{0}".format(
assert async_span.instrumentation_info.name == "apply_async/{}".format(
opentelemetry.instrumentation.celery.__name__
)
assert async_span.instrumentation_info.version == "apply_async/{0}".format(
assert async_span.instrumentation_info.version == "apply_async/{}".format(
opentelemetry.instrumentation.celery.__version__
)
assert run_span.instrumentation_info.name == "run/{0}".format(
assert run_span.instrumentation_info.name == "run/{}".format(
opentelemetry.instrumentation.celery.__name__
)
assert run_span.instrumentation_info.version == "run/{0}".format(
assert run_span.instrumentation_info.version == "run/{}".format(
opentelemetry.instrumentation.celery.__version__
)

Expand Down Expand Up @@ -489,9 +489,7 @@ class CelerySuperClass(celery.task.Task):

@classmethod
def apply_async(cls, args=None, kwargs=None, **kwargs_):
return super(CelerySuperClass, cls).apply_async(
args=args, kwargs=kwargs, **kwargs_
)
return super().apply_async(args=args, kwargs=kwargs, **kwargs_)

def run(self, *args, **kwargs):
if "stop" in kwargs:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def wrapper():
ex,
)
time.sleep(RETRY_INTERVAL)
raise Exception("waiting for {} failed".format(func.__name__))
raise Exception(f"waiting for {func.__name__} failed")

return wrapper

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ def tearDown(self):

def _check_span(self, span, name):
if self.SQL_DB:
name = "{0} {1}".format(name, self.SQL_DB)
name = f"{name} {self.SQL_DB}"
self.assertEqual(span.name, name)
self.assertEqual(
span.attributes.get(SpanAttributes.DB_NAME), self.SQL_DB
Expand Down

0 comments on commit 07c52aa

Please sign in to comment.