Skip to content

Commit

Permalink
Add check_for_missing_imports
Browse files Browse the repository at this point in the history
  • Loading branch information
kumaranvpl committed Jan 17, 2025
1 parent 149a361 commit c20b56f
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
18 changes: 18 additions & 0 deletions autogen/handle_imports.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Copyright (c) 2023 - 2024, Owners of https://github.com/ag2ai
#
# SPDX-License-Identifier: Apache-2.0

from contextlib import contextmanager
from typing import Generator


@contextmanager
def check_for_missing_imports() -> Generator[None, None, None]:
"""
A context manager to temporarily suppress ImportErrors.
Use this to attempt imports without failing immediately on missing modules.
"""
try:
yield
except ImportError:
pass # Ignore ImportErrors during this context
11 changes: 11 additions & 0 deletions test/test_handle_imports.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Copyright (c) 2023 - 2024, Owners of https://github.com/ag2ai
#
# SPDX-License-Identifier: Apache-2.0

from autogen.handle_imports import check_for_missing_imports


def test_check_for_missing_imports():
with check_for_missing_imports():
pass # Safe to attempt, even if it fails
assert True

0 comments on commit c20b56f

Please sign in to comment.