Skip to content

armgit / dspy

Public Imported from GitHub
0

DSPy: The framework for programming—not prompting—language models

Python

Fix coding style in dspy/primitives (#8180)

Chen Qianc1b575453bd6
diff --git a/dspy/primitives/__init__.py b/dspy/primitives/__init__.py
index c7cc2c004..0fa160d26 100644
--- a/dspy/primitives/__init__.py
+++ b/dspy/primitives/__init__.py
@@ -1,8 +1,8 @@
from dspy.primitives import assertions
from dspy.primitives.example import Example
from dspy.primitives.module import BaseModule
-from dspy.primitives.prediction import Prediction, Completions
-from dspy.primitives.program import Program, Module
+from dspy.primitives.prediction import Completions, Prediction
+from dspy.primitives.program import Module, Program
from dspy.primitives.python_interpreter import PythonInterpreter
from dspy.primitives.tool import Tool
diff --git a/dspy/primitives/example.py b/dspy/primitives/example.py
index 0d809482a..589e53748 100644
--- a/dspy/primitives/example.py
+++ b/dspy/primitives/example.py
@@ -104,5 +104,5 @@ class Example:
del copied[key]
return copied
- def toDict(self):
+ def toDict(self): # noqa: N802
return self._store.copy()
diff --git a/dspy/primitives/module.py b/dspy/primitives/module.py
index 13ebe8809..3ddaf74d6 100644
--- a/dspy/primitives/module.py
+++ b/dspy/primitives/module.py
@@ -238,7 +238,7 @@ class BaseModule:
path = Path(path)
if path.suffix == ".json":
- with open(path, "r") as f:
+ with open(path) as f:
state = ujson.loads(f.read())
elif path.suffix == ".pkl":
with open(path, "rb") as f:
diff --git a/dspy/primitives/prediction.py b/dspy/primitives/prediction.py
index b05b3857c..670b816b2 100644
--- a/dspy/primitives/prediction.py
+++ b/dspy/primitives/prediction.py
@@ -26,7 +26,7 @@ class Prediction(Example):
return obj
def __repr__(self):
- store_repr = ",\n ".join(f"{k}={repr(v)}" for k, v in self._store.items())
+ store_repr = ",\n ".join(f"{k}={v!r}" for k, v in self._store.items())
if self._completions is None or len(self._completions) == 1:
return f"Prediction(\n {store_repr}\n)"
@@ -152,7 +152,7 @@ class Completions:
return key in self._completions
def __repr__(self):
- items_repr = ",\n ".join(f"{k}={repr(v)}" for k, v in self._completions.items())
+ items_repr = ",\n ".join(f"{k}={v!r}" for k, v in self._completions.items())
return f"Completions(\n {items_repr}\n)"
def __str__(self):
diff --git a/dspy/primitives/python_interpreter.py b/dspy/primitives/python_interpreter.py
index 7cb63bf98..d96baa363 100644
--- a/dspy/primitives/python_interpreter.py
+++ b/dspy/primitives/python_interpreter.py
@@ -1,8 +1,8 @@
import json
+import os
import subprocess
from types import TracebackType
from typing import Any, Dict, List, Optional
-import os
class InterpreterError(RuntimeError):
@@ -155,4 +155,4 @@ class PythonInterpreter:
self.deno_process.stdin.flush()
self.deno_process.stdin.close()
self.deno_process.wait()
- self.deno_process = None
\ No newline at end of file
+ self.deno_process = None
diff --git a/dspy/primitives/tool.py b/dspy/primitives/tool.py
index 3edea2d52..63574a9b7 100644
--- a/dspy/primitives/tool.py
+++ b/dspy/primitives/tool.py
@@ -65,7 +65,7 @@ class Tool:
self._parse_function(func, arg_desc)
- def _parse_function(self, func: Callable, arg_desc: dict[str, str] = None):
+ def _parse_function(self, func: Callable, arg_desc: Optional[dict[str, str]] = None):
"""Helper method that parses a function to extract the name, description, and args.
This is a helper function that automatically infers the name, description, and args of the tool from the
@@ -107,7 +107,7 @@ class Tool:
self.desc = self.desc or desc
self.args = self.args or args
self.arg_types = self.arg_types or arg_types
- self.has_kwargs = any([param.kind == param.VAR_KEYWORD for param in sig.parameters.values()])
+ self.has_kwargs = any(param.kind == param.VAR_KEYWORD for param in sig.parameters.values())
def _validate_and_parse_args(self, **kwargs):
# Validate the args value comply to the json schema.