core: Put Python version as a project requirement so it is considered by ruff (#26608)

Ruff doesn't know about the python version in
`[tool.poetry.dependencies]`. It can get it from
`project.requires-python`.

Notes:
* poetry seems to have issues getting the python constraints from
`requires-python` and using `python` in per dependency constraints. So I
had to duplicate the info. I will open an issue on poetry.
* `inspect.isclass()` doesn't work correctly with `GenericAlias`
(`list[...]`, `dict[..., ...]`) on Python <3.11 so I added some `not
isinstance(type, GenericAlias)` checks:

Python 3.11
```pycon
>>> import inspect
>>> inspect.isclass(list)
True
>>> inspect.isclass(list[str])
False
```

Python 3.9
```pycon
>>> import inspect
>>> inspect.isclass(list)
True
>>> inspect.isclass(list[str])
True
```

Co-authored-by: Eugene Yurtsev <eyurtsev@gmail.com>
This commit is contained in:
Christophe Bornet
2024-09-18 16:37:57 +02:00
committed by GitHub
parent 0f07cf61da
commit a47b332841
162 changed files with 920 additions and 1002 deletions

View File

@@ -1,7 +1,7 @@
"""A fake callback handler for testing purposes."""
from itertools import chain
from typing import Any, Dict, List, Optional, Union
from typing import Any, Optional, Union
from uuid import UUID
from pydantic import BaseModel
@@ -16,7 +16,7 @@ class BaseFakeCallbackHandler(BaseModel):
starts: int = 0
ends: int = 0
errors: int = 0
errors_args: List[Any] = []
errors_args: list[Any] = []
text: int = 0
ignore_llm_: bool = False
ignore_chain_: bool = False
@@ -265,8 +265,8 @@ class FakeCallbackHandler(BaseCallbackHandler, BaseFakeCallbackHandlerMixin):
class FakeCallbackHandlerWithChatStart(FakeCallbackHandler):
def on_chat_model_start(
self,
serialized: Dict[str, Any],
messages: List[List[BaseMessage]],
serialized: dict[str, Any],
messages: list[list[BaseMessage]],
*,
run_id: UUID,
parent_run_id: Optional[UUID] = None,

View File

@@ -1,7 +1,7 @@
"""Tests for verifying that testing utility code works as expected."""
from itertools import cycle
from typing import Any, Dict, List, Optional, Union
from typing import Any, Optional, Union
from uuid import UUID
from langchain_core.callbacks.base import AsyncCallbackHandler
@@ -149,18 +149,18 @@ async def test_callback_handlers() -> None:
"""Verify that model is implemented correctly with handlers working."""
class MyCustomAsyncHandler(AsyncCallbackHandler):
def __init__(self, store: List[str]) -> None:
def __init__(self, store: list[str]) -> None:
self.store = store
async def on_chat_model_start(
self,
serialized: Dict[str, Any],
messages: List[List[BaseMessage]],
serialized: dict[str, Any],
messages: list[list[BaseMessage]],
*,
run_id: UUID,
parent_run_id: Optional[UUID] = None,
tags: Optional[List[str]] = None,
metadata: Optional[Dict[str, Any]] = None,
tags: Optional[list[str]] = None,
metadata: Optional[dict[str, Any]] = None,
**kwargs: Any,
) -> Any:
# Do nothing
@@ -174,7 +174,7 @@ async def test_callback_handlers() -> None:
chunk: Optional[Union[GenerationChunk, ChatGenerationChunk]] = None,
run_id: UUID,
parent_run_id: Optional[UUID] = None,
tags: Optional[List[str]] = None,
tags: Optional[list[str]] = None,
**kwargs: Any,
) -> None:
self.store.append(token)
@@ -185,7 +185,7 @@ async def test_callback_handlers() -> None:
]
)
model = GenericFakeChatModel(messages=infinite_cycle)
tokens: List[str] = []
tokens: list[str] = []
# New model
results = list(model.stream("meow", {"callbacks": [MyCustomAsyncHandler(tokens)]}))
assert results == [