Skip to content

Commit 065320a

Browse files
committed
chore: add python type checking
1 parent 98d2c8c commit 065320a

File tree

15 files changed

+219
-132
lines changed

15 files changed

+219
-132
lines changed

packages/python-sdk/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ init:
2525

2626
lint:
2727
ruff check .
28+
pyright
2829

2930
format:
3031
ruff format .

packages/python-sdk/e2b/__init__.py

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -31,41 +31,39 @@
3131
)
3232
from .connection_config import (
3333
ConnectionConfig,
34-
ProxyTypes,
3534
)
3635
from .exceptions import (
37-
SandboxException,
38-
TimeoutException,
39-
NotFoundException,
4036
AuthenticationException,
37+
BuildException,
38+
FileUploadException,
4139
InvalidArgumentException,
4240
NotEnoughSpaceException,
41+
NotFoundException,
42+
SandboxException,
4343
TemplateException,
44-
BuildException,
45-
FileUploadException,
44+
TimeoutException,
4645
)
4746
from .sandbox.commands.command_handle import (
48-
CommandResult,
49-
Stderr,
50-
Stdout,
5147
CommandExitException,
48+
CommandResult,
5249
PtyOutput,
5350
PtySize,
51+
Stderr,
52+
Stdout,
5453
)
5554
from .sandbox.commands.main import ProcessInfo
56-
from .sandbox.filesystem.filesystem import EntryInfo, WriteInfo, FileType
55+
from .sandbox.filesystem.filesystem import EntryInfo, FileType, WriteInfo
5756
from .sandbox.filesystem.watch_handle import (
5857
FilesystemEvent,
5958
FilesystemEventType,
6059
)
6160
from .sandbox.sandbox_api import (
62-
SandboxInfo,
63-
SandboxQuery,
64-
SandboxState,
65-
SandboxMetrics,
66-
McpServer,
6761
GitHubMcpServer,
6862
GitHubMcpServerConfig,
63+
McpServer,
64+
SandboxInfo,
65+
SandboxMetrics,
66+
SandboxQuery,
6967
)
7068
from .sandbox_async.commands.command_handle import AsyncCommandHandle
7169
from .sandbox_async.filesystem.watch_handle import AsyncWatchHandle
@@ -78,21 +76,21 @@
7876
from .sandbox_sync.paginator import SandboxPaginator
7977
from .template.logger import (
8078
LogEntry,
79+
LogEntryEnd,
8180
LogEntryLevel,
8281
LogEntryStart,
83-
LogEntryEnd,
8482
default_build_logger,
8583
)
8684
from .template.main import TemplateBase, TemplateClass
8785
from .template.readycmd import (
8886
ReadyCmd,
8987
wait_for_file,
90-
wait_for_url,
9188
wait_for_port,
9289
wait_for_process,
9390
wait_for_timeout,
91+
wait_for_url,
9492
)
95-
from .template.types import CopyItem, BuildInfo
93+
from .template.types import BuildInfo, CopyItem
9694
from .template_async.main import AsyncTemplate
9795
from .template_sync.main import Template
9896

@@ -102,7 +100,6 @@
102100
"client",
103101
# Connection config
104102
"ConnectionConfig",
105-
"ProxyTypes",
106103
# Exceptions
107104
"SandboxException",
108105
"TimeoutException",
@@ -118,7 +115,6 @@
118115
"SandboxMetrics",
119116
"ProcessInfo",
120117
"SandboxQuery",
121-
"SandboxState",
122118
"SandboxMetrics",
123119
# Command handle
124120
"CommandResult",

packages/python-sdk/e2b/api/__init__.py

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
1-
import os
2-
from types import TracebackType
31
import json
42
import logging
5-
from typing import Optional, Union
6-
from httpx import Limits, BaseTransport, AsyncBaseTransport
3+
import os
74
from dataclasses import dataclass
5+
from types import TracebackType
6+
from typing import Any, Optional, Union
7+
8+
from httpx import AsyncBaseTransport, BaseTransport, Limits
89

910
from e2b.api.client.client import AuthenticatedClient
10-
from e2b.connection_config import ConnectionConfig
11+
from e2b.api.client.types import Response
1112
from e2b.api.metadata import default_headers
13+
from e2b.connection_config import ConnectionConfig
1214
from e2b.exceptions import (
1315
AuthenticationException,
14-
SandboxException,
1516
RateLimitException,
17+
SandboxException,
1618
)
17-
from e2b.api.client.types import Response
1819

1920
logger = logging.getLogger(__name__)
2021

