From b0c37109d129840654e5b3a84324e61f1f97c960 Mon Sep 17 00:00:00 2001 From: Dan Buch Date: Tue, 29 Oct 2024 15:06:17 -0400 Subject: [PATCH 1/2] Downgrade `typing-extensions` to fix conflict with `spacy` and `pydantic` that results in seemingly unrelated errors like: ```python File "/root/.pyenv/versions/3.8.16/lib/python3.8/site-packages/spacy/schemas.py", line 250, in class TokenPattern(BaseModel): File "pydantic/main.py", line 197, in pydantic.main.ModelMetaclass.__new__ File "pydantic/fields.py", line 506, in pydantic.fields.ModelField.infer File "pydantic/fields.py", line 436, in pydantic.fields.ModelField.__init__ File "pydantic/fields.py", line 552, in pydantic.fields.ModelField.prepare File "pydantic/fields.py", line 661, in pydantic.fields.ModelField._type_analysis File "pydantic/fields.py", line 668, in pydantic.fields.ModelField._type_analysis File "/root/.pyenv/versions/3.8.16/lib/python3.8/typing.py", line 774, in __subclasscheck__ return issubclass(cls, self.__origin__) TypeError: issubclass() arg 1 must be a class ``` Closes PLAT-443 --- pyproject.toml | 2 +- python/cog/server/connection.py | 16 +++++++++++++++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index c6194db52f..cfe833b8ac 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -27,7 +27,7 @@ dependencies = [ "PyYAML", "requests>=2,<3", "structlog>=20,<25", - "typing_extensions>=4.6.0", + "typing_extensions>=4.4.0", "uvicorn[standard]>=0.12,<1", ] diff --git a/python/cog/server/connection.py b/python/cog/server/connection.py index c0d85722d7..348270493a 100644 --- a/python/cog/server/connection.py +++ b/python/cog/server/connection.py @@ -1,9 +1,23 @@ +import collections.abc import asyncio import multiprocessing from multiprocessing.connection import Connection from typing import Any, Optional -from typing_extensions import Buffer +# Buffer is only available in typing-extensions>=4.6.0 but should be available in stdlib +# python 3.12+. This compatibility code is nearly identical to the implementation in +# typing-extensions>=4.6.0 +if hasattr(collections.abc, "Buffer"): + Buffer = collections.abc.Buffer +else: + import abc + + class Buffer(abc.ABC): + pass + + Buffer.register(memoryview) + Buffer.register(bytearray) + Buffer.register(bytes) _spawn = multiprocessing.get_context("spawn") From 7c3aad5c21da73bac29095cfc2c5e46c8b33dd79 Mon Sep 17 00:00:00 2001 From: Dan Buch Date: Tue, 29 Oct 2024 15:24:01 -0400 Subject: [PATCH 2/2] Add some comments to request linters to calm down --- python/cog/server/connection.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/python/cog/server/connection.py b/python/cog/server/connection.py index 348270493a..8d2d2d2a31 100644 --- a/python/cog/server/connection.py +++ b/python/cog/server/connection.py @@ -1,5 +1,6 @@ -import collections.abc +import abc import asyncio +import collections.abc import multiprocessing from multiprocessing.connection import Connection from typing import Any, Optional @@ -8,11 +9,10 @@ # python 3.12+. This compatibility code is nearly identical to the implementation in # typing-extensions>=4.6.0 if hasattr(collections.abc, "Buffer"): - Buffer = collections.abc.Buffer + Buffer = collections.abc.Buffer # type: ignore else: - import abc - class Buffer(abc.ABC): + class Buffer(abc.ABC): # noqa: B024 pass Buffer.register(memoryview) @@ -72,7 +72,7 @@ def send_bytes( ) -> None: """Send the bytes data from a bytes-like object""" - self._connection.send_bytes(buf, offset, size) + self._connection.send_bytes(buf, offset, size) # type: ignore async def recv_bytes(self, maxlength: Optional[int] = None) -> bytes: """