diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index b533de5c..ae023079 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,13 +1,12 @@ exclude: ^python/tests/__snapshots__/ repos: - repo: https://github.com/astral-sh/ruff-pre-commit - rev: v0.11.2 + rev: v0.12.7 hooks: - - id: ruff - args: [--fix, --exit-non-zero-on-fix] + - id: ruff-check + args: [--fix] - id: ruff-format - repo: https://github.com/astral-sh/uv-pre-commit - # uv version. - rev: 0.6.11 + rev: 0.8.5 hooks: - id: uv-lock diff --git a/pyproject.toml b/pyproject.toml index 9e3f0403..ac8c3894 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -199,9 +199,11 @@ ignore = [ # allow blind exception to add context "BLE001", # Don't move type checking around so that can be accessed at runtime - "TCH001", - "TCH002", - "TCH003", + "TC001", + "TC002", + "TC003", + # allow eq without hash + "PLW1641", ] select = ["ALL"] diff --git a/python/egglog/__init__.py b/python/egglog/__init__.py index af64575d..6a6e2213 100644 --- a/python/egglog/__init__.py +++ b/python/egglog/__init__.py @@ -8,6 +8,6 @@ from .conversion import * from .deconstruct import * from .egraph import * -from .runtime import define_expr_method as define_expr_method # noqa: PLC0414 +from .runtime import define_expr_method as define_expr_method del ipython_magic diff --git a/python/egglog/egraph.py b/python/egglog/egraph.py index 3869bc31..3f9a3db9 100644 --- a/python/egglog/egraph.py +++ b/python/egglog/egraph.py @@ -688,7 +688,7 @@ def relation(name: str, /, *tps: type, egg_fn: str | None = None) -> Callable[.. def _relation_decls(name: str, tps: tuple[type, ...], egg_fn: str | None) -> Declarations: - from .builtins import Unit + from .builtins import Unit # noqa: PLC0415 decls = Declarations() decls |= cast("RuntimeClass", Unit) @@ -1086,9 +1086,9 @@ def display(self, graphviz: bool = False, **kwargs: Unpack[GraphvizKwargs]) -> N If in IPython it will display it inline, otherwise it will write it to a file and open it. """ - from IPython.display import SVG, display + from IPython.display import SVG, display # noqa: PLC0415 - from .visualizer_widget import VisualizerWidget + from .visualizer_widget import VisualizerWidget # noqa: PLC0415 if graphviz: if IN_IPYTHON: @@ -1115,7 +1115,7 @@ def saturate( If an `expr` is passed, it's also extracted after each run and printed """ - from .visualizer_widget import VisualizerWidget + from .visualizer_widget import VisualizerWidget # noqa: PLC0415 def to_json() -> str: if expr is not None: @@ -1608,7 +1608,7 @@ class _NeBuilder(Generic[BASE_EXPR]): lhs: BASE_EXPR def to(self, rhs: BASE_EXPR) -> Unit: - from .builtins import Unit + from .builtins import Unit # noqa: PLC0415 lhs = to_runtime_expr(self.lhs) rhs = convert_to_same_type(rhs, lhs) diff --git a/python/egglog/pretty.py b/python/egglog/pretty.py index 41659074..01beddc5 100644 --- a/python/egglog/pretty.py +++ b/python/egglog/pretty.py @@ -491,24 +491,3 @@ def _pretty_function_body(self, fn: UnnamedFunctionRef, args: list[ExprDecl]) -> if arg_names: prefix += f" {', '.join(self(a.expr) for a in arg_names)}" return f"{prefix}: {self(res.expr)}" - - -def _plot_line_length(expr: object): # pragma: no cover - """ - Plots the number of line lengths based on different max lengths - """ - global MAX_LINE_LENGTH, LINE_DIFFERENCE - import altair as alt - import pandas as pd - - sizes = [] - for line_length in range(40, 180, 10): - MAX_LINE_LENGTH = line_length - for diff in range(0, 40, 5): - LINE_DIFFERENCE = diff - new_l = len(str(expr).split()) - sizes.append((line_length, diff, new_l)) - - df = pd.DataFrame(sizes, columns=["MAX_LINE_LENGTH", "LENGTH_DIFFERENCE", "n"]) - - return alt.Chart(df).mark_rect().encode(x="MAX_LINE_LENGTH:O", y="LENGTH_DIFFERENCE:O", color="n:Q") diff --git a/python/egglog/runtime.py b/python/egglog/runtime.py index 02c7c2dd..26f8d9fa 100644 --- a/python/egglog/runtime.py +++ b/python/egglog/runtime.py @@ -387,7 +387,7 @@ def __egg_ref__(self) -> CallableRef: return self.__egg_ref_thunk__() def __call__(self, *args: object, _egg_partial_function: bool = False, **kwargs: object) -> RuntimeExpr | None: - from .conversion import resolve_literal + from .conversion import resolve_literal # noqa: PLC0415 if isinstance(self.__egg_bound__, RuntimeExpr): args = (self.__egg_bound__, *args) @@ -540,7 +540,7 @@ def __egg_pretty__(self, wrapping_fn: str | None) -> str: return pretty_decl(self.__egg_decls__, self.__egg_typed_expr__.expr, wrapping_fn=wrapping_fn) def _ipython_display_(self) -> None: - from IPython.display import Code, display + from IPython.display import Code, display # noqa: PLC0415 display(Code(str(self), language="python")) @@ -577,7 +577,7 @@ def __eq__(self, other: object) -> object: # type: ignore[override] # TODO: Check if two objects can be upcasted to be the same. If not, then return NotImplemented so other # expr gets a chance to resolve __eq__ which could be a preserved method. - from .egraph import BaseExpr, eq + from .egraph import BaseExpr, eq # noqa: PLC0415 return eq(cast("BaseExpr", self)).to(cast("BaseExpr", other)) @@ -585,7 +585,7 @@ def __ne__(self, other: object) -> object: # type: ignore[override] if (method := _get_expr_method(self, "__ne__")) is not None: return method(other) - from .egraph import BaseExpr, ne + from .egraph import BaseExpr, ne # noqa: PLC0415 return ne(cast("BaseExpr", self)).to(cast("BaseExpr", other)) @@ -647,7 +647,7 @@ def _numeric_binary_method(self: object, other: object, name: str = name, r_meth ) ) ): - from .conversion import CONVERSIONS, resolve_type, retrieve_conversion_decls + from .conversion import CONVERSIONS, resolve_type, retrieve_conversion_decls # noqa: PLC0415 # tuple of (cost, convert_self) best_method: ( diff --git a/python/egglog/version_compat.py b/python/egglog/version_compat.py index 352b2655..e29f4932 100644 --- a/python/egglog/version_compat.py +++ b/python/egglog/version_compat.py @@ -30,7 +30,7 @@ def _collect_type_vars_monkeypatch(types_, typevar_types=None): _collect_type_vars((T, List[S, T])) == (T, S) """ - from .runtime import RuntimeClass + from .runtime import RuntimeClass # noqa: PLC0415 if typevar_types is None: typevar_types = typing.TypeVar @@ -48,7 +48,7 @@ def _collect_type_vars_monkeypatch(types_, typevar_types=None): @typing.no_type_check @typing._tp_cache def __getitem__monkeypatch(self, params): # noqa: C901, PLR0912 - from .runtime import RuntimeClass + from .runtime import RuntimeClass # noqa: PLC0415 if self.__origin__ in (typing.Generic, typing.Protocol): # Can't subscript Generic[...] or Protocol[...]. diff --git a/python/tests/conftest.py b/python/tests/conftest.py index 73aed445..5ca02195 100644 --- a/python/tests/conftest.py +++ b/python/tests/conftest.py @@ -3,11 +3,11 @@ import pytest from syrupy.extensions.single_file import SingleFileSnapshotExtension +import egglog.conversion + @pytest.fixture(autouse=True) def _reset_conversions(): - import egglog.conversion - old_conversions = copy.copy(egglog.conversion.CONVERSIONS) old_conversion_decls = copy.copy(egglog.conversion._TO_PROCESS_DECLS) yield diff --git a/python/tests/test_unstable_fn.py b/python/tests/test_unstable_fn.py index 1a7742cd..eaaab64b 100644 --- a/python/tests/test_unstable_fn.py +++ b/python/tests/test_unstable_fn.py @@ -10,6 +10,7 @@ from typing import ClassVar, TypeAlias from egglog import * +from egglog.runtime import RuntimeFunction class Math(Expr): @@ -167,8 +168,6 @@ def __init__(self) -> None: ... def test_callable_accepted_as_type(): - from egglog.runtime import RuntimeFunction - @function def func(f: UnstableFn[C, A, B]) -> C: ...