From f4e738bb4098b649f37c85d047a324484ddf45fc Mon Sep 17 00:00:00 2001 From: Christophe Bornet Date: Fri, 27 Sep 2024 18:08:35 +0200 Subject: [PATCH] core: Add ruff rules for PIE (#26939) All auto-fixes. --- .../langchain_core/callbacks/streaming_stdout.py | 1 - .../langchain_core/language_models/chat_models.py | 6 +++--- libs/core/langchain_core/prompts/chat.py | 8 +++----- libs/core/langchain_core/runnables/config.py | 2 -- libs/core/langchain_core/runnables/configurable.py | 2 -- libs/core/langchain_core/runnables/fallbacks.py | 3 ++- libs/core/langchain_core/tools/base.py | 2 -- libs/core/langchain_core/tracers/base.py | 1 - libs/core/langchain_core/tracers/event_stream.py | 1 - libs/core/langchain_core/utils/function_calling.py | 2 +- libs/core/langchain_core/utils/mustache.py | 2 -- libs/core/langchain_core/utils/utils.py | 2 -- libs/core/pyproject.toml | 2 +- libs/core/tests/integration_tests/test_compile.py | 1 - .../tests/unit_tests/_api/test_beta_decorator.py | 2 -- libs/core/tests/unit_tests/_api/test_deprecation.py | 4 ---- .../language_models/chat_models/test_base.py | 2 +- .../unit_tests/language_models/llms/test_base.py | 2 +- libs/core/tests/unit_tests/test_messages.py | 12 ++++-------- libs/core/tests/unit_tests/test_tools.py | 1 - .../tests/unit_tests/utils/test_function_calling.py | 10 ---------- 21 files changed, 16 insertions(+), 52 deletions(-) diff --git a/libs/core/langchain_core/callbacks/streaming_stdout.py b/libs/core/langchain_core/callbacks/streaming_stdout.py index 4ae0bcb419a0f..7f6fa5790801b 100644 --- a/libs/core/langchain_core/callbacks/streaming_stdout.py +++ b/libs/core/langchain_core/callbacks/streaming_stdout.py @@ -112,7 +112,6 @@ def on_agent_action(self, action: AgentAction, **kwargs: Any) -> Any: action (AgentAction): The agent action. **kwargs (Any): Additional keyword arguments. """ - pass def on_tool_end(self, output: Any, **kwargs: Any) -> None: """Run when tool ends running. diff --git a/libs/core/langchain_core/language_models/chat_models.py b/libs/core/langchain_core/language_models/chat_models.py index 57a0cd37c13b4..7d46f490bbb70 100644 --- a/libs/core/langchain_core/language_models/chat_models.py +++ b/libs/core/langchain_core/language_models/chat_models.py @@ -357,7 +357,7 @@ def stream( stop: Optional[list[str]] = None, **kwargs: Any, ) -> Iterator[BaseMessageChunk]: - if not self._should_stream(async_api=False, **{**kwargs, **{"stream": True}}): + if not self._should_stream(async_api=False, **{**kwargs, "stream": True}): # model doesn't implement streaming, so use default implementation yield cast( BaseMessageChunk, self.invoke(input, config=config, stop=stop, **kwargs) @@ -427,7 +427,7 @@ async def astream( stop: Optional[list[str]] = None, **kwargs: Any, ) -> AsyncIterator[BaseMessageChunk]: - if not self._should_stream(async_api=True, **{**kwargs, **{"stream": True}}): + if not self._should_stream(async_api=True, **{**kwargs, "stream": True}): # No async or sync stream is implemented, so fall back to ainvoke yield cast( BaseMessageChunk, @@ -550,7 +550,7 @@ def _get_ls_params( def _get_llm_string(self, stop: Optional[list[str]] = None, **kwargs: Any) -> str: if self.is_lc_serializable(): - params = {**kwargs, **{"stop": stop}} + params = {**kwargs, "stop": stop} param_string = str(sorted(params.items())) # This code is not super efficient as it goes back and forth between # json and dict. diff --git a/libs/core/langchain_core/prompts/chat.py b/libs/core/langchain_core/prompts/chat.py index ab4a401057297..7a721daa1e937 100644 --- a/libs/core/langchain_core/prompts/chat.py +++ b/libs/core/langchain_core/prompts/chat.py @@ -1007,11 +1007,9 @@ def __init__( input_vars.update(_message.input_variables) kwargs = { - **{ - "input_variables": sorted(input_vars), - "optional_variables": sorted(optional_variables), - "partial_variables": partial_vars, - }, + "input_variables": sorted(input_vars), + "optional_variables": sorted(optional_variables), + "partial_variables": partial_vars, **kwargs, } cast(type[ChatPromptTemplate], super()).__init__(messages=_messages, **kwargs) diff --git a/libs/core/langchain_core/runnables/config.py b/libs/core/langchain_core/runnables/config.py index ab1c8641b9380..b1d40bd5510fc 100644 --- a/libs/core/langchain_core/runnables/config.py +++ b/libs/core/langchain_core/runnables/config.py @@ -36,8 +36,6 @@ class EmptyDict(TypedDict, total=False): """Empty dict type.""" - pass - class RunnableConfig(TypedDict, total=False): """Configuration for a Runnable.""" diff --git a/libs/core/langchain_core/runnables/configurable.py b/libs/core/langchain_core/runnables/configurable.py index bfd00c1de7f5e..5c56b8ef50380 100644 --- a/libs/core/langchain_core/runnables/configurable.py +++ b/libs/core/langchain_core/runnables/configurable.py @@ -457,8 +457,6 @@ def _prepare( class StrEnum(str, enum.Enum): """String enum.""" - pass - _enums_for_spec: WeakValueDictionary[ Union[ diff --git a/libs/core/langchain_core/runnables/fallbacks.py b/libs/core/langchain_core/runnables/fallbacks.py index d331e3567e820..647c08b3e214c 100644 --- a/libs/core/langchain_core/runnables/fallbacks.py +++ b/libs/core/langchain_core/runnables/fallbacks.py @@ -619,7 +619,8 @@ def wrapped(*args: Any, **kwargs: Any) -> Any: return self.__class__( **{ **self.model_dump(), - **{"runnable": new_runnable, "fallbacks": new_fallbacks}, + "runnable": new_runnable, + "fallbacks": new_fallbacks, } ) diff --git a/libs/core/langchain_core/tools/base.py b/libs/core/langchain_core/tools/base.py index 1fc670cc27784..d8f63c069eba3 100644 --- a/libs/core/langchain_core/tools/base.py +++ b/libs/core/langchain_core/tools/base.py @@ -320,8 +320,6 @@ class ToolException(Exception): # noqa: N818 to the agent as observation, and printed in red on the console. """ - pass - class BaseTool(RunnableSerializable[Union[str, dict, ToolCall], Any]): """Interface LangChain tools must implement.""" diff --git a/libs/core/langchain_core/tracers/base.py b/libs/core/langchain_core/tracers/base.py index a31e6d9edee47..ba8c90f2c9a9e 100644 --- a/libs/core/langchain_core/tracers/base.py +++ b/libs/core/langchain_core/tracers/base.py @@ -850,7 +850,6 @@ async def on_retriever_end( async def _on_run_create(self, run: Run) -> None: """Process a run upon creation.""" - pass async def _on_run_update(self, run: Run) -> None: """Process a run upon update.""" diff --git a/libs/core/langchain_core/tracers/event_stream.py b/libs/core/langchain_core/tracers/event_stream.py index d374a3cd3f4a0..03d7bc956169b 100644 --- a/libs/core/langchain_core/tracers/event_stream.py +++ b/libs/core/langchain_core/tracers/event_stream.py @@ -829,7 +829,6 @@ async def _astream_events_implementation_v1( inputs = log_entry["inputs"] if inputs is not None: data["input"] = inputs - pass if event_type == "end": inputs = log_entry["inputs"] diff --git a/libs/core/langchain_core/utils/function_calling.py b/libs/core/langchain_core/utils/function_calling.py index f88eb40b382a2..f8227a06a6f53 100644 --- a/libs/core/langchain_core/utils/function_calling.py +++ b/libs/core/langchain_core/utils/function_calling.py @@ -561,7 +561,7 @@ def _parse_google_docstring( if block.startswith("Args:"): args_block = block break - elif block.startswith("Returns:") or block.startswith("Example:"): + elif block.startswith(("Returns:", "Example:")): # Don't break in case Args come after past_descriptors = True elif not past_descriptors: diff --git a/libs/core/langchain_core/utils/mustache.py b/libs/core/langchain_core/utils/mustache.py index c517233a3659e..a64ec346c5c7e 100644 --- a/libs/core/langchain_core/utils/mustache.py +++ b/libs/core/langchain_core/utils/mustache.py @@ -32,8 +32,6 @@ class ChevronError(SyntaxError): """Custom exception for Chevron errors.""" - pass - # # Helper functions diff --git a/libs/core/langchain_core/utils/utils.py b/libs/core/langchain_core/utils/utils.py index a90eba11b9d5e..8236e7fba10cb 100644 --- a/libs/core/langchain_core/utils/utils.py +++ b/libs/core/langchain_core/utils/utils.py @@ -268,8 +268,6 @@ def convert_to_secret_str(value: Union[SecretStr, str]) -> SecretStr: class _NoDefaultType: """Type to indicate no default value is provided.""" - pass - _NoDefault = _NoDefaultType() diff --git a/libs/core/pyproject.toml b/libs/core/pyproject.toml index a731811d08a6c..9bb2146b7bc93 100644 --- a/libs/core/pyproject.toml +++ b/libs/core/pyproject.toml @@ -44,7 +44,7 @@ python = ">=3.12.4" [tool.poetry.extras] [tool.ruff.lint] -select = [ "B", "C4", "E", "F", "I", "N", "T201", "UP",] +select = [ "B", "C4", "E", "F", "I", "N", "PIE", "T201", "UP",] ignore = [ "UP007",] [tool.coverage.run] diff --git a/libs/core/tests/integration_tests/test_compile.py b/libs/core/tests/integration_tests/test_compile.py index 33ecccdfa0fbd..f315e45f521c0 100644 --- a/libs/core/tests/integration_tests/test_compile.py +++ b/libs/core/tests/integration_tests/test_compile.py @@ -4,4 +4,3 @@ @pytest.mark.compile def test_placeholder() -> None: """Used for compiling integration tests without running any real tests.""" - pass diff --git a/libs/core/tests/unit_tests/_api/test_beta_decorator.py b/libs/core/tests/unit_tests/_api/test_beta_decorator.py index 07480b683195d..40465813d28f1 100644 --- a/libs/core/tests/unit_tests/_api/test_beta_decorator.py +++ b/libs/core/tests/unit_tests/_api/test_beta_decorator.py @@ -68,7 +68,6 @@ async def beta_async_function() -> str: class ClassWithBetaMethods: def __init__(self) -> None: """original doc""" - pass @beta() def beta_method(self) -> str: @@ -244,7 +243,6 @@ def test_whole_class_beta() -> None: class BetaClass: def __init__(self) -> None: """original doc""" - pass @beta() def beta_method(self) -> str: diff --git a/libs/core/tests/unit_tests/_api/test_deprecation.py b/libs/core/tests/unit_tests/_api/test_deprecation.py index 726e8f9f9d300..06ffa55a8d722 100644 --- a/libs/core/tests/unit_tests/_api/test_deprecation.py +++ b/libs/core/tests/unit_tests/_api/test_deprecation.py @@ -88,7 +88,6 @@ async def deprecated_async_function() -> str: class ClassWithDeprecatedMethods: def __init__(self) -> None: """original doc""" - pass @deprecated(since="2.0.0", removal="3.0.0") def deprecated_method(self) -> str: @@ -268,7 +267,6 @@ def test_whole_class_deprecation() -> None: class DeprecatedClass: def __init__(self) -> None: """original doc""" - pass @deprecated(since="2.0.0", removal="3.0.0") def deprecated_method(self) -> str: @@ -311,7 +309,6 @@ def test_whole_class_inherited_deprecation() -> None: class DeprecatedClass: def __init__(self) -> None: """original doc""" - pass @deprecated(since="2.0.0", removal="3.0.0") def deprecated_method(self) -> str: @@ -324,7 +321,6 @@ class InheritedDeprecatedClass(DeprecatedClass): def __init__(self) -> None: """original doc""" - pass @deprecated(since="2.2.0", removal="3.2.0") def deprecated_method(self) -> str: diff --git a/libs/core/tests/unit_tests/language_models/chat_models/test_base.py b/libs/core/tests/unit_tests/language_models/chat_models/test_base.py index 99d74e47cc91c..85f6d5bee1d91 100644 --- a/libs/core/tests/unit_tests/language_models/chat_models/test_base.py +++ b/libs/core/tests/unit_tests/language_models/chat_models/test_base.py @@ -107,7 +107,7 @@ def eval_response(callback: BaseFakeCallbackHandler, i: int) -> None: else: assert llm_result.generations[0][0].text == message[:i] - for i in range(0, 2): + for i in range(2): llm = FakeListChatModel( responses=[message], error_on_chunk_number=i, diff --git a/libs/core/tests/unit_tests/language_models/llms/test_base.py b/libs/core/tests/unit_tests/language_models/llms/test_base.py index e610838d69509..9c995f924ce57 100644 --- a/libs/core/tests/unit_tests/language_models/llms/test_base.py +++ b/libs/core/tests/unit_tests/language_models/llms/test_base.py @@ -105,7 +105,7 @@ def eval_response(callback: BaseFakeCallbackHandler, i: int) -> None: else: assert llm_result.generations[0][0].text == message[:i] - for i in range(0, 2): + for i in range(2): llm = FakeStreamingListLLM( responses=[message], error_on_chunk_number=i, diff --git a/libs/core/tests/unit_tests/test_messages.py b/libs/core/tests/unit_tests/test_messages.py index 2743783ca85ad..9d659d6bd8c0b 100644 --- a/libs/core/tests/unit_tests/test_messages.py +++ b/libs/core/tests/unit_tests/test_messages.py @@ -429,16 +429,12 @@ def test_message_chunk_to_message() -> None: expected = AIMessage( content="I am", tool_calls=[ - create_tool_call(**{"name": "tool1", "args": {"a": 1}, "id": "1"}), # type: ignore[arg-type] - create_tool_call(**{"name": "tool2", "args": {}, "id": "2"}), # type: ignore[arg-type] + create_tool_call(name="tool1", args={"a": 1}, id="1"), # type: ignore[arg-type] + create_tool_call(name="tool2", args={}, id="2"), # type: ignore[arg-type] ], invalid_tool_calls=[ - create_invalid_tool_call( - **{"name": "tool3", "args": None, "id": "3", "error": None} - ), - create_invalid_tool_call( - **{"name": "tool4", "args": "abc", "id": "4", "error": None} - ), + create_invalid_tool_call(name="tool3", args=None, id="3", error=None), + create_invalid_tool_call(name="tool4", args="abc", id="4", error=None), ], ) assert message_chunk_to_message(chunk) == expected diff --git a/libs/core/tests/unit_tests/test_tools.py b/libs/core/tests/unit_tests/test_tools.py index dc70ef9dbbed5..a61cead53c23f 100644 --- a/libs/core/tests/unit_tests/test_tools.py +++ b/libs/core/tests/unit_tests/test_tools.py @@ -357,7 +357,6 @@ def structured_tool( some_base_model: SomeBaseModel, another_base_model: AnotherBaseModel ) -> None: """Return the arguments directly.""" - pass def test_base_tool_inheritance_base_schema() -> None: diff --git a/libs/core/tests/unit_tests/utils/test_function_calling.py b/libs/core/tests/unit_tests/utils/test_function_calling.py index 6f09624d74397..01943ab4db32d 100644 --- a/libs/core/tests/unit_tests/utils/test_function_calling.py +++ b/libs/core/tests/unit_tests/utils/test_function_calling.py @@ -54,7 +54,6 @@ def dummy_function( arg2: ExtensionsAnnotated[Literal["bar", "baz"], "one of 'bar', 'baz'"], ) -> None: """dummy function""" - pass return dummy_function @@ -68,7 +67,6 @@ def dummy_function(arg1: int, arg2: Literal["bar", "baz"]) -> None: arg1: foo arg2: one of 'bar', 'baz' """ - pass return dummy_function @@ -220,7 +218,6 @@ def dummy_function(self, arg1: int, arg2: Literal["bar", "baz"]) -> None: arg1: foo arg2: one of 'bar', 'baz' """ - pass class DummyWithClassMethod: @@ -232,7 +229,6 @@ def dummy_function(cls, arg1: int, arg2: Literal["bar", "baz"]) -> None: arg1: foo arg2: one of 'bar', 'baz' """ - pass def test_convert_to_openai_function( @@ -334,7 +330,6 @@ class NestedV2(BaseModelV2Maybe): def my_function(arg1: NestedV2) -> None: """dummy function""" - pass convert_to_openai_function(my_function) @@ -348,7 +343,6 @@ class Nested(BaseModel): def my_function(arg1: Nested) -> None: """dummy function""" - pass expected = { "name": "my_function", @@ -386,7 +380,6 @@ class Nested(BaseModel): def my_function(arg1: Nested) -> None: """dummy function""" - pass expected = { "name": "my_function", @@ -429,7 +422,6 @@ def func5( c: Optional[list[Optional[str]]], ) -> None: """A test function""" - pass func = convert_to_openai_function(func5) req = func["parameters"]["required"] @@ -439,7 +431,6 @@ def func5( def test_function_no_params() -> None: def nullary_function() -> None: """nullary function""" - pass func = convert_to_openai_function(nullary_function) req = func["parameters"].get("required") @@ -781,7 +772,6 @@ def test_convert_union_type_py_39() -> None: @tool def magic_function(input: int | float) -> str: """Compute a magic function.""" - pass result = convert_to_openai_function(magic_function) assert result["parameters"]["properties"]["input"] == {