Skip to content

Commit 5c2e06f

Browse files
committed
Apply fixes for UP ruff rule
Signed-off-by: emdneto <9735060+emdneto@users.noreply.github.com>
1 parent 8c82502 commit 5c2e06f

140 files changed

Lines changed: 977 additions & 1073 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+
([#5132](https://github.com/open-telemetry/opentelemetry-python/pull/5132))
1517
- Fix incorrect code example in `create_tracer()` docstring
1618
([#5072](https://github.com/open-telemetry/opentelemetry-python/issues/5072))
1719
- `opentelemetry-sdk`: add `load_entry_point` shared utility to declarative file configuration for loading plugins via entry points; refactor propagator loading to use it

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

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

1919
import logging
2020
from collections import defaultdict
21-
from collections.abc import Iterable
21+
from collections.abc import Callable, Iterable
2222
from pathlib import Path
23-
from typing import Callable, Final, Optional, Set
23+
from typing import Final
2424

2525
from google.protobuf import descriptor_pb2 as descriptor
2626
from google.protobuf.compiler import plugin_pb2 as plugin
@@ -136,7 +136,7 @@ def _index_message(
136136
msg_desc: descriptor.DescriptorProto,
137137
package: str,
138138
file_name: str,
139-
parent_path: Optional[str],
139+
parent_path: str | None,
140140
) -> None:
141141
"""
142142
Recursively index a message and its nested types.
@@ -376,7 +376,7 @@ def _generate_imports(
376376
writer.blank_line()
377377
writer.blank_line()
378378

379-
def _collect_imports(self, proto_file: str) -> Set[str]:
379+
def _collect_imports(self, proto_file: str) -> set[str]:
380380
"""
381381
Collect all import statements needed for cross file references.
382382
@@ -432,7 +432,7 @@ def _generate_message_class(
432432
writer: CodeWriter,
433433
proto_file: str,
434434
msg_desc: descriptor.DescriptorProto,
435-
parent_path: Optional[str] = None,
435+
parent_path: str | None = None,
436436
) -> None:
437437
"""
438438
Generate a complete dataclass for a protobuf message.
@@ -962,7 +962,7 @@ def _resolve_enum_type(self, type_name: str, proto_file: str) -> str:
962962
@classmethod
963963
def _get_field_default(
964964
cls, field_desc: descriptor.FieldDescriptorProto
965-
) -> Optional[str]:
965+
) -> str | None:
966966
"""
967967
Get the default value for a field.
968968

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
@@ -49,14 +49,14 @@ def to_json(self) -> str:
4949
return json.dumps(self.to_dict())
5050

5151
@classmethod
52-
def from_json(cls: type[M], data: typing.Union[str, bytes]) -> M:
52+
def from_json(cls: type[M], data: str | bytes) -> M:
5353
"""
5454
Deserialize from a JSON string or bytes.
5555
"""
5656
return cls.from_dict(json.loads(data))
5757

5858

59-
def encode_hex(value: typing.Optional[bytes]) -> str:
59+
def encode_hex(value: bytes | None) -> str:
6060
"""
6161
Encode bytes as hex string.
6262
@@ -68,7 +68,7 @@ def encode_hex(value: typing.Optional[bytes]) -> str:
6868
return value.hex() if value else ""
6969

7070

71-
def encode_base64(value: typing.Optional[bytes]) -> str:
71+
def encode_base64(value: bytes | None) -> str:
7272
"""
7373
Encode bytes as base64 string.
7474
Standard Proto3 JSON mapping for bytes.
@@ -94,7 +94,7 @@ def encode_int64(value: int) -> str:
9494
return str(value)
9595

9696

97-
def encode_float(value: float) -> typing.Union[float, str]:
97+
def encode_float(value: float) -> float | str:
9898
"""
9999
Encode float/double values.
100100
@@ -111,7 +111,7 @@ def encode_float(value: float) -> typing.Union[float, str]:
111111

112112

113113
def encode_repeated(
114-
values: typing.Optional[list[T]],
114+
values: list[T] | None,
115115
map_fn: typing.Callable[[T], typing.Any],
116116
) -> list[typing.Any]:
117117
"""
@@ -126,7 +126,7 @@ def encode_repeated(
126126
return [map_fn(v) for v in values] if values else []
127127

128128

129-
def decode_hex(value: typing.Optional[str], field_name: str) -> bytes:
129+
def decode_hex(value: str | None, field_name: str) -> bytes:
130130
"""
131131
Decode hex string to bytes.
132132
@@ -147,7 +147,7 @@ def decode_hex(value: typing.Optional[str], field_name: str) -> bytes:
147147
) from None
148148

149149

150-
def decode_base64(value: typing.Optional[str], field_name: str) -> bytes:
150+
def decode_base64(value: str | None, field_name: str) -> bytes:
151151
"""
152152
Decode base64 string to bytes.
153153
@@ -168,9 +168,7 @@ def decode_base64(value: typing.Optional[str], field_name: str) -> bytes:
168168
) from None
169169

170170

171-
def decode_int64(
172-
value: typing.Optional[typing.Union[int, str]], field_name: str
173-
) -> int:
171+
def decode_int64(value: int | str | None, field_name: str) -> int:
174172
"""
175173
Parse int64 from number or string.
176174
@@ -191,9 +189,7 @@ def decode_int64(
191189
) from None
192190

193191

194-
def decode_float(
195-
value: typing.Optional[typing.Union[float, int, str]], field_name: str
196-
) -> float:
192+
def decode_float(value: float | int | str | None, field_name: str) -> float:
197193
"""
198194
Parse float/double from number or string, handling special values.
199195
@@ -221,7 +217,7 @@ def decode_float(
221217

222218

223219
def decode_repeated(
224-
values: typing.Optional[list[typing.Any]],
220+
values: list[typing.Any] | None,
225221
item_parser: typing.Callable[[typing.Any], T],
226222
field_name: str,
227223
) -> list[T]:
@@ -243,7 +239,7 @@ def decode_repeated(
243239

244240
def validate_type(
245241
value: typing.Any,
246-
expected_types: typing.Union[type, tuple[type, ...]],
242+
expected_types: type | tuple[type, ...],
247243
field_name: str,
248244
) -> None:
249245
"""

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

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

1515
from __future__ import annotations
1616

17-
from collections.abc import Iterable
17+
from collections.abc import Generator, Iterable
1818
from contextlib import contextmanager
19-
from typing import Any, Generator, Optional, Union
19+
from typing import Any
2020

2121

2222
# pylint: disable-next=too-many-public-methods
@@ -72,7 +72,7 @@ def writemany(self, *lines: str) -> CodeWriter:
7272
self.writeln(line)
7373
return self
7474

75-
def comment(self, content: Union[str, Iterable[str]]) -> CodeWriter:
75+
def comment(self, content: str | Iterable[str]) -> CodeWriter:
7676
"""
7777
Writes a comment line or block. If content is a string, it writes a single comment line.
7878
@@ -88,7 +88,7 @@ def comment(self, content: Union[str, Iterable[str]]) -> CodeWriter:
8888
self.writeln(f"# {line}" if line else "#")
8989
return self
9090

91-
def docstring(self, content: Union[str, Iterable[str]]) -> CodeWriter:
91+
def docstring(self, content: str | Iterable[str]) -> CodeWriter:
9292
"""
9393
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.
9494
@@ -139,8 +139,8 @@ def block(self, header: str) -> Generator[CodeWriter, None, None]:
139139
def class_(
140140
self,
141141
name: str,
142-
bases: Optional[Iterable[str]] = None,
143-
decorators: Optional[Iterable[str]] = None,
142+
bases: Iterable[str] | None = None,
143+
decorators: Iterable[str] | None = None,
144144
) -> Generator[CodeWriter, None, None]:
145145
"""
146146
Generate a class definition with optional base classes and decorators.
@@ -164,8 +164,8 @@ def class_(
164164
def dataclass(
165165
self,
166166
name: str,
167-
bases: Optional[Iterable[str]] = None,
168-
decorators: Optional[Iterable[str]] = None,
167+
bases: Iterable[str] | None = None,
168+
decorators: Iterable[str] | None = None,
169169
frozen: bool = False,
170170
slots: bool = False,
171171
decorator_name: str = "dataclasses.dataclass",
@@ -212,8 +212,8 @@ def enum(
212212
self,
213213
name: str,
214214
enum_type: str = "enum.Enum",
215-
bases: Optional[Iterable[str]] = None,
216-
decorators: Optional[Iterable[str]] = None,
215+
bases: Iterable[str] | None = None,
216+
decorators: Iterable[str] | None = None,
217217
) -> Generator[CodeWriter, None, None]:
218218
"""
219219
Generate an enum definition with optional base classes and decorators.
@@ -243,7 +243,7 @@ def field(
243243
name: str,
244244
type_hint: str,
245245
default: Any = None,
246-
default_factory: Optional[str] = None,
246+
default_factory: str | None = None,
247247
) -> CodeWriter:
248248
"""
249249
Write a dataclass field with optional default value or default factory.
@@ -279,9 +279,9 @@ def enum_member(self, name: str, value: Any) -> CodeWriter:
279279
def function(
280280
self,
281281
name: str,
282-
params: Union[Iterable[str], str],
283-
decorators: Optional[Iterable[str]] = None,
284-
return_type: Optional[str] = None,
282+
params: Iterable[str] | str,
283+
decorators: Iterable[str] | None = None,
284+
return_type: str | None = None,
285285
) -> Generator[CodeWriter, None, None]:
286286
"""
287287
Create a function definition with optional decorators and return type.
@@ -307,9 +307,9 @@ def function(
307307
def method(
308308
self,
309309
name: str,
310-
params: Union[Iterable[str], str],
311-
decorators: Optional[Iterable[str]] = None,
312-
return_type: Optional[str] = None,
310+
params: Iterable[str] | str,
311+
decorators: Iterable[str] | None = None,
312+
return_type: str | None = None,
313313
) -> Generator[CodeWriter, None, None]:
314314
"""
315315
Create a method definition within a class with optional decorators and return type.
@@ -386,7 +386,7 @@ def while_(self, condition: str) -> Generator[CodeWriter, None, None]:
386386
yield self
387387

388388
def assignment(
389-
self, var: str, value: str, type_hint: Optional[str] = None
389+
self, var: str, value: str, type_hint: str | None = None
390390
) -> CodeWriter:
391391
"""
392392
Write a variable assignment with optional type hint
@@ -404,7 +404,7 @@ def assignment(
404404
self.writeln(f"{var} = {value}")
405405
return self
406406

407-
def return_(self, value: Optional[str] = None) -> CodeWriter:
407+
def return_(self, value: str | None = None) -> CodeWriter:
408408
"""
409409
Write a return statement with an optional return value
410410

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

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
# limitations under the License.
1414

1515
import math
16-
from typing import Optional, Union
1716

1817
import pytest # type: ignore
1918

@@ -40,7 +39,7 @@
4039
(None, ""),
4140
],
4241
)
43-
def test_encode_hex(value: Optional[bytes], expected: str) -> None:
42+
def test_encode_hex(value: bytes | None, expected: str) -> None:
4443
assert encode_hex(value) == expected
4544

4645

@@ -52,7 +51,7 @@ def test_encode_hex(value: Optional[bytes], expected: str) -> None:
5251
(None, b""),
5352
],
5453
)
55-
def test_decode_hex(value: Optional[str], expected: bytes) -> None:
54+
def test_decode_hex(value: str | None, expected: bytes) -> None:
5655
assert decode_hex(value, "field") == expected
5756

5857

@@ -71,7 +70,7 @@ def test_decode_hex_errors() -> None:
7170
(None, ""),
7271
],
7372
)
74-
def test_encode_base64(value: Optional[bytes], expected: str) -> None:
73+
def test_encode_base64(value: bytes | None, expected: str) -> None:
7574
assert encode_base64(value) == expected
7675

7776

@@ -83,7 +82,7 @@ def test_encode_base64(value: Optional[bytes], expected: str) -> None:
8382
(None, b""),
8483
],
8584
)
86-
def test_decode_base64(value: Optional[str], expected: bytes) -> None:
85+
def test_decode_base64(value: str | None, expected: bytes) -> None:
8786
assert decode_base64(value, "field") == expected
8887

8988

@@ -112,7 +111,7 @@ def test_encode_int64(value: int, expected: str) -> None:
112111
(None, 0),
113112
],
114113
)
115-
def test_decode_int64(value: Optional[Union[int, str]], expected: int) -> None:
114+
def test_decode_int64(value: int | str | None, expected: int) -> None:
116115
assert decode_int64(value, "field") == expected
117116

118117

@@ -132,7 +131,7 @@ def test_decode_int64_errors() -> None:
132131
(float("-inf"), "-Infinity"),
133132
],
134133
)
135-
def test_encode_float(value: float, expected: Union[float, str]) -> None:
134+
def test_encode_float(value: float, expected: float | str) -> None:
136135
result = encode_float(value)
137136
if isinstance(expected, float) and math.isnan(expected):
138137
assert math.isnan(result) # type: ignore
@@ -153,7 +152,7 @@ def test_encode_float(value: float, expected: Union[float, str]) -> None:
153152
],
154153
)
155154
def test_decode_float(
156-
value: Optional[Union[float, int, str]], expected: float
155+
value: float | int | str | None, expected: float
157156
) -> None:
158157
result = decode_float(value, "field")
159158
if math.isnan(expected):

0 commit comments

Comments
 (0)