Skip to content

Commit de31889

Browse files
mishushakovclaude
andcommitted
Extract RunCodeLanguage type to deduplicate language unions
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent d1bdaed commit de31889

File tree

6 files changed

+33
-43
lines changed

6 files changed

+33
-43
lines changed

js/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
export * from 'e2b'
22

33
export { Sandbox } from './sandbox'
4-
export type { Context, RunCodeOpts, CreateCodeContextOpts } from './sandbox'
4+
export type { Context, RunCodeLanguage, RunCodeOpts, CreateCodeContextOpts } from './sandbox'
55
export type {
66
Logs,
77
ExecutionError,

js/src/sandbox.ts

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,20 @@ export type Context = {
3333
cwd: string
3434
}
3535

36+
/* eslint-disable @typescript-eslint/ban-types */
37+
/**
38+
* Supported language for code execution.
39+
*/
40+
export type RunCodeLanguage =
41+
| 'python'
42+
| 'javascript'
43+
| 'typescript'
44+
| 'r'
45+
| 'java'
46+
| 'bash'
47+
| (string & {})
48+
/* eslint-enable @typescript-eslint/ban-types */
49+
3650
/**
3751
* Options for running code.
3852
*/
@@ -88,16 +102,7 @@ export interface CreateCodeContextOpts {
88102
*
89103
* @default python
90104
*/
91-
/* eslint-disable @typescript-eslint/ban-types */
92-
language?:
93-
| 'python'
94-
| 'javascript'
95-
| 'typescript'
96-
| 'r'
97-
| 'java'
98-
| 'bash'
99-
| (string & {})
100-
/* eslint-enable @typescript-eslint/ban-types */
105+
language?: RunCodeLanguage
101106
/**
102107
* Timeout for the request in **milliseconds**.
103108
*
@@ -158,16 +163,7 @@ export class Sandbox extends BaseSandbox {
158163
*
159164
* If not defined, the default Python context is used.
160165
*/
161-
/* eslint-disable @typescript-eslint/ban-types */
162-
language?:
163-
| 'python'
164-
| 'javascript'
165-
| 'typescript'
166-
| 'r'
167-
| 'java'
168-
| 'bash'
169-
| (string & {})
170-
/* eslint-enable @typescript-eslint/ban-types */
166+
language?: RunCodeLanguage
171167
}
172168
): Promise<Execution>
173169
/**

python/e2b_code_interpreter/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@
1010
Logs,
1111
OutputHandler,
1212
OutputMessage,
13+
RunCodeLanguage,
1314
)

python/e2b_code_interpreter/code_interpreter_async.py

Lines changed: 4 additions & 11 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, Literal, List
4+
from typing import Optional, Dict, overload, Union, List
55
from httpx import AsyncClient
66

77
from e2b import (
@@ -18,6 +18,7 @@
1818
Execution,
1919
ExecutionError,
2020
Context,
21+
RunCodeLanguage,
2122
Result,
2223
aextract_exception,
2324
OutputHandlerWithAsync,
@@ -68,11 +69,7 @@ def _client(self) -> AsyncClient:
6869
async def run_code(
6970
self,
7071
code: str,
71-
language: Union[
72-
Literal["python", "javascript", "typescript", "r", "java", "bash"],
73-
str,
74-
None,
75-
] = None,
72+
language: RunCodeLanguage = None,
7673
on_stdout: Optional[OutputHandlerWithAsync[OutputMessage]] = None,
7774
on_stderr: Optional[OutputHandlerWithAsync[OutputMessage]] = None,
7875
on_result: Optional[OutputHandlerWithAsync[Result]] = None,
@@ -206,11 +203,7 @@ async def run_code(
206203
async def create_code_context(
207204
self,
208205
cwd: Optional[str] = None,
209-
language: Union[
210-
Literal["python", "javascript", "typescript", "r", "java", "bash"],
211-
str,
212-
None,
213-
] = None,
206+
language: RunCodeLanguage = None,
214207
request_timeout: Optional[float] = None,
215208
) -> Context:
216209
"""

python/e2b_code_interpreter/code_interpreter_sync.py

Lines changed: 4 additions & 11 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, Literal, Union, List
4+
from typing import Optional, Dict, overload, Union, List
55
from httpx import Client
66
from e2b import Sandbox as BaseSandbox, InvalidArgumentException
77

@@ -13,6 +13,7 @@
1313
from e2b_code_interpreter.models import (
1414
ExecutionError,
1515
Execution,
16+
RunCodeLanguage,
1617
Context,
1718
Result,
1819
extract_exception,
@@ -65,11 +66,7 @@ def _client(self) -> Client:
6566
def run_code(
6667
self,
6768
code: str,
68-
language: Union[
69-
Literal["python", "javascript", "typescript", "r", "java", "bash"],
70-
str,
71-
None,
72-
] = None,
69+
language: RunCodeLanguage = None,
7370
on_stdout: Optional[OutputHandler[OutputMessage]] = None,
7471
on_stderr: Optional[OutputHandler[OutputMessage]] = None,
7572
on_result: Optional[OutputHandler[Result]] = None,
@@ -202,11 +199,7 @@ def run_code(
202199
def create_code_context(
203200
self,
204201
cwd: Optional[str] = None,
205-
language: Union[
206-
Literal["python", "javascript", "typescript", "r", "java", "bash"],
207-
str,
208-
None,
209-
] = None,
202+
language: RunCodeLanguage = None,
210203
request_timeout: Optional[float] = None,
211204
) -> Context:
212205
"""

python/e2b_code_interpreter/models.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from dataclasses import dataclass, field
77
from typing import (
88
List,
9+
Literal,
910
Optional,
1011
Iterable,
1112
Dict,
@@ -20,6 +21,12 @@
2021

2122
from .charts import Chart, _deserialize_chart
2223

24+
RunCodeLanguage = Union[
25+
Literal["python", "javascript", "typescript", "r", "java", "bash"],
26+
str,
27+
None,
28+
]
29+
2330
T = TypeVar("T")
2431
OutputHandler = Union[Callable[[T], Any],]
2532

0 commit comments

Comments
 (0)