Skip to content

Commit 57fe0c1

Browse files
authored
chore: apply fixes for UP ruff rule (open-telemetry#5133)
1 parent f092aa7 commit 57fe0c1

146 files changed

Lines changed: 992 additions & 1112 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1212

1313
## Unreleased
1414

15+
- Apply fixes for `UP` ruff rule
16+
([#5133](https://github.com/open-telemetry/opentelemetry-python/pull/5133))
1517
- Switch to SPDX license headers and add CI enforcement
1618
([#5177](https://github.com/open-telemetry/opentelemetry-python/pull/5177))
1719
- `opentelemetry-api`: Enforce W3C Baggage size limits on outbound propagation in `W3CBaggagePropagator.inject()`. Previously only inbound extraction enforced limits; now inject also caps entries at 180, individual pairs at 4096 bytes, and total header at 8192 bytes per the W3C Baggage spec. The extract path max_pairs limit now counts all size-valid entries rather than only successfully parsed ones.

codegen/opentelemetry-codegen-json/src/opentelemetry/codegen/json/generator.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77

88
import logging
99
from collections import defaultdict
10-
from collections.abc import Iterable
10+
from collections.abc import Callable, Iterable
1111
from pathlib import Path
12-
from typing import Callable, Final, Optional, Set
12+
from typing import Final
1313

1414
from google.protobuf import descriptor_pb2 as descriptor
1515
from google.protobuf.compiler import plugin_pb2 as plugin
@@ -125,7 +125,7 @@ def _index_message(
125125
msg_desc: descriptor.DescriptorProto,
126126
package: str,
127127
file_name: str,
128-
parent_path: Optional[str],
128+
parent_path: str | None,
129129
) -> None:
130130
"""
131131
Recursively index a message and its nested types.
@@ -365,7 +365,7 @@ def _generate_imports(
365365
writer.blank_line()
366366
writer.blank_line()
367367

368-
def _collect_imports(self, proto_file: str) -> Set[str]:
368+
def _collect_imports(self, proto_file: str) -> set[str]:
369369
"""
370370
Collect all import statements needed for cross file references.
371371
@@ -421,7 +421,7 @@ def _generate_message_class(
421421
writer: CodeWriter,
422422
proto_file: str,
423423
msg_desc: descriptor.DescriptorProto,
424-
parent_path: Optional[str] = None,
424+
parent_path: str | None = None,
425425
) -> None:
426426
"""
427427
Generate a complete dataclass for a protobuf message.
@@ -951,7 +951,7 @@ def _resolve_enum_type(self, type_name: str, proto_file: str) -> str:
951951
@classmethod
952952
def _get_field_default(
953953
cls, field_desc: descriptor.FieldDescriptorProto
954-
) -> Optional[str]:
954+
) -> str | None:
955955
"""
956956
Get the default value for a field.
957957

codegen/opentelemetry-codegen-json/src/opentelemetry/codegen/json/runtime/json_codec.py

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,14 @@ def to_json(self) -> str:
3838
return json.dumps(self.to_dict())
3939

4040
@classmethod
41-
def from_json(cls: type[M], data: typing.Union[str, bytes]) -> M:
41+
def from_json(cls: type[M], data: str | bytes) -> M:
4242
"""
4343
Deserialize from a JSON string or bytes.
4444
"""
4545
return cls.from_dict(json.loads(data))
4646

4747

48-
def encode_hex(value: typing.Optional[bytes]) -> str:
48+
def encode_hex(value: bytes | None) -> str:
4949
"""
5050
Encode bytes as hex string.
5151
@@ -57,7 +57,7 @@ def encode_hex(value: typing.Optional[bytes]) -> str:
5757
return value.hex() if value else ""
5858

5959

60-
def encode_base64(value: typing.Optional[bytes]) -> str:
60+
def encode_base64(value: bytes | None) -> str:
6161
"""
6262
Encode bytes as base64 string.
6363
Standard Proto3 JSON mapping for bytes.
@@ -83,7 +83,7 @@ def encode_int64(value: int) -> str:
8383
return str(value)
8484

8585

86-
def encode_float(value: float) -> typing.Union[float, str]:
86+
def encode_float(value: float) -> float | str:
8787
"""
8888
Encode float/double values.
8989
@@ -100,7 +100,7 @@ def encode_float(value: float) -> typing.Union[float, str]:
100100

101101

102102
def encode_repeated(
103-
values: typing.Optional[list[T]],
103+
values: list[T] | None,
104104
map_fn: typing.Callable[[T], typing.Any],
105105
) -> list[typing.Any]:
106106
"""
@@ -115,7 +115,7 @@ def encode_repeated(
115115
return [map_fn(v) for v in values] if values else []
116116

117117

118-
def decode_hex(value: typing.Optional[str], field_name: str) -> bytes:
118+
def decode_hex(value: str | None, field_name: str) -> bytes:
119119
"""
120120
Decode hex string to bytes.
121121
@@ -136,7 +136,7 @@ def decode_hex(value: typing.Optional[str], field_name: str) -> bytes:
136136
) from None
137137

138138

139-
def decode_base64(value: typing.Optional[str], field_name: str) -> bytes:
139+
def decode_base64(value: str | None, field_name: str) -> bytes:
140140
"""
141141
Decode base64 string to bytes.
142142
@@ -157,9 +157,7 @@ def decode_base64(value: typing.Optional[str], field_name: str) -> bytes:
157157
) from None
158158

159159

160-
def decode_int64(
161-
value: typing.Optional[typing.Union[int, str]], field_name: str
162-
) -> int:
160+
def decode_int64(value: int | str | None, field_name: str) -> int:
163161
"""
164162
Parse int64 from number or string.
165163
@@ -180,9 +178,7 @@ def decode_int64(
180178
) from None
181179

182180

183-
def decode_float(
184-
value: typing.Optional[typing.Union[float, int, str]], field_name: str
185-
) -> float:
181+
def decode_float(value: float | int | str | None, field_name: str) -> float:
186182
"""
187183
Parse float/double from number or string, handling special values.
188184
@@ -210,7 +206,7 @@ def decode_float(
210206

211207

212208
def decode_repeated(
213-
values: typing.Optional[list[typing.Any]],
209+
values: list[typing.Any] | None,
214210
item_parser: typing.Callable[[typing.Any], T],
215211
field_name: str,
216212
) -> list[T]:
@@ -232,7 +228,7 @@ def decode_repeated(
232228

233229
def validate_type(
234230
value: typing.Any,
235-
expected_types: typing.Union[type, tuple[type, ...]],
231+
expected_types: type | tuple[type, ...],
236232
field_name: str,
237233
) -> None:
238234
"""

codegen/opentelemetry-codegen-json/src/opentelemetry/codegen/json/writer.py

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33

44
from __future__ import annotations
55

6-
from collections.abc import Iterable
6+
from collections.abc import Generator, Iterable
77
from contextlib import contextmanager
8-
from typing import Any, Generator, Optional, Union
8+
from typing import Any
99

1010

1111
# pylint: disable-next=too-many-public-methods
@@ -61,7 +61,7 @@ def writemany(self, *lines: str) -> CodeWriter:
6161
self.writeln(line)
6262
return self
6363

64-
def comment(self, content: Union[str, Iterable[str]]) -> CodeWriter:
64+
def comment(self, content: str | Iterable[str]) -> CodeWriter:
6565
"""
6666
Writes a comment line or block. If content is a string, it writes a single comment line.
6767
@@ -77,7 +77,7 @@ def comment(self, content: Union[str, Iterable[str]]) -> CodeWriter:
7777
self.writeln(f"# {line}" if line else "#")
7878
return self
7979

80-
def docstring(self, content: Union[str, Iterable[str]]) -> CodeWriter:
80+
def docstring(self, content: str | Iterable[str]) -> CodeWriter:
8181
"""
8282
Writes a docstring. If content is a string, it writes a single-line docstring. If content is an iterable of strings, it writes a multi-line docstring.
8383
@@ -128,8 +128,8 @@ def block(self, header: str) -> Generator[CodeWriter, None, None]:
128128
def class_(
129129
self,
130130
name: str,
131-
bases: Optional[Iterable[str]] = None,
132-
decorators: Optional[Iterable[str]] = None,
131+
bases: Iterable[str] | None = None,
132+
decorators: Iterable[str] | None = None,
133133
) -> Generator[CodeWriter, None, None]:
134134
"""
135135
Generate a class definition with optional base classes and decorators.
@@ -153,8 +153,8 @@ def class_(
153153
def dataclass(
154154
self,
155155
name: str,
156-
bases: Optional[Iterable[str]] = None,
157-
decorators: Optional[Iterable[str]] = None,
156+
bases: Iterable[str] | None = None,
157+
decorators: Iterable[str] | None = None,
158158
frozen: bool = False,
159159
slots: bool = False,
160160
decorator_name: str = "dataclasses.dataclass",
@@ -201,8 +201,8 @@ def enum(
201201
self,
202202
name: str,
203203
enum_type: str = "enum.Enum",
204-
bases: Optional[Iterable[str]] = None,
205-
decorators: Optional[Iterable[str]] = None,
204+
bases: Iterable[str] | None = None,
205+
decorators: Iterable[str] | None = None,
206206
) -> Generator[CodeWriter, None, None]:
207207
"""
208208
Generate an enum definition with optional base classes and decorators.
@@ -232,7 +232,7 @@ def field(
232232
name: str,
233233
type_hint: str,
234234
default: Any = None,
235-
default_factory: Optional[str] = None,
235+
default_factory: str | None = None,
236236
) -> CodeWriter:
237237
"""
238238
Write a dataclass field with optional default value or default factory.
@@ -268,9 +268,9 @@ def enum_member(self, name: str, value: Any) -> CodeWriter:
268268
def function(
269269
self,
270270
name: str,
271-
params: Union[Iterable[str], str],
272-
decorators: Optional[Iterable[str]] = None,
273-
return_type: Optional[str] = None,
271+
params: Iterable[str] | str,
272+
decorators: Iterable[str] | None = None,
273+
return_type: str | None = None,
274274
) -> Generator[CodeWriter, None, None]:
275275
"""
276276
Create a function definition with optional decorators and return type.
@@ -296,9 +296,9 @@ def function(
296296
def method(
297297
self,
298298
name: str,
299-
params: Union[Iterable[str], str],
300-
decorators: Optional[Iterable[str]] = None,
301-
return_type: Optional[str] = None,
299+
params: Iterable[str] | str,
300+
decorators: Iterable[str] | None = None,
301+
return_type: str | None = None,
302302
) -> Generator[CodeWriter, None, None]:
303303
"""
304304
Create a method definition within a class with optional decorators and return type.
@@ -375,7 +375,7 @@ def while_(self, condition: str) -> Generator[CodeWriter, None, None]:
375375
yield self
376376

377377
def assignment(
378-
self, var: str, value: str, type_hint: Optional[str] = None
378+
self, var: str, value: str, type_hint: str | None = None
379379
) -> CodeWriter:
380380
"""
381381
Write a variable assignment with optional type hint
@@ -393,7 +393,7 @@ def assignment(
393393
self.writeln(f"{var} = {value}")
394394
return self
395395

396-
def return_(self, value: Optional[str] = None) -> CodeWriter:
396+
def return_(self, value: str | None = None) -> CodeWriter:
397397
"""
398398
Write a return statement with an optional return value
399399

codegen/opentelemetry-codegen-json/tests/test_json_codec.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
# SPDX-License-Identifier: Apache-2.0
33

44
import math
5-
from typing import Optional, Union
65

76
import pytest # type: ignore
87

@@ -29,7 +28,7 @@
2928
(None, ""),
3029
],
3130
)
32-
def test_encode_hex(value: Optional[bytes], expected: str) -> None:
31+
def test_encode_hex(value: bytes | None, expected: str) -> None:
3332
assert encode_hex(value) == expected
3433

3534

@@ -41,7 +40,7 @@ def test_encode_hex(value: Optional[bytes], expected: str) -> None:
4140
(None, b""),
4241
],
4342
)
44-
def test_decode_hex(value: Optional[str], expected: bytes) -> None:
43+
def test_decode_hex(value: str | None, expected: bytes) -> None:
4544
assert decode_hex(value, "field") == expected
4645

4746

@@ -60,7 +59,7 @@ def test_decode_hex_errors() -> None:
6059
(None, ""),
6160
],
6261
)
63-
def test_encode_base64(value: Optional[bytes], expected: str) -> None:
62+
def test_encode_base64(value: bytes | None, expected: str) -> None:
6463
assert encode_base64(value) == expected
6564

6665

@@ -72,7 +71,7 @@ def test_encode_base64(value: Optional[bytes], expected: str) -> None:
7271
(None, b""),
7372
],
7473
)
75-
def test_decode_base64(value: Optional[str], expected: bytes) -> None:
74+
def test_decode_base64(value: str | None, expected: bytes) -> None:
7675
assert decode_base64(value, "field") == expected
7776

7877

@@ -101,7 +100,7 @@ def test_encode_int64(value: int, expected: str) -> None:
101100
(None, 0),
102101
],
103102
)
104-
def test_decode_int64(value: Optional[Union[int, str]], expected: int) -> None:
103+
def test_decode_int64(value: int | str | None, expected: int) -> None:
105104
assert decode_int64(value, "field") == expected
106105

107106

@@ -121,7 +120,7 @@ def test_decode_int64_errors() -> None:
121120
(float("-inf"), "-Infinity"),
122121
],
123122
)
124-
def test_encode_float(value: float, expected: Union[float, str]) -> None:
123+
def test_encode_float(value: float, expected: float | str) -> None:
125124
result = encode_float(value)
126125
if isinstance(expected, float) and math.isnan(expected):
127126
assert math.isnan(result) # type: ignore
@@ -142,7 +141,7 @@ def test_encode_float(value: float, expected: Union[float, str]) -> None:
142141
],
143142
)
144143
def test_decode_float(
145-
value: Optional[Union[float, int, str]], expected: float
144+
value: float | int | str | None, expected: float
146145
) -> None:
147146
result = decode_float(value, "field")
148147
if math.isnan(expected):

0 commit comments

Comments
 (0)