Skip to content

Commit b55eae7

Browse files
fix: align Python SDK migration types
Co-Authored-By: mish@e2b.dev <mish@e2b.dev>
1 parent 7df1757 commit b55eae7

5 files changed

Lines changed: 31 additions & 17 deletions

File tree

.changeset/strong-mice-type.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@e2b/code-interpreter-python': patch
3+
---
4+
5+
Improve Python model typing and safely handle optional chart and sandbox URL values.

python/e2b_code_interpreter/charts.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -194,17 +194,19 @@ def __init__(self, **kwargs):
194194
class SuperChart(Chart):
195195
type = ChartType.SUPERCHART
196196

197-
elements: List[
198-
Union[LineChart, ScatterChart, BarChart, PieChart, BoxAndWhiskerChart]
199-
]
197+
elements: List[Chart]
200198

201199
def __init__(self, **kwargs):
202200
super().__init__(**kwargs)
203-
self.elements = [_deserialize_chart(g) for g in kwargs["elements"]]
201+
self.elements = []
202+
for raw_chart in kwargs["elements"]:
203+
chart = _deserialize_chart(raw_chart)
204+
if chart is not None:
205+
self.elements.append(chart)
204206

205207

206208
ChartTypes = Union[
207-
LineChart, ScatterChart, BarChart, PieChart, BoxAndWhiskerChart, SuperChart
209+
Chart, LineChart, ScatterChart, BarChart, PieChart, BoxAndWhiskerChart, SuperChart
208210
]
209211

210212

python/e2b_code_interpreter/code_interpreter_async.py

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

4-
from typing import Optional, Dict, overload, Union, List
4+
from typing import cast, Optional, Dict, overload, Union, List
55
from httpx import AsyncClient
66

77
from e2b import (
@@ -63,7 +63,7 @@ class AsyncSandbox(BaseAsyncSandbox):
6363
def _jupyter_url(self) -> str:
6464
# Honors the `sandbox_url` option and the `E2B_SANDBOX_URL` environment
6565
# variable, same as the base SDK does for envd requests.
66-
sandbox_url = self.connection_config._sandbox_url
66+
sandbox_url = cast(Optional[str], self.connection_config._sandbox_url)
6767
if sandbox_url:
6868
return sandbox_url
6969
return f"{'http' if self.connection_config.debug else 'https'}://{self.get_host(JUPYTER_PORT)}"

python/e2b_code_interpreter/code_interpreter_sync.py

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

4-
from typing import Optional, Dict, overload, Union, List
4+
from typing import cast, Optional, Dict, overload, Union, List
55
from httpx import Client
66
from e2b import Sandbox as BaseSandbox, InvalidArgumentException
77
from e2b.api.client_sync import get_transport
@@ -60,7 +60,7 @@ class Sandbox(BaseSandbox):
6060
def _jupyter_url(self) -> str:
6161
# Honors the `sandbox_url` option and the `E2B_SANDBOX_URL` environment
6262
# variable, same as the base SDK does for envd requests.
63-
sandbox_url = self.connection_config._sandbox_url
63+
sandbox_url = cast(Optional[str], self.connection_config._sandbox_url)
6464
if sandbox_url:
6565
return sandbox_url
6666
return f"{'http' if self.connection_config.debug else 'https'}://{self.get_host(JUPYTER_PORT)}"

python/e2b_code_interpreter/models.py

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ def formats(self) -> Iterable[str]:
207207

208208
return formats
209209

210-
def __str__(self) -> Optional[str]:
210+
def __str__(self) -> str:
211211
"""
212212
Returns the text representation of the data.
213213
@@ -305,7 +305,12 @@ class Logs:
305305
stderr: List[str] = field(default_factory=list)
306306
"""List of strings printed to stderr by prints, subprocesses, etc."""
307307

308-
def __init__(self, stdout: List[str] = None, stderr: List[str] = None, **kwargs):
308+
def __init__(
309+
self,
310+
stdout: Optional[List[str]] = None,
311+
stderr: Optional[List[str]] = None,
312+
**kwargs,
313+
):
309314
self.stdout = stdout or []
310315
self.stderr = stderr or []
311316

@@ -329,7 +334,9 @@ def serialize_results(results: List[Result]) -> List[Dict[str, str]]:
329334
serialized_dict = {}
330335
for key in result.formats():
331336
if key == "chart":
332-
serialized_dict[key] = result.chart.to_dict()
337+
chart = result.chart
338+
if chart is not None:
339+
serialized_dict[key] = chart.to_dict()
333340
else:
334341
serialized_dict[key] = result[key]
335342

@@ -356,8 +363,8 @@ class Execution:
356363

357364
def __init__(
358365
self,
359-
results: List[Result] = None,
360-
logs: Logs = None,
366+
results: Optional[List[Result]] = None,
367+
logs: Optional[Logs] = None,
361368
error: Optional[ExecutionError] = None,
362369
execution_count: Optional[int] = None,
363370
**kwargs,
@@ -510,7 +517,7 @@ def __init__(self, context_id: str, language: str, cwd: str, **kwargs):
510517
@classmethod
511518
def from_json(cls, data: Dict[str, str]):
512519
return cls(
513-
context_id=data.get("id"),
514-
language=data.get("language"),
515-
cwd=data.get("cwd"),
520+
context_id=data["id"],
521+
language=data["language"],
522+
cwd=data["cwd"],
516523
)

0 commit comments

Comments
 (0)