Skip to content

Commit 8c782d9

Browse files
authored
Union pipe (#113)
* replace all Union types to a pipe operator now that we are python 3.10+ * replace Optional with pipe or None * minor fix ellipsis comment
1 parent 5123463 commit 8c782d9

File tree

24 files changed

+501
-505
lines changed

24 files changed

+501
-505
lines changed

src/lmnr/cli.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import os
55
import re
66
import sys
7-
from typing import Optional
87

98
from lmnr.sdk.evaluations import Evaluation
109

@@ -55,7 +54,7 @@ async def run_evaluation(args):
5554
sys.modules[name] = mod
5655

5756
spec.loader.exec_module(mod)
58-
evaluations: Optional[list[Evaluation]] = EVALUATION_INSTANCES.get()
57+
evaluations: list[Evaluation] | None = EVALUATION_INSTANCES.get()
5958
if evaluations is None:
6059
LOG.warning("Evaluation instance not found")
6160
if args.fail_on_error:

src/lmnr/opentelemetry_lib/__init__.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import logging
22
import sys
33

4-
from typing import Optional, Set
54
from opentelemetry.sdk.trace.export import SpanExporter
65
from opentelemetry.sdk.resources import SERVICE_NAME
76

@@ -16,17 +15,17 @@ class TracerManager:
1615

1716
@staticmethod
1817
def init(
19-
app_name: Optional[str] = sys.argv[0],
18+
app_name: str | None = sys.argv[0],
2019
disable_batch=False,
21-
exporter: Optional[SpanExporter] = None,
20+
exporter: SpanExporter | None = None,
2221
resource_attributes: dict = {},
23-
instruments: Optional[Set[Instruments]] = None,
24-
block_instruments: Optional[Set[Instruments]] = None,
22+
instruments: set[Instruments] | None = None,
23+
block_instruments: set[Instruments] | None = None,
2524
base_url: str = "https://api.lmnr.ai",
2625
port: int = 8443,
2726
http_port: int = 443,
28-
project_api_key: Optional[str] = None,
29-
max_export_batch_size: Optional[int] = None,
27+
project_api_key: str | None = None,
28+
max_export_batch_size: int | None = None,
3029
force_http: bool = False,
3130
timeout_seconds: int = 30,
3231
set_global_tracer_provider: bool = True,

src/lmnr/opentelemetry_lib/decorators/__init__.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import logging
44
import pydantic
55
import types
6-
from typing import Any, Literal, Optional, Union
6+
from typing import Any, Literal
77

88
from opentelemetry import trace
99
from opentelemetry import context as context_api
@@ -41,12 +41,12 @@ def json_dumps(data: dict) -> str:
4141

4242

4343
def entity_method(
44-
name: Optional[str] = None,
44+
name: str | None = None,
4545
ignore_input: bool = False,
46-
ignore_inputs: Optional[list[str]] = None,
46+
ignore_inputs: list[str] | None = None,
4747
ignore_output: bool = False,
48-
span_type: Union[Literal["DEFAULT"], Literal["LLM"], Literal["TOOL"]] = "DEFAULT",
49-
association_properties: Optional[dict[str, Any]] = None,
48+
span_type: Literal["DEFAULT", "LLM", "TOOL"] = "DEFAULT",
49+
association_properties: dict[str, Any] | None = None,
5050
):
5151
def decorate(fn):
5252
@wraps(fn)
@@ -130,12 +130,12 @@ def wrap(*args, **kwargs):
130130

131131
# Async Decorators
132132
def aentity_method(
133-
name: Optional[str] = None,
133+
name: str | None = None,
134134
ignore_input: bool = False,
135-
ignore_inputs: Optional[list[str]] = None,
135+
ignore_inputs: list[str] | None = None,
136136
ignore_output: bool = False,
137-
span_type: Union[Literal["DEFAULT"], Literal["LLM"], Literal["TOOL"]] = "DEFAULT",
138-
association_properties: Optional[dict[str, Any]] = None,
137+
span_type: Literal["DEFAULT", "LLM", "TOOL"] = "DEFAULT",
138+
association_properties: dict[str, Any] | None = None,
139139
):
140140
def decorate(fn):
141141
@wraps(fn)

src/lmnr/opentelemetry_lib/opentelemetry/instrumentation/google_genai/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from collections import defaultdict
44
import logging
55
import os
6-
from typing import AsyncGenerator, Callable, Collection, Generator, Optional
6+
from typing import AsyncGenerator, Callable, Collection, Generator
77

88
from google.genai import types
99

@@ -152,7 +152,7 @@ def _set_request_attributes(span, args, kwargs):
152152

153153
if should_send_prompts():
154154
i = 0
155-
system_instruction: Optional[types.ContentUnion] = config_dict.get(
155+
system_instruction: types.ContentUnion | None = config_dict.get(
156156
"system_instruction"
157157
)
158158
if system_instruction:
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
from typing import Callable, Coroutine, Optional
1+
from typing import Callable, Coroutine
22

33

44
class Config:
55
exception_logger = None
6-
upload_base64_image: Optional[
7-
Callable[[str, str, str, str], Coroutine[None, None, str]]
8-
] = None
6+
upload_base64_image: (
7+
Callable[[str, str, str, str], Coroutine[None, None, str]] | None
8+
) = None
99
convert_image_to_openai_format: bool = True

src/lmnr/opentelemetry_lib/opentelemetry/instrumentation/google_genai/utils.py

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from google.genai._common import BaseModel
1010
import pydantic
1111
from opentelemetry.trace import Span
12-
from typing import Any, Optional, Union
12+
from typing import Any
1313

1414

1515
def set_span_attribute(span: Span, name: str, value: str):
@@ -44,7 +44,7 @@ def wrapper(*args, **kwargs):
4444
return wrapper
4545

4646

47-
def to_dict(obj: Union[BaseModel, pydantic.BaseModel, dict]) -> dict[str, Any]:
47+
def to_dict(obj: BaseModel | pydantic.BaseModel | dict) -> dict[str, Any]:
4848
try:
4949
if isinstance(obj, BaseModel):
5050
return obj.model_dump()
@@ -59,11 +59,11 @@ def to_dict(obj: Union[BaseModel, pydantic.BaseModel, dict]) -> dict[str, Any]:
5959

6060

6161
def process_content_union(
62-
content: Union[types.ContentUnion, types.ContentUnionDict],
63-
trace_id: Optional[str] = None,
64-
span_id: Optional[str] = None,
62+
content: types.ContentUnion | types.ContentUnionDict,
63+
trace_id: str | None = None,
64+
span_id: str | None = None,
6565
message_index: int = 0,
66-
) -> Optional[str]:
66+
) -> str | None:
6767
parts = _process_content_union(content, trace_id, span_id, message_index)
6868
if parts is None:
6969
return None
@@ -83,11 +83,11 @@ def process_content_union(
8383

8484

8585
def _process_content_union(
86-
content: Union[types.ContentUnion, types.ContentUnionDict],
87-
trace_id: Optional[str] = None,
88-
span_id: Optional[str] = None,
86+
content: types.ContentUnion | types.ContentUnionDict,
87+
trace_id: str | None = None,
88+
span_id: str | None = None,
8989
message_index: int = 0,
90-
) -> Union[str, list[str], None]:
90+
) -> str | list[str] | None:
9191
if isinstance(content, types.Content):
9292
parts = to_dict(content).get("parts", [])
9393
return [_process_part(part) for part in parts]
@@ -111,12 +111,12 @@ def _process_content_union(
111111

112112

113113
def _process_part_union(
114-
content: Union[types.PartDict, types.File, types.Part, str],
115-
trace_id: Optional[str] = None,
116-
span_id: Optional[str] = None,
114+
content: types.PartDict | types.File | types.Part | str,
115+
trace_id: str | None = None,
116+
span_id: str | None = None,
117117
message_index: int = 0,
118118
content_index: int = 0,
119-
) -> Optional[str]:
119+
) -> str | None:
120120
if isinstance(content, str):
121121
return content
122122
elif isinstance(content, types.File):
@@ -135,11 +135,11 @@ def _process_part_union(
135135

136136
def _process_part(
137137
content: types.Part,
138-
trace_id: Optional[str] = None,
139-
span_id: Optional[str] = None,
138+
trace_id: str | None = None,
139+
span_id: str | None = None,
140140
message_index: int = 0,
141141
content_index: int = 0,
142-
) -> Optional[str]:
142+
) -> str | None:
143143
part_dict = to_dict(content)
144144
if part_dict.get("text") is not None:
145145
return part_dict.get("text")
@@ -157,8 +157,8 @@ def _process_part(
157157

158158

159159
def role_from_content_union(
160-
content: Union[types.ContentUnion, types.ContentUnionDict],
161-
) -> Optional[str]:
160+
content: types.ContentUnion | types.ContentUnionDict,
161+
) -> str | None:
162162
if isinstance(content, types.Content):
163163
return to_dict(content).get("role")
164164
elif isinstance(content, list) and len(content) > 0:

src/lmnr/opentelemetry_lib/tracing/__init__.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
from opentelemetry.sdk.resources import Resource
1616
from opentelemetry.sdk.trace import TracerProvider, SpanProcessor
1717
from opentelemetry.sdk.trace.export import SpanExporter
18-
from typing import Optional, Set
1918

2019
module_logger = logging.getLogger(__name__)
2120
console_log_handler = logging.StreamHandler()
@@ -31,7 +30,7 @@
3130
class TracerWrapper(object):
3231
resource_attributes: dict = {}
3332
enable_content_tracing: bool = True
34-
__tracer_provider: Optional[TracerProvider] = None
33+
__tracer_provider: TracerProvider | None = None
3534
__logger: logging.Logger
3635
__client: LaminarClient
3736
__async_client: AsyncLaminarClient
@@ -41,14 +40,14 @@ class TracerWrapper(object):
4140
def __new__(
4241
cls,
4342
disable_batch=False,
44-
exporter: Optional[SpanExporter] = None,
45-
instruments: Optional[Set[Instruments]] = None,
46-
block_instruments: Optional[Set[Instruments]] = None,
43+
exporter: SpanExporter | None = None,
44+
instruments: set[Instruments] | None = None,
45+
block_instruments: set[Instruments] | None = None,
4746
base_url: str = "https://api.lmnr.ai",
4847
port: int = 8443,
4948
http_port: int = 443,
50-
project_api_key: Optional[str] = None,
51-
max_export_batch_size: Optional[int] = None,
49+
project_api_key: str | None = None,
50+
max_export_batch_size: int | None = None,
5251
force_http: bool = False,
5352
timeout_seconds: int = 10,
5453
set_global_tracer_provider: bool = True,

0 commit comments

Comments
 (0)