Skip to content

Commit

Permalink
core: fix lint 0.3rc (#25993)
Browse files Browse the repository at this point in the history
  • Loading branch information
efriis authored Sep 3, 2024
1 parent 29413a2 commit 0da201c
Show file tree
Hide file tree
Showing 13 changed files with 53 additions and 53 deletions.
2 changes: 1 addition & 1 deletion libs/core/langchain_core/load/serializable.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
cast,
)

from pydantic import BaseModel, ConfigDict
from pydantic import BaseModel, ConfigDict
from typing_extensions import NotRequired

from langchain_core.utils.pydantic import v1_repr
Expand Down
6 changes: 3 additions & 3 deletions libs/core/langchain_core/output_parsers/json.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
from typing import Any, List, Optional, Type, TypeVar, Union

import jsonpatch # type: ignore[import]
import pydantic
from pydantic import SkipValidation
import pydantic
from pydantic import SkipValidation
from typing_extensions import Annotated

from langchain_core.exceptions import OutputParserException
Expand All @@ -24,7 +24,7 @@
PydanticBaseModel = pydantic.BaseModel

else:
from pydantic.v1 import BaseModel
from pydantic.v1 import BaseModel

# Union type needs to be last assignment to PydanticBaseModel to make mypy happy.
PydanticBaseModel = Union[BaseModel, pydantic.BaseModel] # type: ignore
Expand Down
2 changes: 1 addition & 1 deletion libs/core/langchain_core/output_parsers/openai_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from json import JSONDecodeError
from typing import Any, Dict, List, Optional

from pydantic import SkipValidation, ValidationError
from pydantic import SkipValidation, ValidationError
from typing_extensions import Annotated

from langchain_core.exceptions import OutputParserException
Expand Down
4 changes: 2 additions & 2 deletions libs/core/langchain_core/output_parsers/pydantic.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import json
from typing import Generic, List, Optional, Type

import pydantic
from pydantic import SkipValidation
import pydantic
from pydantic import SkipValidation
from typing_extensions import Annotated

from langchain_core.exceptions import OutputParserException
Expand Down
2 changes: 1 addition & 1 deletion libs/core/langchain_core/runnables/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
cast,
)

