From 07c52aad38d4abe4643d03f717e9fbb1e61f2f2f Mon Sep 17 00:00:00 2001 From: Michael Manganiello Date: Sat, 23 Oct 2021 22:34:06 -0300 Subject: [PATCH] Refactor code using pyupgrade for Python 3.6 (#770) * 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 --- .../tests/test_asgi_middleware.py | 2 +- .../opentelemetry/instrumentation/boto/__init__.py | 6 +++--- .../tests/test_botocore_instrumentation.py | 2 +- .../tests/test_middleware_asgi.py | 2 +- .../opentelemetry/instrumentation/grpc/_client.py | 5 +---- .../tests/protobuf/test_server_pb2.py | 1 - .../tests/protobuf/test_server_pb2_grpc.py | 6 +++--- .../opentelemetry/instrumentation/wsgi/__init__.py | 3 +-- scripts/generate_instrumentation_bootstrap.py | 2 +- scripts/generate_setup.py | 1 - .../tests/celery/conftest.py | 6 +++--- .../tests/celery/test_celery_functional.py | 12 +++++------- .../tests/check_availability.py | 2 +- .../tests/sqlalchemy_tests/mixins.py | 2 +- 14 files changed, 22 insertions(+), 30 deletions(-) diff --git a/instrumentation/opentelemetry-instrumentation-asgi/tests/test_asgi_middleware.py b/instrumentation/opentelemetry-instrumentation-asgi/tests/test_asgi_middleware.py index b19160b347..baeb6dd94e 100644 --- a/instrumentation/opentelemetry-instrumentation-asgi/tests/test_asgi_middleware.py +++ b/instrumentation/opentelemetry-instrumentation-asgi/tests/test_asgi_middleware.py @@ -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) diff --git a/instrumentation/opentelemetry-instrumentation-boto/src/opentelemetry/instrumentation/boto/__init__.py b/instrumentation/opentelemetry-instrumentation-boto/src/opentelemetry/instrumentation/boto/__init__.py index fcad7f635c..61302dbae8 100644 --- a/instrumentation/opentelemetry-instrumentation-boto/src/opentelemetry/instrumentation/boto/__init__.py +++ b/instrumentation/opentelemetry-instrumentation-boto/src/opentelemetry/instrumentation/boto/__init__.py @@ -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(): diff --git a/instrumentation/opentelemetry-instrumentation-botocore/tests/test_botocore_instrumentation.py b/instrumentation/opentelemetry-instrumentation-botocore/tests/test_botocore_instrumentation.py index 5ee04c610f..70bb703cb3 100644 --- a/instrumentation/opentelemetry-instrumentation-botocore/tests/test_botocore_instrumentation.py +++ b/instrumentation/opentelemetry-instrumentation-botocore/tests/test_botocore_instrumentation.py @@ -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 diff --git a/instrumentation/opentelemetry-instrumentation-django/tests/test_middleware_asgi.py b/instrumentation/opentelemetry-instrumentation-django/tests/test_middleware_asgi.py index 36a45c5fc1..d1f578ccc8 100644 --- a/instrumentation/opentelemetry-instrumentation-django/tests/test_middleware_asgi.py +++ b/instrumentation/opentelemetry-instrumentation-django/tests/test_middleware_asgi.py @@ -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), ), diff --git a/instrumentation/opentelemetry-instrumentation-grpc/src/opentelemetry/instrumentation/grpc/_client.py b/instrumentation/opentelemetry-instrumentation-grpc/src/opentelemetry/instrumentation/grpc/_client.py index 0f1ed88197..b73b822b14 100644 --- a/instrumentation/opentelemetry-instrumentation-grpc/src/opentelemetry/instrumentation/grpc/_client.py +++ b/instrumentation/opentelemetry-instrumentation-grpc/src/opentelemetry/instrumentation/grpc/_client.py @@ -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( diff --git a/instrumentation/opentelemetry-instrumentation-grpc/tests/protobuf/test_server_pb2.py b/instrumentation/opentelemetry-instrumentation-grpc/tests/protobuf/test_server_pb2.py index 735206f850..ad3dcf3fe7 100644 --- a/instrumentation/opentelemetry-instrumentation-grpc/tests/protobuf/test_server_pb2.py +++ b/instrumentation/opentelemetry-instrumentation-grpc/tests/protobuf/test_server_pb2.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- # Generated by the protocol buffer compiler. DO NOT EDIT! # source: test_server.proto diff --git a/instrumentation/opentelemetry-instrumentation-grpc/tests/protobuf/test_server_pb2_grpc.py b/instrumentation/opentelemetry-instrumentation-grpc/tests/protobuf/test_server_pb2_grpc.py index 9ff1ea27b5..003b68f4f1 100644 --- a/instrumentation/opentelemetry-instrumentation-grpc/tests/protobuf/test_server_pb2_grpc.py +++ b/instrumentation/opentelemetry-instrumentation-grpc/tests/protobuf/test_server_pb2_grpc.py @@ -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): @@ -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): @@ -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 diff --git a/instrumentation/opentelemetry-instrumentation-wsgi/src/opentelemetry/instrumentation/wsgi/__init__.py b/instrumentation/opentelemetry-instrumentation-wsgi/src/opentelemetry/instrumentation/wsgi/__init__.py index c8e804ff98..3c971e4482 100644 --- a/instrumentation/opentelemetry-instrumentation-wsgi/src/opentelemetry/instrumentation/wsgi/__init__.py +++ b/instrumentation/opentelemetry-instrumentation-wsgi/src/opentelemetry/instrumentation/wsgi/__init__.py @@ -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: diff --git a/scripts/generate_instrumentation_bootstrap.py b/scripts/generate_instrumentation_bootstrap.py index 6318795572..ef66420bce 100755 --- a/scripts/generate_instrumentation_bootstrap.py +++ b/scripts/generate_instrumentation_bootstrap.py @@ -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) diff --git a/scripts/generate_setup.py b/scripts/generate_setup.py index af134f7a20..452a5d77bf 100755 --- a/scripts/generate_setup.py +++ b/scripts/generate_setup.py @@ -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()) diff --git a/tests/opentelemetry-docker-tests/tests/celery/conftest.py b/tests/opentelemetry-docker-tests/tests/celery/conftest.py index 8dc36ca7bd..b73445eb87 100644 --- a/tests/opentelemetry-docker-tests/tests/celery/conftest.py +++ b/tests/opentelemetry-docker-tests/tests/celery/conftest.py @@ -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") diff --git a/tests/opentelemetry-docker-tests/tests/celery/test_celery_functional.py b/tests/opentelemetry-docker-tests/tests/celery/test_celery_functional.py index 0f707a4571..4cdf3718fe 100644 --- a/tests/opentelemetry-docker-tests/tests/celery/test_celery_functional.py +++ b/tests/opentelemetry-docker-tests/tests/celery/test_celery_functional.py @@ -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__ ) @@ -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: diff --git a/tests/opentelemetry-docker-tests/tests/check_availability.py b/tests/opentelemetry-docker-tests/tests/check_availability.py index 2ec8e43373..f9cd5b998e 100644 --- a/tests/opentelemetry-docker-tests/tests/check_availability.py +++ b/tests/opentelemetry-docker-tests/tests/check_availability.py @@ -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 diff --git a/tests/opentelemetry-docker-tests/tests/sqlalchemy_tests/mixins.py b/tests/opentelemetry-docker-tests/tests/sqlalchemy_tests/mixins.py index df976e97a8..084b8114a1 100644 --- a/tests/opentelemetry-docker-tests/tests/sqlalchemy_tests/mixins.py +++ b/tests/opentelemetry-docker-tests/tests/sqlalchemy_tests/mixins.py @@ -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