From 2a92993821852300e671a5f87316014e29b6156b Mon Sep 17 00:00:00 2001 From: Mubashir78 Date: Thu, 2 Jul 2026 12:11:16 +0500 Subject: [PATCH] Add ruff linter, fix unused imports and dead variables - Add [tool.ruff] config to pyproject.toml (select F, I) - Remove unused Union, List, Dict imports from __init__.py - Remove unused Optional import from conftest.py - Remove unused result/param_types variables across app.py and tests - Add make lint target --- Makefile | 3 +++ app.py | 23 +++++++++++------------ demo.py | 7 ++++--- pyproject.toml | 7 +++++++ smartfunc/__init__.py | 5 +++-- tests/conftest.py | 2 +- tests/test_basic.py | 11 ++++++----- tests/test_messages.py | 5 +++-- 8 files changed, 38 insertions(+), 25 deletions(-) diff --git a/Makefile b/Makefile index 20563a1..7be6bad 100644 --- a/Makefile +++ b/Makefile @@ -10,6 +10,9 @@ pypi: clean check: clean uv run pytest tests +lint: + uv run ruff check + clean: rm -rf __pycache__ .pytest_cache dist diff --git a/app.py b/app.py index f3673e0..9477fc1 100644 --- a/app.py +++ b/app.py @@ -24,9 +24,10 @@ def _(rq): @app.cell(hide_code=True) def _(): + from typing import Literal + from instructor.batch import BatchJob from pydantic import BaseModel, Field - from typing import Literal class EmojiDescription(BaseModel): terms: list[str] = Field(..., description="List of words/phrases that could fit the emoji. List can be long, around 10 examples.") @@ -74,7 +75,7 @@ def _(): @app.cell def _(cache): - import polars as pl + import polars as pl from lazylines import LazyLines pl.DataFrame( @@ -92,10 +93,11 @@ def _(cache): @app.cell(hide_code=True) def _(mo): import asyncio - import random import logging + import random + from typing import Any, Callable, Dict, List, Optional + import tqdm - from typing import List, Dict, Any, Callable, Optional async def process_with_retry( func, @@ -279,12 +281,13 @@ async def main(): @app.cell def _(): import inspect - import json - import llm - from typing import TypeVar, get_type_hints + import json from functools import wraps - from jinja2 import Template + from typing import TypeVar, get_type_hints + + import llm from diskcache import Cache + from jinja2 import Template return Cache, Template, TypeVar, get_type_hints, inspect, json, llm, wraps @@ -318,8 +321,6 @@ def wrapper(*args, **kwargs): if type_hints.get('return', None): assert issubclass(type_hints.get('return', None), BaseModel), "Output type must be Pydantic class" - # Create a dictionary of parameter types - param_types = {name: param.default for name, param in signature.parameters.items()} bound_args = signature.bind(*args, **kwargs) bound_args.apply_defaults() # Apply default values for missing parameters all_kwargs = bound_args.arguments @@ -383,8 +384,6 @@ async def wrapper(*args, **kwargs): if type_hints.get('return', None): assert issubclass(type_hints.get('return', None), BaseModel), "Output type must be Pydantic class" - # Create a dictionary of parameter types - param_types = {name: param.default for name, param in signature.parameters.items()} bound_args = signature.bind(*args, **kwargs) bound_args.apply_defaults() # Apply default values for missing parameters all_kwargs = bound_args.arguments diff --git a/demo.py b/demo.py index ffbe189..b3186f7 100644 --- a/demo.py +++ b/demo.py @@ -20,10 +20,11 @@ def _(mo): @app.cell def _(): - from smartfunc import backend - from openai import OpenAI from dotenv import load_dotenv - from pydantic import BaseModel + from openai import OpenAI + from pydantic import BaseModel + + from smartfunc import backend load_dotenv(".env") diff --git a/pyproject.toml b/pyproject.toml index b415e12..3276e73 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -12,6 +12,13 @@ dependencies = [ requires = ["hatchling"] build-backend = "hatchling.build" +[tool.ruff] +target-version = "py310" +line-length = 88 + +[tool.ruff.lint] +select = ["F", "I"] + [tool.pytest.ini_options] testpaths = ["tests"] python_files = ["test_*.py"] diff --git a/smartfunc/__init__.py b/smartfunc/__init__.py index 10167d2..c077385 100644 --- a/smartfunc/__init__.py +++ b/smartfunc/__init__.py @@ -1,7 +1,8 @@ from functools import wraps -from typing import Any, Callable, Optional, Type, Union, List, Dict +from typing import Any, Callable, Optional, Type + +from openai import AsyncOpenAI, OpenAI from pydantic import BaseModel -from openai import OpenAI, AsyncOpenAI def _disallow_additional_properties(schema: Any) -> Any: diff --git a/tests/conftest.py b/tests/conftest.py index 4518919..b01d10b 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,7 +1,7 @@ """Pytest fixtures and mock clients for smartfunc tests.""" + import pytest -from typing import Optional class MockMessage: diff --git a/tests/test_basic.py b/tests/test_basic.py index 6443f48..c1e9e43 100644 --- a/tests/test_basic.py +++ b/tests/test_basic.py @@ -1,6 +1,7 @@ import pytest from pydantic import BaseModel -from smartfunc import backend, async_backend + +from smartfunc import async_backend, backend class Summary(BaseModel): @@ -59,7 +60,7 @@ def test_system_prompt(mock_client_factory): def generate(prompt: str) -> str: return prompt - result = generate("test") + generate("test") assert len(client.calls[0]["messages"]) == 2 assert client.calls[0]["messages"][0]["role"] == "system" @@ -75,7 +76,7 @@ def test_extra_kwargs(mock_client_factory): def generate(prompt: str) -> str: return prompt - result = generate("test") + generate("test") assert client.calls[0]["temperature"] == 0.7 assert client.calls[0]["max_tokens"] == 100 @@ -166,7 +167,7 @@ def test_multiple_arguments(mock_client_factory): def generate(topic: str, style: str, length: int) -> str: return f"Write a {length} word {style} piece about {topic}" - result = generate("AI", "formal", 500) + generate("AI", "formal", 500) content = client.calls[0]["messages"][0]["content"] assert "Write a 500 word formal piece about AI" in content @@ -187,7 +188,7 @@ def smart_generate(items: list[str], include_summary: bool) -> str: return prompt - result = smart_generate(["apple", "banana"], True) + smart_generate(["apple", "banana"], True) content = client.calls[0]["messages"][0]["content"] assert "1. apple" in content diff --git a/tests/test_messages.py b/tests/test_messages.py index 327d332..139540d 100644 --- a/tests/test_messages.py +++ b/tests/test_messages.py @@ -1,6 +1,7 @@ import pytest from pydantic import BaseModel -from smartfunc import backend, async_backend + +from smartfunc import async_backend, backend class Summary(BaseModel): @@ -46,7 +47,7 @@ def chat() -> list: {"role": "user", "content": "Hello"}, ] - result = chat() + chat() messages = client.calls[0]["messages"] assert len(messages) == 2