from pydantic import BaseModel, ConfigDict, RootModel
from pydantic import BaseModel, ConfigDict, RootModel
from pydantic import create_model as _create_model_base # pydantic :ignore
from pydantic.json_schema import (
DEFAULT_REF_TEMPLATE,
Expand Down
6 changes: 3 additions & 3 deletions libs/core/langchain_core/utils/function_calling.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ def convert_python_function_to_openai_function(

def _convert_typed_dict_to_openai_function(typed_dict: Type) -> FunctionDescription:
visited: Dict = {}
from pydantic.v1 import BaseModel
from pydantic.v1 import BaseModel

model = cast(
Type[BaseModel],
Expand All @@ -214,8 +214,8 @@ def _convert_any_typed_dicts_to_pydantic(
visited: Dict,
depth: int = 0,
) -> Type:
from pydantic.v1 import Field as Field_v1
from pydantic.v1 import create_model as create_model_v1
from pydantic.v1 import Field as Field_v1
from pydantic.v1 import create_model as create_model_v1

if type_ in visited:
return visited[type_]
Expand Down
30 changes: 15 additions & 15 deletions libs/core/langchain_core/utils/pydantic.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
from functools import wraps
from typing import Any, Callable, Dict, List, Optional, Type, TypeVar, Union, overload

import pydantic
from pydantic import BaseModel, root_validator
from pydantic.json_schema import GenerateJsonSchema, JsonSchemaValue
from pydantic_core import core_schema
import pydantic
from pydantic import BaseModel, root_validator
from pydantic.json_schema import GenerateJsonSchema, JsonSchemaValue
from pydantic_core import core_schema


def get_pydantic_major_version() -> int:
Expand Down Expand Up @@ -77,13 +77,13 @@ def is_basemodel_subclass(cls: Type) -> bool:
return False

if PYDANTIC_MAJOR_VERSION == 1:
from pydantic import BaseModel as BaseModelV1Proper
from pydantic import BaseModel as BaseModelV1Proper

if issubclass(cls, BaseModelV1Proper):
return True
elif PYDANTIC_MAJOR_VERSION == 2:
from pydantic import BaseModel as BaseModelV2
from pydantic.v1 import BaseModel as BaseModelV1
from pydantic import BaseModel as BaseModelV2
from pydantic.v1 import BaseModel as BaseModelV1

if issubclass(cls, BaseModelV2):
return True
Expand All @@ -105,13 +105,13 @@ def is_basemodel_instance(obj: Any) -> bool:
* pydantic.v1.BaseModel in Pydantic 2.x
"""
if PYDANTIC_MAJOR_VERSION == 1:
from pydantic import BaseModel as BaseModelV1Proper
from pydantic import BaseModel as BaseModelV1Proper

if isinstance(obj, BaseModelV1Proper):
return True
elif PYDANTIC_MAJOR_VERSION == 2:
from pydantic import BaseModel as BaseModelV2
from pydantic.v1 import BaseModel as BaseModelV1
from pydantic import BaseModel as BaseModelV2
from pydantic.v1 import BaseModel as BaseModelV1

if isinstance(obj, BaseModelV2):
return True
Expand Down Expand Up @@ -222,9 +222,9 @@ def _create_subset_model_v1(
) -> Type[BaseModel]:
"""Create a pydantic model with only a subset of model's fields."""
if PYDANTIC_MAJOR_VERSION == 1:
from pydantic import create_model
from pydantic import create_model
elif PYDANTIC_MAJOR_VERSION == 2:
from pydantic.v1 import create_model # type: ignore
from pydantic.v1 import create_model # type: ignore
else:
raise NotImplementedError(
f"Unsupported pydantic version: {PYDANTIC_MAJOR_VERSION}"
Expand Down Expand Up @@ -259,8 +259,8 @@ def _create_subset_model_v2(
fn_description: Optional[str] = None,
) -> Type[pydantic.BaseModel]:
"""Create a pydantic model with a subset of the model fields."""
from pydantic import create_model
from pydantic.fields import FieldInfo
from pydantic import create_model
from pydantic.fields import FieldInfo

descriptions_ = descriptions or {}
fields = {}
Expand Down Expand Up @@ -299,7 +299,7 @@ def _create_subset_model(
fn_description=fn_description,
)
elif PYDANTIC_MAJOR_VERSION == 2:
from pydantic.v1 import BaseModel as BaseModelV1
from pydantic.v1 import BaseModel as BaseModelV1

if issubclass(model, BaseModelV1):
return _create_subset_model_v1(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -531,7 +531,7 @@ async def test_partial_pydantic_output_parser_async() -> None:
@pytest.mark.skipif(PYDANTIC_MAJOR_VERSION != 2, reason="This test is for pydantic 2")
def test_parse_with_different_pydantic_2_v1() -> None:
"""Test with pydantic.v1.BaseModel from pydantic 2."""
import pydantic
import pydantic

class Forecast(pydantic.v1.BaseModel):
temperature: int
Expand Down Expand Up @@ -566,7 +566,7 @@ class Forecast(pydantic.v1.BaseModel):
@pytest.mark.skipif(PYDANTIC_MAJOR_VERSION != 2, reason="This test is for pydantic 2")
def test_parse_with_different_pydantic_2_proper() -> None:
"""Test with pydantic.BaseModel from pydantic 2."""
import pydantic
import pydantic

class Forecast(pydantic.BaseModel):
temperature: int
Expand Down Expand Up @@ -601,7 +601,7 @@ class Forecast(pydantic.BaseModel):
@pytest.mark.skipif(PYDANTIC_MAJOR_VERSION != 1, reason="This test is for pydantic 1")
def test_parse_with_different_pydantic_1_proper() -> None:
"""Test with pydantic.BaseModel from pydantic 1."""
import pydantic
import pydantic

class Forecast(pydantic.BaseModel):
temperature: int
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
from enum import Enum
from typing import Literal, Optional

import pydantic
import pydantic
import pytest
from pydantic import BaseModel, Field
from pydantic.v1 import BaseModel as V1BaseModel
from pydantic.v1 import BaseModel as V1BaseModel

from langchain_core.exceptions import OutputParserException
from langchain_core.language_models import ParrotFakeChatModel
Expand Down
2 changes: 1 addition & 1 deletion libs/core/tests/unit_tests/runnables/test_history.py
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,7 @@ class RunnableWithChatHistoryInput(BaseModel):


def test_get_input_schema_input_messages() -> None:
from pydantic import RootModel
from pydantic import RootModel

RunnableWithMessageHistoryInput = RootModel[Sequence[BaseMessage]]

Expand Down
12 changes: 6 additions & 6 deletions libs/core/tests/unit_tests/test_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

import pytest
from pydantic import BaseModel, Field, ValidationError
from pydantic import BaseModel as BaseModelProper
from pydantic import BaseModel as BaseModelProper
from typing_extensions import Annotated, TypedDict, TypeVar

from langchain_core import tools
Expand Down Expand Up @@ -1572,7 +1572,7 @@ class FooProper(BaseModelProper):

def generate_backwards_compatible_v1() -> List[Any]:
"""Generate a model with pydantic 2 from the v1 namespace."""
from pydantic.v1 import BaseModel as BaseModelV1
from pydantic.v1 import BaseModel as BaseModelV1

class FooV1Namespace(BaseModelV1):
a: int
Expand Down Expand Up @@ -1629,7 +1629,7 @@ def test_args_schema_explicitly_typed() -> None:
is a pydantic 1 model!
"""
# Check with whatever pydantic model is passed in and not via v1 namespace
from pydantic import BaseModel
from pydantic import BaseModel

class Foo(BaseModel):
a: int
Expand Down Expand Up @@ -1873,9 +1873,9 @@ class ModelD(ModelC, Generic[D]):

@pytest.mark.skipif(PYDANTIC_MAJOR_VERSION != 2, reason="Testing pydantic v2.")
def test_tool_args_schema_pydantic_v2_with_metadata() -> None:
from pydantic import BaseModel as BaseModelV2
from pydantic import Field as FieldV2
from pydantic import ValidationError as ValidationErrorV2
from pydantic import BaseModel as BaseModelV2
from pydantic import Field as FieldV2
from pydantic import ValidationError as ValidationErrorV2

class Foo(BaseModelV2):
x: List[int] = FieldV2(
Expand Down
22 changes: 11 additions & 11 deletions libs/core/tests/unit_tests/utils/test_pydantic.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,12 @@ def validator(cls, v: Dict[str, Any]) -> Dict[str, Any]:
def test_is_basemodel_subclass() -> None:
"""Test pydantic."""
if PYDANTIC_MAJOR_VERSION == 1:
from pydantic import BaseModel as BaseModelV1Proper
from pydantic import BaseModel as BaseModelV1Proper

assert is_basemodel_subclass(BaseModelV1Proper)
elif PYDANTIC_MAJOR_VERSION == 2:
from pydantic import BaseModel as BaseModelV2
from pydantic.v1 import BaseModel as BaseModelV1
from pydantic import BaseModel as BaseModelV2
from pydantic.v1 import BaseModel as BaseModelV1

assert is_basemodel_subclass(BaseModelV2)

Expand All @@ -111,15 +111,15 @@ def test_is_basemodel_subclass() -> None:
def test_is_basemodel_instance() -> None:
"""Test pydantic."""
if PYDANTIC_MAJOR_VERSION == 1:
from pydantic import BaseModel as BaseModelV1Proper
from pydantic import BaseModel as BaseModelV1Proper

class FooV1(BaseModelV1Proper):
x: int

assert is_basemodel_instance(FooV1(x=5))
elif PYDANTIC_MAJOR_VERSION == 2:
from pydantic import BaseModel as BaseModelV2
from pydantic.v1 import BaseModel as BaseModelV1
from pydantic import BaseModel as BaseModelV2
from pydantic.v1 import BaseModel as BaseModelV1

class Foo(BaseModelV2):
x: int
Expand All @@ -137,8 +137,8 @@ class Bar(BaseModelV1):
@pytest.mark.skipif(PYDANTIC_MAJOR_VERSION != 2, reason="Only tests Pydantic v2")
def test_with_field_metadata() -> None:
"""Test pydantic with field metadata"""
from pydantic import BaseModel as BaseModelV2
from pydantic import Field as FieldV2
from pydantic import BaseModel as BaseModelV2
from pydantic import Field as FieldV2

class Foo(BaseModelV2):
x: List[int] = FieldV2(
Expand All @@ -165,7 +165,7 @@ class Foo(BaseModelV2):

@pytest.mark.skipif(PYDANTIC_MAJOR_VERSION != 1, reason="Only tests Pydantic v1")
def test_fields_pydantic_v1() -> None:
from pydantic import BaseModel
from pydantic import BaseModel

class Foo(BaseModel):
x: int
Expand All @@ -176,7 +176,7 @@ class Foo(BaseModel):

@pytest.mark.skipif(PYDANTIC_MAJOR_VERSION != 2, reason="Only tests Pydantic v2")
def test_fields_pydantic_v2_proper() -> None:
from pydantic import BaseModel
from pydantic import BaseModel

class Foo(BaseModel):
x: int
Expand All @@ -187,7 +187,7 @@ class Foo(BaseModel):

@pytest.mark.skipif(PYDANTIC_MAJOR_VERSION != 2, reason="Only tests Pydantic v2")
def test_fields_pydantic_v1_from_2() -> None:
from pydantic.v1 import BaseModel
from pydantic.v1 import BaseModel

class Foo(BaseModel):
x: int
Expand Down
8 changes: 4 additions & 4 deletions libs/core/tests/unit_tests/utils/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,8 +221,8 @@ def test_guard_import_failure(

@pytest.mark.skipif(PYDANTIC_MAJOR_VERSION != 2, reason="Requires pydantic 2")
def test_get_pydantic_field_names_v1_in_2() -> None:
from pydantic.v1 import BaseModel as PydanticV1BaseModel
from pydantic.v1 import Field
from pydantic.v1 import BaseModel as PydanticV1BaseModel
from pydantic.v1 import Field

class PydanticV1Model(PydanticV1BaseModel):
field1: str
Expand All @@ -236,7 +236,7 @@ class PydanticV1Model(PydanticV1BaseModel):

@pytest.mark.skipif(PYDANTIC_MAJOR_VERSION != 2, reason="Requires pydantic 2")
def test_get_pydantic_field_names_v2_in_2() -> None:
from pydantic import BaseModel, Field
from pydantic import BaseModel, Field

class PydanticModel(BaseModel):
field1: str
Expand All @@ -250,7 +250,7 @@ class PydanticModel(BaseModel):

@pytest.mark.skipif(PYDANTIC_MAJOR_VERSION != 1, reason="Requires pydantic 1")
def test_get_pydantic_field_names_v1() -> None:
from pydantic import BaseModel, Field
from pydantic import BaseModel, Field

class PydanticModel(BaseModel):
field1: str
Expand Down

0 comments on commit 0da201c

Please sign in to comment.