From 42709f55b27f52048939907b34f42cfdc3537ec0 Mon Sep 17 00:00:00 2001 From: H Lohaus Date: Sun, 14 Jan 2024 16:37:21 +0100 Subject: [PATCH] Fix unittests (#1472) * Fix unittests * Fix missing distutils, install setuptools * Fix version not found * Remove deprecation in get_event_loop --- .github/workflows/unittest.yml | 4 ++-- etc/unittest/main.py | 1 + g4f/Provider/helper.py | 14 ++++---------- g4f/gui/server/backend.py | 7 ++++++- requirements.txt | 3 ++- setup.py | 1 + 6 files changed, 16 insertions(+), 14 deletions(-) diff --git a/.github/workflows/unittest.yml b/.github/workflows/unittest.yml index 68ada22bc4..20d9e55f83 100644 --- a/.github/workflows/unittest.yml +++ b/.github/workflows/unittest.yml @@ -14,6 +14,6 @@ jobs: python-version: "3.x" cache: 'pip' - name: Install requirements - - run: pip install -r requirements.txt + run: pip install -r requirements.txt - name: Run tests - - run: python -m etc.unittest.main \ No newline at end of file + run: python -m etc.unittest.main \ No newline at end of file diff --git a/etc/unittest/main.py b/etc/unittest/main.py index 61f4ffda84..4b92a6a679 100644 --- a/etc/unittest/main.py +++ b/etc/unittest/main.py @@ -11,6 +11,7 @@ from g4f.base_provider import BaseProvider g4f.debug.logging = False +g4f.debug.version_check = False class MockProvider(BaseProvider): working = True diff --git a/g4f/Provider/helper.py b/g4f/Provider/helper.py index fce1ee6fff..160074f711 100644 --- a/g4f/Provider/helper.py +++ b/g4f/Provider/helper.py @@ -5,7 +5,7 @@ import random import secrets import string -from asyncio import AbstractEventLoop, BaseEventLoop +from asyncio import AbstractEventLoop from platformdirs import user_config_dir from browser_cookie3 import ( chrome, chromium, opera, opera_gx, @@ -27,19 +27,13 @@ def get_event_loop() -> AbstractEventLoop: AbstractEventLoop: The current or new event loop. """ try: - loop = asyncio.get_event_loop() - if isinstance(loop, BaseEventLoop): - loop._check_closed() - except RuntimeError: - loop = asyncio.new_event_loop() - asyncio.set_event_loop(loop) - try: - asyncio.get_running_loop() + loop = asyncio.get_running_loop() if not hasattr(loop.__class__, "_nest_patched"): import nest_asyncio nest_asyncio.apply(loop) except RuntimeError: - pass + loop = asyncio.new_event_loop() + asyncio.set_event_loop(loop) except ImportError: raise RuntimeError( 'Use "create_async" instead of "create" function in a running event loop. Or install "nest_asyncio" package.' diff --git a/g4f/gui/server/backend.py b/g4f/gui/server/backend.py index 4a5cafa8c9..b4c8f56c05 100644 --- a/g4f/gui/server/backend.py +++ b/g4f/gui/server/backend.py @@ -5,6 +5,7 @@ from g4f import debug, version, models from g4f import _all_models, get_last_provider, ChatCompletion from g4f.image import is_allowed_extension, to_image +from g4f.errors import VersionNotFoundError from g4f.Provider import __providers__ from g4f.Provider.bing.create_images import patch_provider from .internet import get_search_message @@ -91,8 +92,12 @@ def get_version(self): Returns: dict: A dictionary containing the current and latest version. """ + try: + current_version = version.utils.current_version + except VersionNotFoundError: + current_version = None return { - "version": version.utils.current_version, + "version": current_version, "latest_version": version.get_latest_version(), } diff --git a/requirements.txt b/requirements.txt index 8a751302c9..12a7aec8ad 100644 --- a/requirements.txt +++ b/requirements.txt @@ -20,4 +20,5 @@ asyncstdlib async-property undetected-chromedriver brotli -beautifulsoup4 \ No newline at end of file +beautifulsoup4 +setuptools \ No newline at end of file diff --git a/setup.py b/setup.py index 30e037a4a5..34f083eee1 100644 --- a/setup.py +++ b/setup.py @@ -32,6 +32,7 @@ "undetected-chromedriver", "brotli", "beautifulsoup4", + "setuptools", ] DESCRIPTION = (