@@ -34,7 +35,7 @@ class SandboxCreateResponse:
3435

3536

3637
def handle_api_exception(
37-
e: Response,
38+
e: Response[Any],
3839
default_exception_class: type[Exception] = SandboxException,
3940
stack_trace: Optional[TracebackType] = None,
4041
):
@@ -106,6 +107,11 @@ def __init__(
106107
)
107108
token = config.access_token
108109

110+
if token is None:
111+
raise AuthenticationException(
112+
"Authentication token is required but not provided.",
113+
)
114+
109115
auth_header_name = "X-API-KEY" if require_api_key else "Authorization"
110116
prefix = "" if require_api_key else "Bearer"
111117

@@ -115,7 +121,7 @@ def __init__(
115121
}
116122

117123
# Prevent passing these parameters twice
118-
more_headers: Optional[dict] = kwargs.pop("headers", None)
124+
more_headers: Optional[dict[str, str]] = kwargs.pop("headers", None)
119125
if more_headers:
120126
headers.update(more_headers)
121127
kwargs.pop("token", None)
@@ -143,7 +149,7 @@ def __init__(
143149
def _log_request(self, request):
144150
logger.info(f"Request {request.method} {request.url}")
145151

146-
def _log_response(self, response: Response):
152+
def _log_response(self, response: Response[Any]):
147153
if response.status_code >= 400:
148154
logger.error(f"Response {response.status_code}")
149155
else:
@@ -155,7 +161,7 @@ class AsyncApiClient(ApiClient):
155161
async def _log_request(self, request):
156162
logger.info(f"Request {request.method} {request.url}")
157163

158-
async def _log_response(self, response: Response):
164+
async def _log_response(self, response: Response[Any]):
159165
if response.status_code >= 400:
160166
logger.error(f"Response {response.status_code}")
161167
else:

packages/python-sdk/e2b/connection_config.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import os
2-
3-
from typing import Optional, Dict, TypedDict
2+
from typing import Any, Dict, Optional, TypedDict
43

54
from httpx._types import ProxyTypes
65
from typing_extensions import Unpack
@@ -117,7 +116,7 @@ def __init__(
117116
or ("http://localhost:3000" if self.debug else f"https://api.{self.domain}")
118117
)
119118

120-
self._sandbox_url = sandbox_url or ConnectionConfig._sandbox_url()
119+
self.sandbox_url = sandbox_url or ConnectionConfig._sandbox_url()
121120

122121
@staticmethod
123122
def _get_request_timeout(
@@ -135,8 +134,9 @@ def get_request_timeout(self, request_timeout: Optional[float] = None):
135134
return self._get_request_timeout(self.request_timeout, request_timeout)
136135

137136
def get_sandbox_url(self, sandbox_id: str, sandbox_domain: str) -> str:
138-
if self._sandbox_url:
139-
return self._sandbox_url
137+
url = self._sandbox_url()
138+
if url is not None:
139+
return url
140140

141141
return f"{'http' if self.debug else 'https'}://{self.get_host(sandbox_id, sandbox_domain, self.envd_port)}"
142142

@@ -159,7 +159,7 @@ def get_host(self, sandbox_id: str, sandbox_domain: str, port: int) -> str:
159159
def get_api_params(
160160
self,
161161
**opts: Unpack[ApiParams],
162-
) -> dict:
162+
) -> dict[str, Any]:
163163
"""
164164
Get the parameters for the API call.
165165

packages/python-sdk/e2b/sandbox/main.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,19 @@
11
import urllib.parse
2-
from packaging.version import Version
3-
42
from typing import Optional, TypedDict
53

6-
from e2b.sandbox.signature import get_signature
4+
from packaging.version import Version
5+
76
from e2b.connection_config import ConnectionConfig, default_username
87
from e2b.envd.api import ENVD_API_FILES_ROUTE
98
from e2b.envd.versions import ENVD_DEFAULT_USER
9+
from e2b.sandbox.signature import get_signature
1010

1111

1212
class SandboxOpts(TypedDict):
1313
sandbox_id: str
1414
sandbox_domain: Optional[str]
1515
envd_version: Version
1616
envd_access_token: Optional[str]
17-
sandbox_url: Optional[str]
1817
connection_config: ConnectionConfig
1918

2019

@@ -66,7 +65,7 @@ def _envd_version(self) -> Version:
6665
return self.__envd_version
6766

6867
@property
69-
def sandbox_domain(self) -> Optional[str]:
68+
def sandbox_domain(self) -> str:
7069
return self.__sandbox_domain
7170

7271
@property

packages/python-sdk/e2b/sandbox_async/commands/command.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,22 @@
11
from typing import Dict, List, Literal, Optional, Union, overload
22

3-
import e2b_connect
43
import httpcore
54
from packaging.version import Version
5+
6+
import e2b_connect
67
from e2b.connection_config import (
7-
ConnectionConfig,
8-
Username,
98
KEEPALIVE_PING_HEADER,
109
KEEPALIVE_PING_INTERVAL_SEC,
10+
ConnectionConfig,
11+
Username,
12+
default_username,
1113
)
1214
from e2b.envd.process import process_connect, process_pb2
1315
from e2b.envd.rpc import authentication_header, handle_rpc_exception
14-
from e2b.envd.versions import ENVD_COMMANDS_STDIN
16+
from e2b.envd.versions import ENVD_COMMANDS_STDIN, ENVD_DEFAULT_USER
1517
from e2b.exceptions import SandboxException
16-
from e2b.sandbox.commands.main import ProcessInfo
1718
from e2b.sandbox.commands.command_handle import CommandResult
19+
from e2b.sandbox.commands.main import ProcessInfo
1820
from e2b.sandbox_async.commands.command_handle import AsyncCommandHandle, Stderr, Stdout
1921
from e2b.sandbox_async.utils import OutputHandler
2022

@@ -220,10 +222,14 @@ async def run(
220222
# Default to `False`
221223
stdin = stdin or False
222224

225+
username = user
226+
if username is None and self._envd_version < ENVD_DEFAULT_USER:
227+
username = default_username
228+
223229
proc = await self._start(
224230
cmd,
225231
envs,
226-
user,
232+
username,
227233
cwd,
228234
timeout,
229235
request_timeout,
@@ -238,7 +244,7 @@ async def _start(
238244
self,
239245
cmd: str,
240246
envs: Optional[Dict[str, str]],
241-
user: Username,
247+
user: Optional[Username],
242248
cwd: Optional[str],
243249
timeout: Optional[float],
244250
request_timeout: Optional[float],

packages/python-sdk/e2b/sandbox_async/main.py

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,26 @@
11
import datetime
2+
import json
23
import logging
3-
import httpx
44
import uuid
5-
import json
6-
7-
from typing import Dict, Optional, overload, List
5+
from typing import Dict, List, Optional, overload
86

7+
import httpx
98
from packaging.version import Version
10-
from typing_extensions import Unpack, Self
9+
from typing_extensions import Self, Unpack
1110

1211
from e2b.api.client.types import Unset
13-
from e2b.connection_config import ConnectionConfig, ApiParams
12+
from e2b.api.client_async import get_transport
13+
from e2b.connection_config import ApiParams, ConnectionConfig
1414
from e2b.envd.api import ENVD_API_HEALTH_ROUTE, ahandle_envd_api_exception
1515
from e2b.envd.versions import ENVD_DEBUG_FALLBACK
16-
from e2b.exceptions import format_request_timeout_error, SandboxException
16+
from e2b.exceptions import SandboxException, format_request_timeout_error
1717
from e2b.sandbox.main import SandboxOpts
18-
from e2b.sandbox.sandbox_api import SandboxMetrics, McpServer
18+
from e2b.sandbox.sandbox_api import McpServer, SandboxMetrics
1919
from e2b.sandbox.utils import class_method_variant
20-
from e2b.sandbox_async.filesystem.filesystem import Filesystem
2120
from e2b.sandbox_async.commands.command import Commands
2221
from e2b.sandbox_async.commands.pty import Pty
22+
from e2b.sandbox_async.filesystem.filesystem import Filesystem
2323
from e2b.sandbox_async.sandbox_api import SandboxApi, SandboxInfo
24-
from e2b.api.client_async import get_transport
2524

2625
logger = logging.getLogger(__name__)
2726

@@ -659,9 +658,13 @@ async def _cls_connect(
659658

660659
return cls(
661660
sandbox_id=sandbox.sandbox_id,
662-
sandbox_domain=sandbox.domain,
661+
sandbox_domain=sandbox.domain
662+
if not isinstance(sandbox.domain, Unset)
663+
else None,
663664
envd_version=Version(sandbox.envd_version),
664-
envd_access_token=envd_access_token,
665+
envd_access_token=envd_access_token
666+
if not isinstance(envd_access_token, Unset)
667+
else None,
665668
connection_config=connection_config,
666669
)
667670

0 commit comments

Comments
 (0)