Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## Unreleased

- Apply fixes for `UP` ruff rule
([#5132](https://github.com/open-telemetry/opentelemetry-python/pull/5132))
- Fix incorrect code example in `create_tracer()` docstring
([#5072](https://github.com/open-telemetry/opentelemetry-python/issues/5072))
- `opentelemetry-sdk`: add `load_entry_point` shared utility to declarative file configuration for loading plugins via entry points; refactor propagator loading to use it
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@

import logging
from collections import defaultdict
from collections.abc import Iterable
from collections.abc import Callable, Iterable
from pathlib import Path
from typing import Callable, Final, Optional, Set
from typing import Final

from google.protobuf import descriptor_pb2 as descriptor
from google.protobuf.compiler import plugin_pb2 as plugin
Expand Down Expand Up @@ -136,7 +136,7 @@ def _index_message(
msg_desc: descriptor.DescriptorProto,
package: str,
file_name: str,
parent_path: Optional[str],
parent_path: str | None,
) -> None:
"""
Recursively index a message and its nested types.
Expand Down Expand Up @@ -376,7 +376,7 @@ def _generate_imports(
writer.blank_line()
writer.blank_line()

def _collect_imports(self, proto_file: str) -> Set[str]:
def _collect_imports(self, proto_file: str) -> set[str]:
"""
Collect all import statements needed for cross file references.

Expand Down Expand Up @@ -432,7 +432,7 @@ def _generate_message_class(
writer: CodeWriter,
proto_file: str,
msg_desc: descriptor.DescriptorProto,
parent_path: Optional[str] = None,
parent_path: str | None = None,
) -> None:
"""
Generate a complete dataclass for a protobuf message.
Expand Down Expand Up @@ -962,7 +962,7 @@ def _resolve_enum_type(self, type_name: str, proto_file: str) -> str:
@classmethod
def _get_field_default(
cls, field_desc: descriptor.FieldDescriptorProto
) -> Optional[str]:
) -> str | None:
"""
Get the default value for a field.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,14 @@ def to_json(self) -> str:
return json.dumps(self.to_dict())

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


def encode_hex(value: typing.Optional[bytes]) -> str:
def encode_hex(value: bytes | None) -> str:
"""
Encode bytes as hex string.

Expand All @@ -68,7 +68,7 @@ def encode_hex(value: typing.Optional[bytes]) -> str:
return value.hex() if value else ""


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


def encode_float(value: float) -> typing.Union[float, str]:
def encode_float(value: float) -> float | str:
"""
Encode float/double values.

Expand All @@ -111,7 +111,7 @@ def encode_float(value: float) -> typing.Union[float, str]:


def encode_repeated(
values: typing.Optional[list[T]],
values: list[T] | None,
map_fn: typing.Callable[[T], typing.Any],
) -> list[typing.Any]:
"""
Expand All @@ -126,7 +126,7 @@ def encode_repeated(
return [map_fn(v) for v in values] if values else []


def decode_hex(value: typing.Optional[str], field_name: str) -> bytes:
def decode_hex(value: str | None, field_name: str) -> bytes:
"""
Decode hex string to bytes.

Expand All @@ -147,7 +147,7 @@ def decode_hex(value: typing.Optional[str], field_name: str) -> bytes:
) from None


def decode_base64(value: typing.Optional[str], field_name: str) -> bytes:
def decode_base64(value: str | None, field_name: str) -> bytes:
"""
Decode base64 string to bytes.

Expand All @@ -168,9 +168,7 @@ def decode_base64(value: typing.Optional[str], field_name: str) -> bytes:
) from None


def decode_int64(
value: typing.Optional[typing.Union[int, str]], field_name: str
) -> int:
def decode_int64(value: int | str | None, field_name: str) -> int:
"""
Parse int64 from number or string.

Expand All @@ -191,9 +189,7 @@ def decode_int64(
) from None


def decode_float(
value: typing.Optional[typing.Union[float, int, str]], field_name: str
) -> float:
def decode_float(value: float | int | str | None, field_name: str) -> float:
"""
Parse float/double from number or string, handling special values.

Expand Down Expand Up @@ -221,7 +217,7 @@ def decode_float(


def decode_repeated(
values: typing.Optional[list[typing.Any]],
values: list[typing.Any] | None,
item_parser: typing.Callable[[typing.Any], T],
field_name: str,
) -> list[T]:
Expand All @@ -243,7 +239,7 @@ def decode_repeated(

def validate_type(
value: typing.Any,
expected_types: typing.Union[type, tuple[type, ...]],
expected_types: type | tuple[type, ...],
field_name: str,
) -> None:
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@

from __future__ import annotations

from collections.abc import Iterable
from collections.abc import Generator, Iterable
from contextlib import contextmanager
from typing import Any, Generator, Optional, Union
from typing import Any


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

def comment(self, content: Union[str, Iterable[str]]) -> CodeWriter:
def comment(self, content: str | Iterable[str]) -> CodeWriter:
"""
Writes a comment line or block. If content is a string, it writes a single comment line.

Expand All @@ -88,7 +88,7 @@ def comment(self, content: Union[str, Iterable[str]]) -> CodeWriter:
self.writeln(f"# {line}" if line else "#")
return self

def docstring(self, content: Union[str, Iterable[str]]) -> CodeWriter:
def docstring(self, content: str | Iterable[str]) -> CodeWriter:
"""
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.

Expand Down Expand Up @@ -139,8 +139,8 @@ def block(self, header: str) -> Generator[CodeWriter, None, None]:
def class_(
self,
name: str,
bases: Optional[Iterable[str]] = None,
decorators: Optional[Iterable[str]] = None,
bases: Iterable[str] | None = None,
decorators: Iterable[str] | None = None,
) -> Generator[CodeWriter, None, None]:
"""
Generate a class definition with optional base classes and decorators.
Expand All @@ -164,8 +164,8 @@ def class_(
def dataclass(
self,
name: str,
bases: Optional[Iterable[str]] = None,
decorators: Optional[Iterable[str]] = None,
bases: Iterable[str] | None = None,
decorators: Iterable[str] | None = None,
frozen: bool = False,
slots: bool = False,
decorator_name: str = "dataclasses.dataclass",
Expand Down Expand Up @@ -212,8 +212,8 @@ def enum(
self,
name: str,
enum_type: str = "enum.Enum",
bases: Optional[Iterable[str]] = None,
decorators: Optional[Iterable[str]] = None,
bases: Iterable[str] | None = None,
decorators: Iterable[str] | None = None,
) -> Generator[CodeWriter, None, None]:
"""
Generate an enum definition with optional base classes and decorators.
Expand Down Expand Up @@ -243,7 +243,7 @@ def field(
name: str,
type_hint: str,
default: Any = None,
default_factory: Optional[str] = None,
default_factory: str | None = None,
) -> CodeWriter:
"""
Write a dataclass field with optional default value or default factory.
Expand Down Expand Up @@ -279,9 +279,9 @@ def enum_member(self, name: str, value: Any) -> CodeWriter:
def function(
self,
name: str,
params: Union[Iterable[str], str],
decorators: Optional[Iterable[str]] = None,
return_type: Optional[str] = None,
params: Iterable[str] | str,
decorators: Iterable[str] | None = None,
return_type: str | None = None,
) -> Generator[CodeWriter, None, None]:
"""
Create a function definition with optional decorators and return type.
Expand All @@ -307,9 +307,9 @@ def function(
def method(
self,
name: str,
params: Union[Iterable[str], str],
decorators: Optional[Iterable[str]] = None,
return_type: Optional[str] = None,
params: Iterable[str] | str,
decorators: Iterable[str] | None = None,
return_type: str | None = None,
) -> Generator[CodeWriter, None, None]:
"""
Create a method definition within a class with optional decorators and return type.
Expand Down Expand Up @@ -386,7 +386,7 @@ def while_(self, condition: str) -> Generator[CodeWriter, None, None]:
yield self

def assignment(
self, var: str, value: str, type_hint: Optional[str] = None
self, var: str, value: str, type_hint: str | None = None
) -> CodeWriter:
"""
Write a variable assignment with optional type hint
Expand All @@ -404,7 +404,7 @@ def assignment(
self.writeln(f"{var} = {value}")
return self

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

Expand Down
15 changes: 7 additions & 8 deletions codegen/opentelemetry-codegen-json/tests/test_json_codec.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
# limitations under the License.

import math
from typing import Optional, Union

import pytest # type: ignore

Expand All @@ -40,7 +39,7 @@
(None, ""),
],
)
def test_encode_hex(value: Optional[bytes], expected: str) -> None:
def test_encode_hex(value: bytes | None, expected: str) -> None:
assert encode_hex(value) == expected


Expand All @@ -52,7 +51,7 @@ def test_encode_hex(value: Optional[bytes], expected: str) -> None:
(None, b""),
],
)
def test_decode_hex(value: Optional[str], expected: bytes) -> None:
def test_decode_hex(value: str | None, expected: bytes) -> None:
assert decode_hex(value, "field") == expected


Expand All @@ -71,7 +70,7 @@ def test_decode_hex_errors() -> None:
(None, ""),
],
)
def test_encode_base64(value: Optional[bytes], expected: str) -> None:
def test_encode_base64(value: bytes | None, expected: str) -> None:
assert encode_base64(value) == expected


Expand All @@ -83,7 +82,7 @@ def test_encode_base64(value: Optional[bytes], expected: str) -> None:
(None, b""),
],
)
def test_decode_base64(value: Optional[str], expected: bytes) -> None:
def test_decode_base64(value: str | None, expected: bytes) -> None:
assert decode_base64(value, "field") == expected


Expand Down Expand Up @@ -112,7 +111,7 @@ def test_encode_int64(value: int, expected: str) -> None:
(None, 0),
],
)
def test_decode_int64(value: Optional[Union[int, str]], expected: int) -> None:
def test_decode_int64(value: int | str | None, expected: int) -> None:
assert decode_int64(value, "field") == expected


Expand All @@ -132,7 +131,7 @@ def test_decode_int64_errors() -> None:
(float("-inf"), "-Infinity"),
],
)
def test_encode_float(value: float, expected: Union[float, str]) -> None:
def test_encode_float(value: float, expected: float | str) -> None:
result = encode_float(value)
if isinstance(expected, float) and math.isnan(expected):
assert math.isnan(result) # type: ignore
Expand All @@ -153,7 +152,7 @@ def test_encode_float(value: float, expected: Union[float, str]) -> None:
],
)
def test_decode_float(
value: Optional[Union[float, int, str]], expected: float
value: float | int | str | None, expected: float
) -> None:
result = decode_float(value, "field")
if math.isnan(expected):
Expand Down
Loading
Loading