From 37af4f1ad5fc06928b96d00e73d813ff6dde456c Mon Sep 17 00:00:00 2001 From: zhuming Date: Thu, 9 Jan 2025 15:01:45 +0800 Subject: [PATCH] fix: not import from typing_extensions but typing which is not compatible to 3.10 --- .gitignore | 1 + RELEASES.md | 4 +- ghostos/abcd/realtime.py | 3 +- ghostos/core/llms/configs.py | 6 +- ghostos/core/llms/prompt.py | 3 +- ghostos/core/messages/message_classes.py | 4 +- ghostos/core/messages/transport.py | 5 +- ghostos/core/moss/abcd.py | 3 +- ghostos/core/moss/utils.py | 3 +- ghostos/core/runtime/tasks.py | 3 +- ghostos/core/runtime/threads.py | 3 +- ghostos/entity.py | 3 +- ghostos/framework/ghostos/taskflow_impl.py | 5 +- ghostos/framework/openai_realtime/configs.py | 3 +- .../openai_realtime/event_data_objects.py | 3 +- .../openai_realtime/event_from_client.py | 2 +- .../openai_realtime/event_from_server.py | 4 +- .../openai_realtime/state_of_client.py | 3 +- .../openai_realtime/state_of_server.py | 3 +- ghostos/ghosts/moss_agent/agent.py | 3 +- ghostos/prompter.py | 3 +- .../prototypes/spherogpt/bolt/ball_impl.py | 3 +- ghostos/scripts/cli/utils.py | 3 +- ghostos/streamlit.py | 18 +++--- pyproject.toml | 4 +- tests/core/aifuncs/test_aifunc_repository.py | 4 ++ tests/core/moss/test_callable.py | 2 +- tests/python/test_asyncio.py | 58 +++++++++---------- tests/python/test_dict.py | 3 +- tests/python/test_pydantic.py | 4 +- tests/python/test_typing.py | 3 +- tests/test_streamlit_render.py | 4 +- 32 files changed, 99 insertions(+), 77 deletions(-) diff --git a/.gitignore b/.gitignore index 69202bde..d10a976f 100644 --- a/.gitignore +++ b/.gitignore @@ -14,6 +14,7 @@ __pycache__/ # Distribution / packaging .Python +.python-version build/ develop-eggs/ dist/ diff --git a/RELEASES.md b/RELEASES.md index 5d437b5e..bec319a2 100644 --- a/RELEASES.md +++ b/RELEASES.md @@ -5,9 +5,9 @@ first release version. -## v0.1.2 +## v0.1.3 -fix import `Self` from `typing`, incompatible to python 3.10. +fix not import some libs from `typing_extensions` but `typing` , incompatible to python 3.10. ## v0.1.1 diff --git a/ghostos/abcd/realtime.py b/ghostos/abcd/realtime.py index a697b832..44e4acb5 100644 --- a/ghostos/abcd/realtime.py +++ b/ghostos/abcd/realtime.py @@ -2,8 +2,9 @@ from abc import ABC, abstractmethod from typing import ( Generic, - List, Iterable, Tuple, TypeVar, Optional, Dict, Callable, Union, Self + List, Iterable, Tuple, TypeVar, Optional, Dict, Callable, Union ) +from typing_extensions import Self from ghostos.abcd import Conversation from ghostos.core.messages import Message, ReceiverBuffer from ghostos.entity import ModelEntityMeta, to_entity_model_meta, from_entity_model_meta diff --git a/ghostos/core/llms/configs.py b/ghostos/core/llms/configs.py index a56548e9..d5081d89 100644 --- a/ghostos/core/llms/configs.py +++ b/ghostos/core/llms/configs.py @@ -2,10 +2,12 @@ import os -from typing import List, Dict, Optional, Any, ClassVar, Literal +from typing import List, Dict, Optional, Any, ClassVar +from typing_extensions import Literal from pydantic import BaseModel, Field from ghostos.core.messages import Payload -from ghostos.helpers import gettext as _ + +# from ghostos.helpers import gettext as _ __all__ = [ 'ModelConf', 'ServiceConf', 'LLMsConfig', diff --git a/ghostos/core/llms/prompt.py b/ghostos/core/llms/prompt.py index 8e7c0fac..7d81c522 100644 --- a/ghostos/core/llms/prompt.py +++ b/ghostos/core/llms/prompt.py @@ -2,7 +2,8 @@ from abc import ABC, abstractmethod -from typing import List, Iterable, Optional, Union, Callable, Self, Set +from typing import List, Iterable, Optional, Union, Callable, Set +from typing_extensions import Self from openai.types.chat.completion_create_params import Function, FunctionCall from openai import NotGiven, NOT_GIVEN from openai.types.chat.chat_completion_function_call_option_param import ChatCompletionFunctionCallOptionParam diff --git a/ghostos/core/messages/message_classes.py b/ghostos/core/messages/message_classes.py index 56f631a0..af39a8c1 100644 --- a/ghostos/core/messages/message_classes.py +++ b/ghostos/core/messages/message_classes.py @@ -1,6 +1,6 @@ import base64 -from typing import Optional, Dict, List, Iterable, Any, Union, Literal -from typing_extensions import Self +from typing import Optional, Dict, List, Iterable, Any, Union +from typing_extensions import Self, Literal from ghostos.contracts.variables import Variables from ghostos.contracts.assets import FileInfo diff --git a/ghostos/core/messages/transport.py b/ghostos/core/messages/transport.py index 12b2f3fd..0dd88987 100644 --- a/ghostos/core/messages/transport.py +++ b/ghostos/core/messages/transport.py @@ -1,7 +1,6 @@ from __future__ import annotations -from typing import Iterable, Optional, Tuple, List, Self, Iterator - -from typing_extensions import Protocol +from typing import Iterable, Optional, Tuple, List, Iterator +from typing_extensions import Protocol, Self from collections import deque from abc import abstractmethod from ghostos.core.messages.message import Message, MessageType diff --git a/ghostos/core/moss/abcd.py b/ghostos/core/moss/abcd.py index be4e8102..85d0de60 100644 --- a/ghostos/core/moss/abcd.py +++ b/ghostos/core/moss/abcd.py @@ -1,5 +1,6 @@ from __future__ import annotations -from typing import Dict, Any, Union, List, Optional, NamedTuple, Type, Callable, Self, TypeVar, ClassVar +from typing import Dict, Any, Union, List, Optional, NamedTuple, Type, Callable, TypeVar, ClassVar +from typing_extensions import Self from types import ModuleType from abc import ABC, abstractmethod from ghostos.container import Container, Provider, Factory, provide diff --git a/ghostos/core/moss/utils.py b/ghostos/core/moss/utils.py index e13d92c6..bd9e0d45 100644 --- a/ghostos/core/moss/utils.py +++ b/ghostos/core/moss/utils.py @@ -1,6 +1,7 @@ import inspect import re -from typing import Any, Dict, Callable, Optional, List, Iterable, TypedDict, is_typeddict +from typing import Any, Callable, Optional, List, Iterable +from typing_extensions import TypedDict, is_typeddict from pydantic import BaseModel __all__ = [ diff --git a/ghostos/core/runtime/tasks.py b/ghostos/core/runtime/tasks.py index 917db808..80a42009 100644 --- a/ghostos/core/runtime/tasks.py +++ b/ghostos/core/runtime/tasks.py @@ -1,4 +1,5 @@ -from typing import Optional, List, ClassVar, Dict, Self +from typing import Optional, List, ClassVar, Dict +from typing_extensions import Self from abc import ABC, abstractmethod from enum import Enum from pydantic import BaseModel, Field diff --git a/ghostos/core/runtime/threads.py b/ghostos/core/runtime/threads.py index 093df577..693c9b51 100644 --- a/ghostos/core/runtime/threads.py +++ b/ghostos/core/runtime/threads.py @@ -1,4 +1,5 @@ -from typing import Optional, List, Iterable, Dict, Any, Self +from typing import Optional, List, Iterable, Dict, Any +from typing_extensions import Self from abc import ABC, abstractmethod from pydantic import BaseModel, Field from ghostos.core.messages import Message, copy_messages, Role diff --git a/ghostos/entity.py b/ghostos/entity.py index 1857dbca..170e8437 100644 --- a/ghostos/entity.py +++ b/ghostos/entity.py @@ -2,7 +2,8 @@ import json from abc import ABC, abstractmethod -from typing import Union, Any, TypedDict, Required, Self, TypeVar, Type, Optional, Protocol +from typing import Union, Any, TypeVar, Type, Optional, Protocol +from typing_extensions import Required, Self, TypedDict from types import ModuleType from pydantic import BaseModel from ghostos.helpers import generate_import_path, import_from_path, parse_import_path_module_and_attr_name diff --git a/ghostos/framework/ghostos/taskflow_impl.py b/ghostos/framework/ghostos/taskflow_impl.py index 66523167..b06fd4e8 100644 --- a/ghostos/framework/ghostos/taskflow_impl.py +++ b/ghostos/framework/ghostos/taskflow_impl.py @@ -1,6 +1,7 @@ from __future__ import annotations -from typing import Union, List, Self +from typing import Union, List +from typing_extensions import Self from abc import ABC from ghostos.container import Container @@ -8,7 +9,7 @@ from ghostos.abcd import fire_session_event from ghostos.core.runtime import TaskState, EventTypes, TaskBrief from ghostos.core.moss import Injection, MossRuntime -from ghostos.core.messages import MessageKind, MessageKindParser, Message, Role, MessageType +from ghostos.core.messages import MessageKind, MessageKindParser, Message, Role from pprint import pprint from contextlib import redirect_stdout from io import StringIO diff --git a/ghostos/framework/openai_realtime/configs.py b/ghostos/framework/openai_realtime/configs.py index f30d6894..6c707003 100644 --- a/ghostos/framework/openai_realtime/configs.py +++ b/ghostos/framework/openai_realtime/configs.py @@ -1,4 +1,5 @@ -from typing import ClassVar, Optional, Self +from typing import ClassVar, Optional +from typing_extensions import Self from ghostos.abcd.realtime import RealtimeAppConfig from ghostos.contracts.configs import YamlConfig from ghostos.framework.openai_realtime.event_data_objects import SessionObject diff --git a/ghostos/framework/openai_realtime/event_data_objects.py b/ghostos/framework/openai_realtime/event_data_objects.py index d65e6e28..6e0ee9ab 100644 --- a/ghostos/framework/openai_realtime/event_data_objects.py +++ b/ghostos/framework/openai_realtime/event_data_objects.py @@ -3,7 +3,8 @@ import base64 from pydantic import BaseModel, Field -from typing import Optional, Literal, List, Union, Dict +from typing import Optional, List, Union, Dict +from typing_extensions import Literal from io import BytesIO from ghostos.core.messages import ( MessageType, Message, AudioMessage, FunctionCallMessage, FunctionCallOutputMessage, diff --git a/ghostos/framework/openai_realtime/event_from_client.py b/ghostos/framework/openai_realtime/event_from_client.py index cb59b301..00661ff5 100644 --- a/ghostos/framework/openai_realtime/event_from_client.py +++ b/ghostos/framework/openai_realtime/event_from_client.py @@ -1,4 +1,4 @@ -from typing import Optional, ClassVar, Self +from typing import Optional, ClassVar from abc import ABC from enum import Enum from pydantic import BaseModel, Field diff --git a/ghostos/framework/openai_realtime/event_from_server.py b/ghostos/framework/openai_realtime/event_from_server.py index bfebf2f8..5c4c709c 100644 --- a/ghostos/framework/openai_realtime/event_from_server.py +++ b/ghostos/framework/openai_realtime/event_from_server.py @@ -1,5 +1,6 @@ import base64 -from typing import Self, List, Optional, Union, ClassVar +from typing import List, Optional, Union, ClassVar +from typing_extensions import Self from abc import ABC from enum import Enum from pydantic import BaseModel, Field @@ -7,7 +8,6 @@ from ghostos.framework.openai_realtime.event_data_objects import ( RateLimit, Response, MessageItem, DeltaIndex, ConversationObject, Error, SessionObject, - ResponseSettings, Content, ) diff --git a/ghostos/framework/openai_realtime/state_of_client.py b/ghostos/framework/openai_realtime/state_of_client.py index b407fb5e..de78dc8a 100644 --- a/ghostos/framework/openai_realtime/state_of_client.py +++ b/ghostos/framework/openai_realtime/state_of_client.py @@ -1,5 +1,6 @@ from __future__ import annotations -from typing import List, Optional, Tuple, Protocol, Self +from typing import List, Optional, Protocol +from typing_extensions import Self from abc import ABC, abstractmethod from ghostos.framework.openai_realtime.configs import OpenAIRealtimeAppConf from ghostos.core.runtime import Event as GhostOSEvent diff --git a/ghostos/framework/openai_realtime/state_of_server.py b/ghostos/framework/openai_realtime/state_of_server.py index ad728c4c..3bfef6d3 100644 --- a/ghostos/framework/openai_realtime/state_of_server.py +++ b/ghostos/framework/openai_realtime/state_of_server.py @@ -1,6 +1,7 @@ from __future__ import annotations from abc import ABC, abstractmethod -from typing import Protocol, Optional, Dict, Self, List, Union +from typing import Protocol, Optional, Dict, List, Union +from typing_extensions import Self from ghostos.framework.openai_realtime.event_from_server import * from ghostos.framework.openai_realtime.event_data_objects import ( MessageItem, diff --git a/ghostos/ghosts/moss_agent/agent.py b/ghostos/ghosts/moss_agent/agent.py index e8fb8b8f..b8a9ae54 100644 --- a/ghostos/ghosts/moss_agent/agent.py +++ b/ghostos/ghosts/moss_agent/agent.py @@ -1,4 +1,5 @@ -from typing import Union, Optional, Dict, List, Self, Iterable, Tuple, ClassVar +from typing import Union, Optional, Dict, List, Iterable, Tuple, ClassVar +from typing_extensions import Self from types import ModuleType from ghostos.identifier import Identifier diff --git a/ghostos/prompter.py b/ghostos/prompter.py index 56802c38..1726a764 100644 --- a/ghostos/prompter.py +++ b/ghostos/prompter.py @@ -2,8 +2,9 @@ import inspect from typing import ( - List, Self, Union, Callable, Any, Protocol, Optional, Dict, TypeVar, Type, Generic, Set, + List, Union, Callable, Any, Protocol, Optional, Dict, TypeVar, Type, Generic, ) +from typing_extensions import Self from abc import ABC, abstractmethod from types import ModuleType from ghostos.container import Container diff --git a/ghostos/prototypes/spherogpt/bolt/ball_impl.py b/ghostos/prototypes/spherogpt/bolt/ball_impl.py index 61b86665..92428618 100644 --- a/ghostos/prototypes/spherogpt/bolt/ball_impl.py +++ b/ghostos/prototypes/spherogpt/bolt/ball_impl.py @@ -1,4 +1,5 @@ -from typing import Literal, Optional, Callable, Self, Dict, Tuple +from typing import Optional, Callable, Dict, Tuple +from typing_extensions import Self, Literal from spherov2.commands.io import FrameRotationOptions diff --git a/ghostos/scripts/cli/utils.py b/ghostos/scripts/cli/utils.py index 43dc191d..2959b532 100644 --- a/ghostos/scripts/cli/utils.py +++ b/ghostos/scripts/cli/utils.py @@ -1,6 +1,7 @@ from __future__ import annotations import sys -from typing import Tuple, List, NamedTuple, Any, Optional, Dict, Self +from typing import Tuple, List, NamedTuple, Any, Optional, Dict +from typing_extensions import Self from types import ModuleType from ghostos.bootstrap import expect_workspace_dir diff --git a/ghostos/streamlit.py b/ghostos/streamlit.py index 4e519d26..fbded5b4 100644 --- a/ghostos/streamlit.py +++ b/ghostos/streamlit.py @@ -1,4 +1,4 @@ -from typing import Protocol, Self, TypeVar, Optional, NamedTuple, Generic +from typing import Protocol, Optional, NamedTuple, Any from abc import ABC, abstractmethod __all__ = [ @@ -8,31 +8,29 @@ 'Rendered', ] -T = TypeVar('T') - -class Rendered(Generic[T], NamedTuple): - value: T +class Rendered(NamedTuple): + value: Any changed: bool class StreamlitRenderable(Protocol): @abstractmethod - def __streamlit_render__(self) -> Optional[Rendered[Self]]: + def __streamlit_render__(self) -> Optional[Rendered]: pass class StreamlitObject(ABC): @abstractmethod - def __streamlit_render__(self) -> Optional[Rendered[Self]]: + def __streamlit_render__(self) -> Optional[Rendered]: pass class StreamlitRenderer(ABC): @abstractmethod - def render(self, value: T) -> Optional[Rendered[T]]: + def render(self, value) -> Optional[Rendered]: pass @@ -40,7 +38,7 @@ class GroupRenderer(StreamlitRenderer): def __init__(self, *renderers: StreamlitRenderer): self.renderers = list(renderers) - def render(self, value: T) -> Optional[Rendered[T]]: + def render(self, value) -> Optional[Rendered]: for renderer in self.renderers: result = renderer.render(value) if result is None: @@ -53,7 +51,7 @@ def is_streamlit_renderable(obj): return isinstance(obj, StreamlitObject) or hasattr(obj, "__streamlit_render__") -def render_streamlit_object(obj: T) -> Optional[Rendered[T]]: +def render_streamlit_object(obj) -> Optional[Rendered]: if is_streamlit_renderable(obj): fn = getattr(obj, "__streamlit_render__", None) if fn is not None: diff --git a/pyproject.toml b/pyproject.toml index 470968ec..9b1ec657 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "ghostos" -version = "0.1.2" +version = "0.1.3" description = "A framework offers an operating system simulator with a Python Code Interface for AI Agents" authors = ["zhuming ", "Nile Zhou "] license = "MIT" @@ -13,7 +13,7 @@ documentation = "https://ghost-in-moss.github.io/GhostOS/#/en/" issues = "https://github.com/ghost-in-moss/GhostOS/issues" [tool.poetry.dependencies] -python = ">=3.9,<3.14,!=3.9.7" +python = ">=3.10,<3.14" pydantic = "^2.7.0" openai = "^1.19.0" pyyaml = "^6.0.1" diff --git a/tests/core/aifuncs/test_aifunc_repository.py b/tests/core/aifuncs/test_aifunc_repository.py index 6b9ea65b..6ac40166 100644 --- a/tests/core/aifuncs/test_aifunc_repository.py +++ b/tests/core/aifuncs/test_aifunc_repository.py @@ -1,6 +1,9 @@ from ghostos.core.aifunc import AIFuncRepoByConfigsProvider, AIFuncRepository, AIFuncsConf from ghostos.framework.configs import Configs, MemoryConfigs from ghostos.contracts.modules import Modules, DefaultModules +from ghostos.contracts.workspace import Workspace +from ghostos.framework.workspaces import BasicWorkspace +from ghostos.framework.storage import MemStorage from ghostos.container import Container from ghostos.demo import aifuncs_demo @@ -8,6 +11,7 @@ def test_aifunc_repository(): container = Container() container.set(Modules, DefaultModules()) + container.set(Workspace, BasicWorkspace(MemStorage())) container.set(Configs, MemoryConfigs({ AIFuncsConf.conf_path(): "{}", diff --git a/tests/core/moss/test_callable.py b/tests/core/moss/test_callable.py index aebcdcd1..22791d3a 100644 --- a/tests/core/moss/test_callable.py +++ b/tests/core/moss/test_callable.py @@ -177,4 +177,4 @@ def test_get_callable_definition_with_abstractmethod(): from abc import abstractmethod assert not inspect.isbuiltin(abstractmethod) code = get_callable_definition(abstractmethod) - assert code == "" + assert code != "" diff --git a/tests/python/test_asyncio.py b/tests/python/test_asyncio.py index 686dbbc9..f1839aef 100644 --- a/tests/python/test_asyncio.py +++ b/tests/python/test_asyncio.py @@ -26,32 +26,32 @@ async def foo(): lp = asyncio.new_event_loop() lp.run_until_complete(foo()) - -def test_producer_and_consumer(): - class Main: - stop = False - - got = [] - - async def _producer(self, q: asyncio.Queue): - count = 0 - while not self.stop: - q.put_nowait(count) - count += 1 - await asyncio.sleep(0.1) - - async def _consumer(self, q: asyncio.Queue): - while not self.stop: - v = await q.get() - self.got.append(v) - self.stop = v > 2 - - async def run(self): - async with asyncio.TaskGroup() as tg: - q = asyncio.Queue() - tg.create_task(self._producer(q)) - tg.create_task(self._consumer(q)) - - main = Main() - asyncio.run(main.run()) - assert main.got == [0, 1, 2, 3] +# for 3.12 +# def test_producer_and_consumer(): +# class Main: +# stop = False +# +# got = [] +# +# async def _producer(self, q: asyncio.Queue): +# count = 0 +# while not self.stop: +# q.put_nowait(count) +# count += 1 +# await asyncio.sleep(0.1) +# +# async def _consumer(self, q: asyncio.Queue): +# while not self.stop: +# v = await q.get() +# self.got.append(v) +# self.stop = v > 2 +# +# async def run(self): +# async with asyncio.TaskGroup() as tg: +# q = asyncio.Queue() +# tg.create_task(self._producer(q)) +# tg.create_task(self._consumer(q)) +# +# main = Main() +# asyncio.run(main.run()) +# assert main.got == [0, 1, 2, 3] diff --git a/tests/python/test_dict.py b/tests/python/test_dict.py index e0b77fd3..e83cf2b1 100644 --- a/tests/python/test_dict.py +++ b/tests/python/test_dict.py @@ -1,4 +1,5 @@ -from typing import TypedDict, Optional +from typing import Optional +from typing_extensions import TypedDict def test_dict_items(): diff --git a/tests/python/test_pydantic.py b/tests/python/test_pydantic.py index 068d4799..eb3064ac 100644 --- a/tests/python/test_pydantic.py +++ b/tests/python/test_pydantic.py @@ -2,8 +2,8 @@ from pydantic import BaseModel, Field from pydantic.errors import PydanticSchemaGenerationError -from typing import TypedDict, Required, Iterable, List, Optional, ClassVar, Type -from typing_extensions import Literal +from typing import Iterable, List, Optional, ClassVar, Type +from typing_extensions import Literal, Required, TypedDict from datetime import datetime diff --git a/tests/python/test_typing.py b/tests/python/test_typing.py index 7dc7d81c..6bb3af1a 100644 --- a/tests/python/test_typing.py +++ b/tests/python/test_typing.py @@ -1,4 +1,5 @@ -from typing import Union, TypedDict, Optional, Literal +from typing import Union, Optional +from typing_extensions import Literal, TypedDict import inspect diff --git a/tests/test_streamlit_render.py b/tests/test_streamlit_render.py index d525c59c..9d698386 100644 --- a/tests/test_streamlit_render.py +++ b/tests/test_streamlit_render.py @@ -1,4 +1,4 @@ -from typing import Optional, Self +from typing import Optional from ghostos.streamlit import ( is_streamlit_renderable, @@ -11,7 +11,7 @@ def test_render_streamlit_object(): class Foo(StreamlitObject): - def __streamlit_render__(self) -> Optional[Rendered[Self]]: + def __streamlit_render__(self) -> Optional[Rendered]: return Rendered(value=self, changed=False) foo = Foo()