diff --git a/.flake8 b/.flake8 index 83786a686a7..8e17b0ab21a 100644 --- a/.flake8 +++ b/.flake8 @@ -12,6 +12,10 @@ ignore = # allow whitespace before ':' (https://github.com/psf/black#slices) E203 + # conflicts with black + E701 + E704 + exclude = .bzr .git diff --git a/dev-requirements.txt b/dev-requirements.txt index 01f46ae87e9..90385cc6fab 100644 --- a/dev-requirements.txt +++ b/dev-requirements.txt @@ -1,7 +1,7 @@ pylint==3.0.2 flake8==6.1.0 isort==5.12.0 -black==22.3.0 +black==24.3.0 httpretty==1.1.4 mypy==1.9.0 sphinx==7.1.2 diff --git a/exporter/opentelemetry-exporter-opencensus/src/opentelemetry/exporter/opencensus/trace_exporter/__init__.py b/exporter/opentelemetry-exporter-opencensus/src/opentelemetry/exporter/opencensus/trace_exporter/__init__.py index b855728cad5..e5d7e6239a8 100644 --- a/exporter/opentelemetry-exporter-opencensus/src/opentelemetry/exporter/opencensus/trace_exporter/__init__.py +++ b/exporter/opentelemetry-exporter-opencensus/src/opentelemetry/exporter/opencensus/trace_exporter/__init__.py @@ -133,11 +133,11 @@ def translate_to_collector(spans: Sequence[ReadableSpan]): collector_span.parent_span_id = parent_id.to_bytes(8, "big") if span.context.trace_state is not None: - for (key, value) in span.context.trace_state.items(): + for key, value in span.context.trace_state.items(): collector_span.tracestate.entries.add(key=key, value=value) if span.attributes: - for (key, value) in span.attributes.items(): + for key, value in span.attributes.items(): utils.add_proto_attribute_value( collector_span.attributes, key, value ) @@ -150,7 +150,7 @@ def translate_to_collector(spans: Sequence[ReadableSpan]): ) if event.attributes: - for (key, value) in event.attributes.items(): + for key, value in event.attributes.items(): utils.add_proto_attribute_value( collector_annotation.attributes, key, value ) @@ -183,7 +183,7 @@ def translate_to_collector(spans: Sequence[ReadableSpan]): ) if link.attributes: - for (key, value) in link.attributes.items(): + for key, value in link.attributes.items(): utils.add_proto_attribute_value( collector_span_link.attributes, key, value ) diff --git a/exporter/opentelemetry-exporter-prometheus/src/opentelemetry/exporter/prometheus/__init__.py b/exporter/opentelemetry-exporter-prometheus/src/opentelemetry/exporter/prometheus/__init__.py index 6cde540ef6f..a84e21214d7 100644 --- a/exporter/opentelemetry-exporter-prometheus/src/opentelemetry/exporter/prometheus/__init__.py +++ b/exporter/opentelemetry-exporter-prometheus/src/opentelemetry/exporter/prometheus/__init__.py @@ -196,9 +196,9 @@ def collect(self) -> None: self._target_info = self._create_info_metric( _TARGET_INFO_NAME, _TARGET_INFO_DESCRIPTION, attributes ) - metric_family_id_metric_family[ - _TARGET_INFO_NAME - ] = self._target_info + metric_family_id_metric_family[_TARGET_INFO_NAME] = ( + self._target_info + ) while self._metrics_datas: self._translate_to_prometheus( @@ -294,13 +294,13 @@ def _translate_to_prometheus( ) if metric_family_id not in metric_family_id_metric_family: - metric_family_id_metric_family[ - metric_family_id - ] = CounterMetricFamily( - name=metric_name, - documentation=metric_description, - labels=label_keys, - unit=metric.unit, + metric_family_id_metric_family[metric_family_id] = ( + CounterMetricFamily( + name=metric_name, + documentation=metric_description, + labels=label_keys, + unit=metric.unit, + ) ) metric_family_id_metric_family[ metric_family_id @@ -318,13 +318,13 @@ def _translate_to_prometheus( metric_family_id not in metric_family_id_metric_family.keys() ): - metric_family_id_metric_family[ - metric_family_id - ] = GaugeMetricFamily( - name=metric_name, - documentation=metric_description, - labels=label_keys, - unit=metric.unit, + metric_family_id_metric_family[metric_family_id] = ( + GaugeMetricFamily( + name=metric_name, + documentation=metric_description, + labels=label_keys, + unit=metric.unit, + ) ) metric_family_id_metric_family[ metric_family_id @@ -339,13 +339,13 @@ def _translate_to_prometheus( metric_family_id not in metric_family_id_metric_family.keys() ): - metric_family_id_metric_family[ - metric_family_id - ] = HistogramMetricFamily( - name=metric_name, - documentation=metric_description, - labels=label_keys, - unit=metric.unit, + metric_family_id_metric_family[metric_family_id] = ( + HistogramMetricFamily( + name=metric_name, + documentation=metric_description, + labels=label_keys, + unit=metric.unit, + ) ) metric_family_id_metric_family[ metric_family_id diff --git a/opentelemetry-api/tests/propagators/test_w3cbaggagepropagator.py b/opentelemetry-api/tests/propagators/test_w3cbaggagepropagator.py index ccc4b3cb2d0..99e37652ab0 100644 --- a/opentelemetry-api/tests/propagators/test_w3cbaggagepropagator.py +++ b/opentelemetry-api/tests/propagators/test_w3cbaggagepropagator.py @@ -134,11 +134,13 @@ def test_header_max_entries_skip_invalid_entry(self): self._extract( ",".join( [ - f"key{index}=value{index}" - if index != 2 - else ( - f"key{index}=" - f"value{'s' * (W3CBaggagePropagator._MAX_PAIR_LENGTH + 1)}" + ( + f"key{index}=value{index}" + if index != 2 + else ( + f"key{index}=" + f"value{'s' * (W3CBaggagePropagator._MAX_PAIR_LENGTH + 1)}" + ) ) for index in range( W3CBaggagePropagator._MAX_PAIRS + 1 @@ -162,9 +164,11 @@ def test_header_max_entries_skip_invalid_entry(self): self._extract( ",".join( [ - f"key{index}=value{index}" - if index != 2 - else f"key{index}xvalue{index}" + ( + f"key{index}=value{index}" + if index != 2 + else f"key{index}xvalue{index}" + ) for index in range( W3CBaggagePropagator._MAX_PAIRS + 1 ) diff --git a/opentelemetry-sdk/src/opentelemetry/sdk/_configuration/__init__.py b/opentelemetry-sdk/src/opentelemetry/sdk/_configuration/__init__.py index 33c5147a599..14c560c41b0 100644 --- a/opentelemetry-sdk/src/opentelemetry/sdk/_configuration/__init__.py +++ b/opentelemetry-sdk/src/opentelemetry/sdk/_configuration/__init__.py @@ -265,7 +265,10 @@ def _import_exporters( metric_exporters = {} log_exporters = {} - for (exporter_name, exporter_impl,) in _import_config_components( + for ( + exporter_name, + exporter_impl, + ) in _import_config_components( trace_exporter_names, "opentelemetry_traces_exporter" ): if issubclass(exporter_impl, SpanExporter): @@ -273,7 +276,10 @@ def _import_exporters( else: raise RuntimeError(f"{exporter_name} is not a trace exporter") - for (exporter_name, exporter_impl,) in _import_config_components( + for ( + exporter_name, + exporter_impl, + ) in _import_config_components( metric_exporter_names, "opentelemetry_metrics_exporter" ): # The metric exporter components may be push MetricExporter or pull exporters which @@ -283,7 +289,10 @@ def _import_exporters( else: raise RuntimeError(f"{exporter_name} is not a metric exporter") - for (exporter_name, exporter_impl,) in _import_config_components( + for ( + exporter_name, + exporter_impl, + ) in _import_config_components( log_exporter_names, "opentelemetry_logs_exporter" ): if issubclass(exporter_impl, LogExporter): @@ -360,9 +369,9 @@ def _initialize_components(auto_instrumentation_version): auto_resource = {} # populate version if using auto-instrumentation if auto_instrumentation_version: - auto_resource[ - ResourceAttributes.TELEMETRY_AUTO_VERSION - ] = auto_instrumentation_version + auto_resource[ResourceAttributes.TELEMETRY_AUTO_VERSION] = ( + auto_instrumentation_version + ) resource = Resource.create(auto_resource) _init_tracing( diff --git a/opentelemetry-sdk/src/opentelemetry/sdk/_logs/_internal/__init__.py b/opentelemetry-sdk/src/opentelemetry/sdk/_logs/_internal/__init__.py index 8ba0dae6f2e..08835942971 100644 --- a/opentelemetry-sdk/src/opentelemetry/sdk/_logs/_internal/__init__.py +++ b/opentelemetry-sdk/src/opentelemetry/sdk/_logs/_internal/__init__.py @@ -202,22 +202,26 @@ def to_json(self, indent=4) -> str: "body": self.body, "severity_number": repr(self.severity_number), "severity_text": self.severity_text, - "attributes": dict(self.attributes) - if bool(self.attributes) - else None, + "attributes": ( + dict(self.attributes) if bool(self.attributes) else None + ), "dropped_attributes": self.dropped_attributes, "timestamp": ns_to_iso_str(self.timestamp), "observed_timestamp": ns_to_iso_str(self.observed_timestamp), - "trace_id": f"0x{format_trace_id(self.trace_id)}" - if self.trace_id is not None - else "", - "span_id": f"0x{format_span_id(self.span_id)}" - if self.span_id is not None - else "", + "trace_id": ( + f"0x{format_trace_id(self.trace_id)}" + if self.trace_id is not None + else "" + ), + "span_id": ( + f"0x{format_span_id(self.span_id)}" + if self.span_id is not None + else "" + ), "trace_flags": self.trace_flags, - "resource": repr(self.resource.attributes) - if self.resource - else "", + "resource": ( + repr(self.resource.attributes) if self.resource else "" + ), }, indent=indent, ) diff --git a/opentelemetry-sdk/src/opentelemetry/sdk/metrics/_internal/export/__init__.py b/opentelemetry-sdk/src/opentelemetry/sdk/metrics/_internal/export/__init__.py index 14546636a94..02dc2cf287a 100644 --- a/opentelemetry-sdk/src/opentelemetry/sdk/metrics/_internal/export/__init__.py +++ b/opentelemetry-sdk/src/opentelemetry/sdk/metrics/_internal/export/__init__.py @@ -247,27 +247,27 @@ def __init__( if typ is Counter: self._instrument_class_temporality[_Counter] = temporality elif typ is UpDownCounter: - self._instrument_class_temporality[ - _UpDownCounter - ] = temporality + self._instrument_class_temporality[_UpDownCounter] = ( + temporality + ) elif typ is Histogram: - self._instrument_class_temporality[ - _Histogram - ] = temporality + self._instrument_class_temporality[_Histogram] = ( + temporality + ) elif typ is Gauge: self._instrument_class_temporality[_Gauge] = temporality elif typ is ObservableCounter: - self._instrument_class_temporality[ - _ObservableCounter - ] = temporality + self._instrument_class_temporality[_ObservableCounter] = ( + temporality + ) elif typ is ObservableUpDownCounter: self._instrument_class_temporality[ _ObservableUpDownCounter ] = temporality elif typ is ObservableGauge: - self._instrument_class_temporality[ - _ObservableGauge - ] = temporality + self._instrument_class_temporality[_ObservableGauge] = ( + temporality + ) else: raise Exception(f"Invalid instrument class found {typ}") @@ -287,27 +287,27 @@ def __init__( if typ is Counter: self._instrument_class_aggregation[_Counter] = aggregation elif typ is UpDownCounter: - self._instrument_class_aggregation[ - _UpDownCounter - ] = aggregation + self._instrument_class_aggregation[_UpDownCounter] = ( + aggregation + ) elif typ is Histogram: - self._instrument_class_aggregation[ - _Histogram - ] = aggregation + self._instrument_class_aggregation[_Histogram] = ( + aggregation + ) elif typ is Gauge: self._instrument_class_aggregation[_Gauge] = aggregation elif typ is ObservableCounter: - self._instrument_class_aggregation[ - _ObservableCounter - ] = aggregation + self._instrument_class_aggregation[_ObservableCounter] = ( + aggregation + ) elif typ is ObservableUpDownCounter: self._instrument_class_aggregation[ _ObservableUpDownCounter ] = aggregation elif typ is ObservableGauge: - self._instrument_class_aggregation[ - _ObservableGauge - ] = aggregation + self._instrument_class_aggregation[_ObservableGauge] = ( + aggregation + ) else: raise Exception(f"Invalid instrument class found {typ}") @@ -398,13 +398,13 @@ def __init__( preferred_aggregation=preferred_aggregation, ) self._lock = RLock() - self._metrics_data: ( - "opentelemetry.sdk.metrics.export.MetricsData" - ) = None + self._metrics_data: "opentelemetry.sdk.metrics.export.MetricsData" = ( + None + ) def get_metrics_data( self, - ) -> ("opentelemetry.sdk.metrics.export.MetricsData"): + ) -> "opentelemetry.sdk.metrics.export.MetricsData": """Reads and returns current metrics from the SDK""" with self._lock: self.collect() @@ -551,9 +551,7 @@ def _shutdown(): self._shutdown_event.set() if self._daemon_thread: - self._daemon_thread.join( - timeout=(deadline_ns - time_ns()) / 10**9 - ) + self._daemon_thread.join(timeout=(deadline_ns - time_ns()) / 10**9) self._exporter.shutdown(timeout=(deadline_ns - time_ns()) / 10**6) def force_flush(self, timeout_millis: float = 10_000) -> bool: diff --git a/opentelemetry-sdk/src/opentelemetry/sdk/metrics/_internal/metric_reader_storage.py b/opentelemetry-sdk/src/opentelemetry/sdk/metrics/_internal/metric_reader_storage.py index 700ace87204..7fac6c6c105 100644 --- a/opentelemetry-sdk/src/opentelemetry/sdk/metrics/_internal/metric_reader_storage.py +++ b/opentelemetry-sdk/src/opentelemetry/sdk/metrics/_internal/metric_reader_storage.py @@ -107,9 +107,9 @@ def _get_or_init_view_instrument_match( ), ) ) - self._instrument_view_instrument_matches[ - instrument - ] = view_instrument_matches + self._instrument_view_instrument_matches[instrument] = ( + view_instrument_matches + ) return view_instrument_matches @@ -136,9 +136,9 @@ def collect(self) -> Optional[MetricsData]: with self._lock: - instrumentation_scope_scope_metrics: ( - Dict[InstrumentationScope, ScopeMetrics] - ) = {} + instrumentation_scope_scope_metrics: Dict[ + InstrumentationScope, ScopeMetrics + ] = {} for ( instrument, diff --git a/opentelemetry-sdk/src/opentelemetry/sdk/resources/__init__.py b/opentelemetry-sdk/src/opentelemetry/sdk/resources/__init__.py index 852b23f5002..20dd56862a0 100644 --- a/opentelemetry-sdk/src/opentelemetry/sdk/resources/__init__.py +++ b/opentelemetry-sdk/src/opentelemetry/sdk/resources/__init__.py @@ -335,10 +335,12 @@ def detect(self) -> "Resource": _runtime_version = ".".join( map( str, - sys.version_info[:3] - if sys.version_info.releaselevel == "final" - and not sys.version_info.serial - else sys.version_info, + ( + sys.version_info[:3] + if sys.version_info.releaselevel == "final" + and not sys.version_info.serial + else sys.version_info + ), ) ) _process_pid = os.getpid() diff --git a/opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py b/opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py index c07f4f42c46..55a375315b4 100644 --- a/opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py +++ b/opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py @@ -485,9 +485,9 @@ def to_json(self, indent: int = 4): f_span = { "name": self._name, - "context": self._format_context(self._context) - if self._context - else None, + "context": ( + self._format_context(self._context) if self._context else None + ), "kind": str(self.kind), "parent_id": parent_id, "start_time": start_time, @@ -627,23 +627,29 @@ def __init__( self.max_span_attributes = self._from_env_if_absent( max_span_attributes, OTEL_SPAN_ATTRIBUTE_COUNT_LIMIT, - global_max_attributes - if global_max_attributes is not None - else _DEFAULT_OTEL_SPAN_ATTRIBUTE_COUNT_LIMIT, + ( + global_max_attributes + if global_max_attributes is not None + else _DEFAULT_OTEL_SPAN_ATTRIBUTE_COUNT_LIMIT + ), ) self.max_event_attributes = self._from_env_if_absent( max_event_attributes, OTEL_EVENT_ATTRIBUTE_COUNT_LIMIT, - global_max_attributes - if global_max_attributes is not None - else _DEFAULT_OTEL_EVENT_ATTRIBUTE_COUNT_LIMIT, + ( + global_max_attributes + if global_max_attributes is not None + else _DEFAULT_OTEL_EVENT_ATTRIBUTE_COUNT_LIMIT + ), ) self.max_link_attributes = self._from_env_if_absent( max_link_attributes, OTEL_LINK_ATTRIBUTE_COUNT_LIMIT, - global_max_attributes - if global_max_attributes is not None - else _DEFAULT_OTEL_LINK_ATTRIBUTE_COUNT_LIMIT, + ( + global_max_attributes + if global_max_attributes is not None + else _DEFAULT_OTEL_LINK_ATTRIBUTE_COUNT_LIMIT + ), ) # attribute length diff --git a/opentelemetry-sdk/src/opentelemetry/sdk/util/__init__.py b/opentelemetry-sdk/src/opentelemetry/sdk/util/__init__.py index c74ecd0c649..68f10ddc9e9 100644 --- a/opentelemetry-sdk/src/opentelemetry/sdk/util/__init__.py +++ b/opentelemetry-sdk/src/opentelemetry/sdk/util/__init__.py @@ -34,9 +34,9 @@ def get_dict_as_key(labels): return tuple( sorted( map( - lambda kv: (kv[0], tuple(kv[1])) - if isinstance(kv[1], list) - else kv, + lambda kv: ( + (kv[0], tuple(kv[1])) if isinstance(kv[1], list) else kv + ), labels.items(), ) ) diff --git a/opentelemetry-sdk/tests/metrics/integration_test/test_cpu_time.py b/opentelemetry-sdk/tests/metrics/integration_test/test_cpu_time.py index 7b440c0332a..18b8cbdcea0 100644 --- a/opentelemetry-sdk/tests/metrics/integration_test/test_cpu_time.py +++ b/opentelemetry-sdk/tests/metrics/integration_test/test_cpu_time.py @@ -188,9 +188,9 @@ def cpu_time_callback( ) def test_cpu_time_generator(self): - def cpu_time_generator() -> Generator[ - Iterable[Observation], None, None - ]: + def cpu_time_generator() -> ( + Generator[Iterable[Observation], None, None] + ): options = yield while True: self.assertIsInstance(options, CallbackOptions) diff --git a/opentelemetry-sdk/tests/metrics/integration_test/test_explicit_bucket_histogram_aggregation.py b/opentelemetry-sdk/tests/metrics/integration_test/test_explicit_bucket_histogram_aggregation.py index 6db35fd4c0a..bdae2aad13e 100644 --- a/opentelemetry-sdk/tests/metrics/integration_test/test_explicit_bucket_histogram_aggregation.py +++ b/opentelemetry-sdk/tests/metrics/integration_test/test_explicit_bucket_histogram_aggregation.py @@ -193,9 +193,11 @@ def test_synchronous_cumulative_temporality(self): metric_data.bucket_counts, tuple( [ - 0 - if internal_index < 1 or internal_index > index + 1 - else 1 + ( + 0 + if internal_index < 1 or internal_index > index + 1 + else 1 + ) for internal_index in range(16) ] ), diff --git a/opentelemetry-sdk/tests/resources/test_resources.py b/opentelemetry-sdk/tests/resources/test_resources.py index da3f9469617..a0c6159010d 100644 --- a/opentelemetry-sdk/tests/resources/test_resources.py +++ b/opentelemetry-sdk/tests/resources/test_resources.py @@ -511,9 +511,9 @@ def test_invalid_key_value_pairs(self): def test_multiple_with_url_decode(self): detector = OTELResourceDetector() - environ[ - OTEL_RESOURCE_ATTRIBUTES - ] = "key=value%20test%0A, key2=value+%202" + environ[OTEL_RESOURCE_ATTRIBUTES] = ( + "key=value%20test%0A, key2=value+%202" + ) self.assertEqual( detector.detect(), Resource({"key": "value test\n", "key2": "value+ 2"}), diff --git a/shim/opentelemetry-opencensus-shim/src/opentelemetry/shim/opencensus/_shim_span.py b/shim/opentelemetry-opencensus-shim/src/opentelemetry/shim/opencensus/_shim_span.py index f3ff804c6f2..2012035247a 100644 --- a/shim/opentelemetry-opencensus-shim/src/opentelemetry/shim/opencensus/_shim_span.py +++ b/shim/opentelemetry-opencensus-shim/src/opentelemetry/shim/opencensus/_shim_span.py @@ -97,13 +97,13 @@ def add_message_event(self, message_event: MessageEvent): ], } if message_event.uncompressed_size_bytes is not None: - attrs[ - _MESSAGE_EVENT_ATTRIBUTE_KEY_SIZE_UNCOMPRESSED - ] = message_event.uncompressed_size_bytes + attrs[_MESSAGE_EVENT_ATTRIBUTE_KEY_SIZE_UNCOMPRESSED] = ( + message_event.uncompressed_size_bytes + ) if message_event.compressed_size_bytes is not None: - attrs[ - _MESSAGE_EVENT_ATTRIBUTE_KEY_SIZE_COMPRESSED - ] = message_event.compressed_size_bytes + attrs[_MESSAGE_EVENT_ATTRIBUTE_KEY_SIZE_COMPRESSED] = ( + message_event.compressed_size_bytes + ) timestamp = _opencensus_time_to_nanos(message_event.timestamp) self._self_otel_span.add_event( diff --git a/shim/opentelemetry-opentracing-shim/tests/test_shim.py b/shim/opentelemetry-opentracing-shim/tests/test_shim.py index 99394ad2169..b75915d20fe 100644 --- a/shim/opentelemetry-opentracing-shim/tests/test_shim.py +++ b/shim/opentelemetry-opentracing-shim/tests/test_shim.py @@ -302,7 +302,7 @@ def test_parent_child_implicit(self): # Verify parent span becomes the active span again. self.assertEqual( self.shim.active_span.context.unwrap(), - parent.span.context.unwrap() + parent.span.context.unwrap(), # TODO: Check equality of the spans themselves rather than # their context once the SpanShim reconstruction problem has # been addressed (see previous TODO).