Skip to content

Commit

Permalink
core: Put Python version as a project requirement so it is considered…
Browse files Browse the repository at this point in the history
… by ruff (langchain-ai#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 <[email protected]>
  • Loading branch information
2 people authored and Sheepsta300 committed Oct 1, 2024
1 parent 7494d81 commit f85e67c
Show file tree
Hide file tree
Showing 162 changed files with 919 additions and 1,001 deletions.
5 changes: 3 additions & 2 deletions libs/core/langchain_core/_api/beta_decorator.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
import functools
import inspect
import warnings
from typing import Any, Callable, Generator, Type, TypeVar, Union, cast
from collections.abc import Generator
from typing import Any, Callable, TypeVar, Union, cast

from langchain_core._api.internal import is_caller_internal

Expand All @@ -26,7 +27,7 @@ class LangChainBetaWarning(DeprecationWarning):
# PUBLIC API


T = TypeVar("T", bound=Union[Callable[..., Any], Type])
T = TypeVar("T", bound=Union[Callable[..., Any], type])


def beta(
Expand Down
9 changes: 4 additions & 5 deletions libs/core/langchain_core/_api/deprecation.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,10 @@
import functools
import inspect
import warnings
from collections.abc import Generator
from typing import (
Any,
Callable,
Generator,
Type,
TypeVar,
Union,
cast,
Expand All @@ -41,7 +40,7 @@ class LangChainPendingDeprecationWarning(PendingDeprecationWarning):


# Last Any should be FieldInfoV1 but this leads to circular imports
T = TypeVar("T", bound=Union[Type, Callable[..., Any], Any])
T = TypeVar("T", bound=Union[type, Callable[..., Any], Any])


def _validate_deprecation_params(
Expand Down Expand Up @@ -262,7 +261,7 @@ def finalize(wrapper: Callable[..., Any], new_doc: str) -> T:
if not _obj_type:
_obj_type = "attribute"
wrapped = None
_name = _name or cast(Union[Type, Callable], obj.fget).__qualname__
_name = _name or cast(Union[type, Callable], obj.fget).__qualname__
old_doc = obj.__doc__

class _deprecated_property(property):
Expand Down Expand Up @@ -304,7 +303,7 @@ def finalize(wrapper: Callable[..., Any], new_doc: str) -> T:
)

else:
_name = _name or cast(Union[Type, Callable], obj).__qualname__
_name = _name or cast(Union[type, Callable], obj).__qualname__
if not _obj_type:
# edge case: when a function is within another function
# within a test, this will call it a "method" not a "function"
Expand Down
3 changes: 2 additions & 1 deletion libs/core/langchain_core/agents.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
from __future__ import annotations

import json
from typing import Any, Literal, Sequence, Union
from collections.abc import Sequence
from typing import Any, Literal, Union

from langchain_core.load.serializable import Serializable
from langchain_core.messages import (
Expand Down
36 changes: 15 additions & 21 deletions libs/core/langchain_core/beta/runnables/context.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,13 @@
import asyncio
import threading
from collections import defaultdict
from collections.abc import Awaitable, Mapping, Sequence
from functools import partial
from itertools import groupby
from typing import (
Any,
Awaitable,
Callable,
DefaultDict,
Dict,
List,
Mapping,
Optional,
Sequence,
Type,
TypeVar,
Union,
)
Expand All @@ -30,7 +24,7 @@
from langchain_core.runnables.utils import ConfigurableFieldSpec, Input, Output

T = TypeVar("T")
Values = Dict[Union[asyncio.Event, threading.Event], Any]
Values = dict[Union[asyncio.Event, threading.Event], Any]
CONTEXT_CONFIG_PREFIX = "__context__/"
CONTEXT_CONFIG_SUFFIX_GET = "/get"
CONTEXT_CONFIG_SUFFIX_SET = "/set"
Expand Down Expand Up @@ -70,10 +64,10 @@ def _key_from_id(id_: str) -> str:

def _config_with_context(
config: RunnableConfig,
steps: List[Runnable],
steps: list[Runnable],
setter: Callable,
getter: Callable,
event_cls: Union[Type[threading.Event], Type[asyncio.Event]],
event_cls: Union[type[threading.Event], type[asyncio.Event]],
) -> RunnableConfig:
if any(k.startswith(CONTEXT_CONFIG_PREFIX) for k in config.get("configurable", {})):
return config
Expand All @@ -99,10 +93,10 @@ def _config_with_context(
}

values: Values = {}
events: DefaultDict[str, Union[asyncio.Event, threading.Event]] = defaultdict(
events: defaultdict[str, Union[asyncio.Event, threading.Event]] = defaultdict(
event_cls
)
context_funcs: Dict[str, Callable[[], Any]] = {}
context_funcs: dict[str, Callable[[], Any]] = {}
for key, group in grouped_by_key.items():
getters = [s for s in group if s[0].id.endswith(CONTEXT_CONFIG_SUFFIX_GET)]
setters = [s for s in group if s[0].id.endswith(CONTEXT_CONFIG_SUFFIX_SET)]
Expand All @@ -129,7 +123,7 @@ def _config_with_context(

def aconfig_with_context(
config: RunnableConfig,
steps: List[Runnable],
steps: list[Runnable],
) -> RunnableConfig:
"""Asynchronously patch a runnable config with context getters and setters.
Expand All @@ -145,7 +139,7 @@ def aconfig_with_context(

def config_with_context(
config: RunnableConfig,
steps: List[Runnable],
steps: list[Runnable],
) -> RunnableConfig:
"""Patch a runnable config with context getters and setters.
Expand All @@ -165,13 +159,13 @@ class ContextGet(RunnableSerializable):

prefix: str = ""

key: Union[str, List[str]]
key: Union[str, list[str]]

def __str__(self) -> str:
return f"ContextGet({_print_keys(self.key)})"

@property
def ids(self) -> List[str]:
def ids(self) -> list[str]:
prefix = self.prefix + "/" if self.prefix else ""
keys = self.key if isinstance(self.key, list) else [self.key]
return [
Expand All @@ -180,7 +174,7 @@ def ids(self) -> List[str]:
]

@property
def config_specs(self) -> List[ConfigurableFieldSpec]:
def config_specs(self) -> list[ConfigurableFieldSpec]:
return super().config_specs + [
ConfigurableFieldSpec(
id=id_,
Expand Down Expand Up @@ -256,15 +250,15 @@ def __str__(self) -> str:
return f"ContextSet({_print_keys(list(self.keys.keys()))})"

@property
def ids(self) -> List[str]:
def ids(self) -> list[str]:
prefix = self.prefix + "/" if self.prefix else ""
return [
f"{CONTEXT_CONFIG_PREFIX}{prefix}{key}{CONTEXT_CONFIG_SUFFIX_SET}"
for key in self.keys
]

@property
def config_specs(self) -> List[ConfigurableFieldSpec]:
def config_specs(self) -> list[ConfigurableFieldSpec]:
mapper_config_specs = [
s
for mapper in self.keys.values()
Expand Down Expand Up @@ -364,7 +358,7 @@ def create_scope(scope: str, /) -> "PrefixContext":
return PrefixContext(prefix=scope)

@staticmethod
def getter(key: Union[str, List[str]], /) -> ContextGet:
def getter(key: Union[str, list[str]], /) -> ContextGet:
return ContextGet(key=key)

@staticmethod
Expand All @@ -385,7 +379,7 @@ class PrefixContext:
def __init__(self, prefix: str = ""):
self.prefix = prefix

def getter(self, key: Union[str, List[str]], /) -> ContextGet:
def getter(self, key: Union[str, list[str]], /) -> ContextGet:
return ContextGet(key=key, prefix=self.prefix)

def setter(
Expand Down
3 changes: 2 additions & 1 deletion libs/core/langchain_core/caches.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
from __future__ import annotations

from abc import ABC, abstractmethod
from typing import Any, Optional, Sequence
from collections.abc import Sequence
from typing import Any, Optional

from langchain_core.outputs import Generation
from langchain_core.runnables import run_in_executor
Expand Down
5 changes: 3 additions & 2 deletions libs/core/langchain_core/callbacks/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
from __future__ import annotations

import logging
from typing import TYPE_CHECKING, Any, List, Optional, Sequence, TypeVar, Union
from collections.abc import Sequence
from typing import TYPE_CHECKING, Any, Optional, TypeVar, Union
from uuid import UUID

from tenacity import RetryCallState
Expand Down Expand Up @@ -1070,4 +1071,4 @@ def remove_metadata(self, keys: list[str]) -> None:
self.inheritable_metadata.pop(key)


Callbacks = Optional[Union[List[BaseCallbackHandler], BaseCallbackManager]]
Callbacks = Optional[Union[list[BaseCallbackHandler], BaseCallbackManager]]
8 changes: 2 additions & 6 deletions libs/core/langchain_core/callbacks/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,15 @@
import logging
import uuid
from abc import ABC, abstractmethod
from collections.abc import AsyncGenerator, Coroutine, Generator, Sequence
from concurrent.futures import ThreadPoolExecutor
from contextlib import asynccontextmanager, contextmanager
from contextvars import copy_context
from typing import (
TYPE_CHECKING,
Any,
AsyncGenerator,
Callable,
Coroutine,
Generator,
Optional,
Sequence,
Type,
TypeVar,
Union,
cast,
Expand Down Expand Up @@ -2352,7 +2348,7 @@ def _configure(
and handler_class is not None
)
if var.get() is not None or create_one:
var_handler = var.get() or cast(Type[BaseCallbackHandler], handler_class)()
var_handler = var.get() or cast(type[BaseCallbackHandler], handler_class)()
if handler_class is None:
if not any(
handler is var_handler # direct pointer comparison
Expand Down
3 changes: 2 additions & 1 deletion libs/core/langchain_core/chat_history.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
from __future__ import annotations

from abc import ABC, abstractmethod
from typing import Sequence, Union
from collections.abc import Sequence
from typing import Union

from pydantic import BaseModel, Field

Expand Down
4 changes: 2 additions & 2 deletions libs/core/langchain_core/chat_loaders.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from abc import ABC, abstractmethod
from typing import Iterator, List
from collections.abc import Iterator

from langchain_core.chat_sessions import ChatSession

Expand All @@ -15,7 +15,7 @@ def lazy_load(self) -> Iterator[ChatSession]:
An iterator of chat sessions.
"""

def load(self) -> List[ChatSession]:
def load(self) -> list[ChatSession]:
"""Eagerly load the chat sessions into memory.
Returns:
Expand Down
3 changes: 2 additions & 1 deletion libs/core/langchain_core/chat_sessions.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""**Chat Sessions** are a collection of messages and function calls."""

from typing import Sequence, TypedDict
from collections.abc import Sequence
from typing import TypedDict

from langchain_core.messages import BaseMessage

Expand Down
3 changes: 2 additions & 1 deletion libs/core/langchain_core/document_loaders/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
from __future__ import annotations

from abc import ABC, abstractmethod
from typing import TYPE_CHECKING, AsyncIterator, Iterator, Optional
from collections.abc import AsyncIterator, Iterator
from typing import TYPE_CHECKING, Optional

from langchain_core.documents import Document
from langchain_core.runnables import run_in_executor
Expand Down
2 changes: 1 addition & 1 deletion libs/core/langchain_core/document_loaders/blob_loaders.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from __future__ import annotations

from abc import ABC, abstractmethod
from typing import Iterable
from collections.abc import Iterable

# Re-export Blob and PathLike for backwards compatibility
from langchain_core.documents.base import Blob as Blob
Expand Down
3 changes: 2 additions & 1 deletion libs/core/langchain_core/document_loaders/langsmith.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import datetime
import json
import uuid
from typing import Any, Callable, Iterator, Optional, Sequence, Union
from collections.abc import Iterator, Sequence
from typing import Any, Callable, Optional, Union

from langsmith import Client as LangSmithClient

Expand Down
3 changes: 2 additions & 1 deletion libs/core/langchain_core/documents/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

import contextlib
import mimetypes
from collections.abc import Generator
from io import BufferedReader, BytesIO
from pathlib import PurePath
from typing import Any, Generator, Literal, Optional, Union, cast
from typing import Any, Literal, Optional, Union, cast

from pydantic import ConfigDict, Field, field_validator, model_validator

Expand Down
3 changes: 2 additions & 1 deletion libs/core/langchain_core/documents/compressor.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from __future__ import annotations

from abc import ABC, abstractmethod
from typing import Optional, Sequence
from collections.abc import Sequence
from typing import Optional

from pydantic import BaseModel

Expand Down
3 changes: 2 additions & 1 deletion libs/core/langchain_core/documents/transformers.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from __future__ import annotations

from abc import ABC, abstractmethod
from typing import TYPE_CHECKING, Any, Sequence
from collections.abc import Sequence
from typing import TYPE_CHECKING, Any

from langchain_core.runnables.config import run_in_executor

Expand Down
9 changes: 4 additions & 5 deletions libs/core/langchain_core/embeddings/embeddings.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"""**Embeddings** interface."""

from abc import ABC, abstractmethod
from typing import List

from langchain_core.runnables.config import run_in_executor

Expand Down Expand Up @@ -35,7 +34,7 @@ class Embeddings(ABC):
"""

@abstractmethod
def embed_documents(self, texts: List[str]) -> List[List[float]]:
def embed_documents(self, texts: list[str]) -> list[list[float]]:
"""Embed search docs.
Args:
Expand All @@ -46,7 +45,7 @@ def embed_documents(self, texts: List[str]) -> List[List[float]]:
"""

@abstractmethod
def embed_query(self, text: str) -> List[float]:
def embed_query(self, text: str) -> list[float]:
"""Embed query text.
Args:
Expand All @@ -56,7 +55,7 @@ def embed_query(self, text: str) -> List[float]:
Embedding.
"""

async def aembed_documents(self, texts: List[str]) -> List[List[float]]:
async def aembed_documents(self, texts: list[str]) -> list[list[float]]:
"""Asynchronous Embed search docs.
Args:
Expand All @@ -67,7 +66,7 @@ async def aembed_documents(self, texts: List[str]) -> List[List[float]]:
"""
return await run_in_executor(None, self.embed_documents, texts)

async def aembed_query(self, text: str) -> List[float]:
async def aembed_query(self, text: str) -> list[float]:
"""Asynchronous Embed query text.
Args:
Expand Down
Loading

0 comments on commit f85e67c

Please sign in to comment.