Skip to content

Commit

Permalink
Move third party non default module imports to optional block in auto…
Browse files Browse the repository at this point in the history
…gen/coding files
  • Loading branch information
kumaranvpl committed Jan 21, 2025
1 parent b03a178 commit a60ea7c
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 18 deletions.
4 changes: 1 addition & 3 deletions autogen/coding/jupyter/docker_jupyter_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,12 @@

import docker

from ..docker_commandline_code_executor import _wait_for_ready

if sys.version_info >= (3, 11):
from typing import Self
else:
from typing_extensions import Self


from ..docker_commandline_code_executor import _wait_for_ready
from .base import JupyterConnectable, JupyterConnectionInfo
from .jupyter_client import JupyterClient

Expand Down
8 changes: 6 additions & 2 deletions autogen/coding/jupyter/embedded_ipython_code_executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,20 @@
from queue import Empty
from typing import Any

from jupyter_client import KernelManager # type: ignore[attr-defined]
from jupyter_client.kernelspec import KernelSpecManager
from pydantic import BaseModel, Field, field_validator

from ...import_utils import optional_import_block, require_optional_import
from ..base import CodeBlock, CodeExtractor, IPythonCodeResult
from ..markdown_code_extractor import MarkdownCodeExtractor

with optional_import_block():
from jupyter_client import KernelManager # type: ignore[attr-defined]
from jupyter_client.kernelspec import KernelSpecManager

__all__ = "EmbeddedIPythonCodeExecutor"


@require_optional_import("jupyter_client", "jupyter-executor")
class EmbeddedIPythonCodeExecutor(BaseModel):
"""(Experimental) A code executor class that executes code statefully using an embedded
IPython kernel managed by this class.
Expand Down
17 changes: 11 additions & 6 deletions autogen/coding/jupyter/jupyter_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@
# SPDX-License-Identifier: MIT
from __future__ import annotations

import datetime
import json
import sys
import uuid
from dataclasses import dataclass
from types import TracebackType
from typing import Any, cast
Expand All @@ -16,17 +19,17 @@
else:
from typing_extensions import Self

import datetime
import json
import uuid

import requests
import websocket
from requests.adapters import HTTPAdapter, Retry
from websocket import WebSocket

from ...import_utils import optional_import_block, require_optional_import
from .base import JupyterConnectionInfo

with optional_import_block():
import websocket
from websocket import WebSocket


class JupyterClient:
def __init__(self, connection_info: JupyterConnectionInfo):
Expand Down Expand Up @@ -90,12 +93,14 @@ def restart_kernel(self, kernel_id: str) -> None:
)
response.raise_for_status()

@require_optional_import("websocket", "jupyter-executor")
def get_kernel_client(self, kernel_id: str) -> JupyterKernelClient:
ws_url = f"{self._get_ws_base_url()}/api/kernels/{kernel_id}/channels"
ws = websocket.create_connection(ws_url, header=self._get_headers())
return JupyterKernelClient(ws)


@require_optional_import("websocket", "jupyter-executor")
class JupyterKernelClient:
"""(Experimental) A client for communicating with a Jupyter kernel."""

Expand All @@ -110,7 +115,7 @@ class DataItem:
output: str
data_items: list[DataItem]

def __init__(self, websocket: WebSocket):
def __init__(self, websocket: "WebSocket"):
self._session_id: str = uuid.uuid4().hex
self._websocket: WebSocket = websocket

Expand Down
4 changes: 1 addition & 3 deletions autogen/coding/jupyter/jupyter_code_executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,14 @@
from types import TracebackType
from typing import Optional, Union

from autogen.coding.utils import silence_pip

if sys.version_info >= (3, 11):
from typing import Self
else:
from typing_extensions import Self


from ..base import CodeBlock, CodeExecutor, CodeExtractor, IPythonCodeResult
from ..markdown_code_extractor import MarkdownCodeExtractor
from ..utils import silence_pip
from .base import JupyterConnectable, JupyterConnectionInfo
from .jupyter_client import JupyterClient

Expand Down
7 changes: 3 additions & 4 deletions autogen/coding/local_commandline_code_executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,14 @@

from typing_extensions import ParamSpec

from autogen.coding.func_with_reqs import (
from ..code_utils import PYTHON_VARIANTS, TIMEOUT_MSG, WIN32, _cmd
from .base import CodeBlock, CodeExecutor, CodeExtractor, CommandLineCodeResult
from .func_with_reqs import (
FunctionWithRequirements,
FunctionWithRequirementsStr,
_build_python_functions_file,
to_stub,
)

from ..code_utils import PYTHON_VARIANTS, TIMEOUT_MSG, WIN32, _cmd
from .base import CodeBlock, CodeExecutor, CodeExtractor, CommandLineCodeResult
from .markdown_code_extractor import MarkdownCodeExtractor
from .utils import _get_file_name_from_content, silence_pip

Expand Down

0 comments on commit a60ea7c

Please sign in to comment.