Skip to content

armgit / dspy

Public Imported from GitHub
0

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

Python

Standardize coding style in DSPy (#7885)

Chen Qiana304b1311d3c
diff --git a/.github/workflows/run_tests.yml b/.github/workflows/run_tests.yml
index 451ff2571..640802a56 100644
--- a/.github/workflows/run_tests.yml
+++ b/.github/workflows/run_tests.yml
@@ -31,19 +31,10 @@ jobs:
uv venv .venv
echo "${{ github.workspace }}/.venv/bin" >> $GITHUB_PATH
- name: Install dependencies
- run: uv sync --dev -p .venv
+ run: uv sync --dev -p .venv --extra dev
- name: Ruff Fix Attempt
id: ruff_fix
- uses: chartboost/ruff-action@v1
- with:
- args: check --fix-only --diff --exit-non-zero-on-fix
- continue-on-error: true
- - name: Fail Workflow if Ruff Fix Failed
- if: steps.ruff_fix.outcome == 'failure'
- run: |
- echo "Ruff fix failed, failing the workflow."
- echo "Please run 'ruff check . --fix-only' locally and push the changes."
- exit 1
+ run: ruff check --fix-only --diff --exit-non-zero-on-fix
test:
name: Run Tests
diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml
index 0c43f5121..88aa349f2 100644
--- a/.pre-commit-config.yaml
+++ b/.pre-commit-config.yaml
@@ -2,65 +2,27 @@ default_language_version:
python: python3.9
default_stages: [pre-commit]
-default_install_hook_types: [pre-commit, commit-msg]
+default_install_hook_types: [pre-commit]
repos:
- - repo: https://github.com/astral-sh/ruff-pre-commit
- rev: v0.1.11
- hooks:
- - id: ruff
- args: [--fix]
- - id: ruff-format
-
- - repo: https://github.com/timothycrosley/isort
- rev: 5.12.0
+ - repo: local
hooks:
- - id: isort
- args:
- [
- "--profile=black",
- "--py=39",
- "--line-length=120",
- "--multi-line=3",
- "--trailing-comma",
- "--force-grid-wrap=0",
- "--use-parentheses",
- "--ensure-newline-before-comments",
- "--project=CORE,src,config,preprocess,train,transform,main,model",
- ]
+ - id: ruff-check
+ name: ruff (lint)
+ entry: ruff
+ language: system
+ types_or: [python, pyi]
+ files: ^(dspy|tests)/.*\.py$
+ exclude: ^(dspy/__metadata__\.py|dspy/retrieve/.*\.py|tests/reliability/.*\.py|tests/retrieve/.*\.py)$
+ args: [check, --fix-only]
- repo: https://github.com/pre-commit/pre-commit-hooks
- rev: v4.1.0
+ rev: v5.0.0
hooks:
- id: check-yaml
args: ["--allow-multiple-documents", "--unsafe"]
- - id: end-of-file-fixer
- - id: trailing-whitespace
- - id: check-docstring-first
- id: check-toml
- id: check-added-large-files
args: ["--maxkb=1024"]
- - id: requirements-txt-fixer
- id: check-merge-conflict
- id: debug-statements
- - id: pretty-format-json
- args:
- - "--autofix"
- - "--indent=2"
-
- - repo: local
- hooks:
- - id: validate-commit-msg
- name: Commit Message is Valid
- language: pygrep
- entry: ^(break|build|ci|docs|feat|fix|perf|refactor|style|test|ops|hotfix|release|maint|init|enh|revert)\([\w,\.,\-,\(,\),\/]+\)(!?)(:)\s{1}([\w,\W,:]+)
- stages: [commit-msg]
- args: [--negate]
-
- - repo: https://github.com/pre-commit/mirrors-prettier
- rev: v3.0.3
- hooks:
- - id: prettier
- additional_dependencies:
- - "@prettier/[email protected]"
diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md
index 0199e4678..5e1d02ad2 100644
--- a/CONTRIBUTING.md
+++ b/CONTRIBUTING.md
@@ -35,17 +35,55 @@ For minor changes (simple bug fixes or documentation fixes), feel free to open a
To make code changes, fork the repository and set up your local development environment following the
instructions in the "Environment Setup" section below.
-### Step 3. Create a Pull Request
+### Step 3 Commit Your Code and Run Autoformatting
+
+We follow the [Google Python Style Guide](https://google.github.io/styleguide/pyguide.html) and use `ruff` for both linting and formatting. To ensure consistent code quality, we use pre-commit hooks that automatically check and fix common issues.
+
+
+First you need to set up the pre-commit hooks (do this once after cloning the repository):
+
+```shell
+pre-commit install
+```
+
+Then stage and commit your changes. When you run `git commit`, the pre-commit hook will be
+automatically run.
+
+```shell
+git add .
+git commit -m "your commit message"
+```
+
+If the hooks make any changes, you'll need to stage and commit those changes as well.
+
+You can also run the hooks manually:
+
+- Check staged files only:
+
+ ```shell
+ pre-commit run
+ ```
+
+- Check specific files:
+
+ ```shell
+ pre-commit run --files path/to/file1.py path/to/file2.py
+ ```
+
+Please ensure all pre-commit checks pass before creating your pull request. If you're unsure about any
+formatting issues, feel free to commit your changes and let the pre-commit hooks fix them automatically.
+
+### Step 4. Create a Pull Request
Once your changes are ready, open a pull request from your branch in your fork to the main branch in the
[DSPy repo](https://github.com/stanfordnlp/dspy).
-### Step 4. Code Review
+### Step 5. Code Review
Once your PR is up and passes all CI tests, we will assign reviewers to review the code. There may be
several rounds of comments and code changes before the pull request gets approved by the reviewer.
-### Step 5. Merging
+### Step 6. Merging
Once the pull request is approved, a team member will take care of merging.
diff --git a/dspy/signatures/signature.py b/dspy/signatures/signature.py
index c25f4e7e0..b47d63f7b 100644
--- a/dspy/signatures/signature.py
+++ b/dspy/signatures/signature.py
@@ -41,7 +41,7 @@ class SignatureMeta(type(BaseModel)):
def __call__(cls, *args, **kwargs):
if cls is Signature:
# We don't create an actual Signature instance, instead, we create a new Signature class.
- custom_types = kwargs.pop('custom_types', None)
+ custom_types = kwargs.pop("custom_types", None)
if custom_types is None and args and isinstance(args[0], str):
custom_types = cls._detect_custom_types_from_caller(args[0])
@@ -52,19 +52,19 @@ class SignatureMeta(type(BaseModel)):
@staticmethod
def _detect_custom_types_from_caller(signature_str):
"""Detect custom types from the caller's frame based on the signature string.
-
+
Note: This method relies on Python's frame introspection which has some limitations:
1. May not work in all Python implementations (e.g., compiled with optimizations)
2. Looks up a limited number of frames in the call stack
3. Cannot find types that are imported but not in the caller's namespace
-
- For more reliable custom type resolution, explicitly provide types using the
+
+ For more reliable custom type resolution, explicitly provide types using the
`custom_types` parameter when creating a Signature.
"""
# Extract potential type names from the signature string, including dotted names
# Match both simple types like 'MyType' and dotted names like 'Module.Type'
- type_pattern = r':\s*([A-Za-z_][A-Za-z0-9_]*(?:\.[A-Za-z_][A-Za-z0-9_]*)*)'
+ type_pattern = r":\s*([A-Za-z_][A-Za-z0-9_]*(?:\.[A-Za-z_][A-Za-z0-9_]*)*)"
type_names = re.findall(type_pattern, signature_str)
if not type_names:
return None
@@ -76,7 +76,7 @@ class SignatureMeta(type(BaseModel)):
dotted_types = {}
for type_name in type_names:
- parts = type_name.split('.')
+ parts = type_name.split(".")
base_name = parts[0]
if base_name not in typing.__dict__ and base_name not in __builtins__:
@@ -115,6 +115,7 @@ class SignatureMeta(type(BaseModel)):
if needed_types and frame_count >= max_frames:
import logging
+
logging.getLogger("dspy").warning(
f"Reached maximum frame search depth ({max_frames}) while looking for types: {needed_types}. "
"Consider providing custom_types explicitly to Signature."
@@ -122,6 +123,7 @@ class SignatureMeta(type(BaseModel)):
except (AttributeError, ValueError):
# Handle environments where frame introspection is not available
import logging
+
logging.getLogger("dspy").debug(
"Frame introspection failed while trying to resolve custom types. "
"Consider providing custom_types explicitly to Signature."
@@ -395,11 +397,11 @@ def make_signature(
"question": (str, InputField()),
"answer": (str, OutputField())
})
-
+
# Using custom types
class MyType:
pass
-
+
sig3 = make_signature("input: MyType -> output", custom_types={"MyType": MyType})
```
"""
diff --git a/pyproject.toml b/pyproject.toml
index fd73fc6d7..94f739f86 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -113,27 +113,69 @@ exclude_lines = [
]
[tool.ruff]
+include = ["dspy/**/*.py", "tests/**/*.py"]
+exclude = [
+ "dspy/__metadata__.py",
+ "dspy/retrieve/*.py",
+ "tests/reliability/*.py",
+ "tests/retrieve/*.py",
+]
+
+
line-length = 120
indent-width = 4
target-version = "py39"
[tool.ruff.lint]
select = [
- "F", # Pyflakes
- "E", # Pycodestyle
- "TID252", # Absolute imports
+ "E", # pycodestyle errors
+ "W", # pycodestyle warnings
+ "F", # pyflakes
+ "I", # isort
+ "C", # flake8-comprehensions
+ "B", # flake8-bugbear
+ "UP", # pyupgrade
+ "N", # pep8-naming
+ "RUF", # ruff-specific rules
]
+
ignore = [
- "E501", # Line too long
+ "B027", # Allow non-abstract empty methods in abstract base classes
+ "FBT003",# Allow boolean positional values in function calls
+ "C901", # Ignore complexity checking
+ "E501", # Ignore line length errors (handled by formatter)
+ "UP006", # Allow python typing modules
+ "UP035", # Allow python typing modules
+ "RUF005", # Allow using + operator to concatenate collections
+ "B904", # Allow raise custom exceptions in except blocks
+ "F403", # Allow wildcard imports
+ "E721", # Allow using == to compare with type
+ "UP031", # Allow percent format
+ "RUF022", # Allow unsorted __all__ value
]
+
+# Allow fix for all enabled rules (when `--fix`) is provided.
fixable = ["ALL"]
unfixable = []
[tool.ruff.format]
docstring-code-format = false
+quote-style = "double"
indent-style = "space"
+skip-magic-trailing-comma = false
line-ending = "auto"
+[tool.ruff.lint.isort]
+known-first-party = ["dspy"]
+
+[tool.ruff.lint.flake8-tidy-imports]
+ban-relative-imports = "all"
+
[tool.ruff.lint.per-file-ignores]
-"**/{tests,testing,docs}/*" = ["ALL"]
-"**__init__.py" = ["ALL"]
+"tests/**/*.py" = [
+ "S101", # Allow assert statements in tests
+ "TID252", # Allow relative imports in tests
+ "ARG001", # Allow unused arguments in tests (like pytest fixtures)
+]
+"__init__.py" = ["F401"] # Init files can be empty
+"dspy/__init__.py" = ["I001", "E402", "F405"]
diff --git a/tests/datasets/test_dataset.py b/tests/datasets/test_dataset.py
index a038681bd..f9af8c1b8 100644
--- a/tests/datasets/test_dataset.py
+++ b/tests/datasets/test_dataset.py
@@ -14,8 +14,8 @@ dummy_data = """content,question,answer
class CSVDataset(Dataset):
- def __init__(self, file_path, input_keys=None, *args, **kwargs) -> None:
- super().__init__(input_keys=input_keys, *args, **kwargs)
+ def __init__(self, file_path, input_keys=None, **kwargs) -> None:
+ super().__init__(input_keys=input_keys, **kwargs)
df = pd.read_csv(file_path)
data = df.to_dict(orient="records")
self._train = [