Skip to content

Commit

Permalink
chore: enable preview rules from ruff (#3663)
Browse files Browse the repository at this point in the history
Enable `preview = true` to get some more helpful rules from ruff.
  • Loading branch information
mscolnick authored Feb 3, 2025
1 parent ebd8d84 commit 9acad2c
Show file tree
Hide file tree
Showing 14 changed files with 36 additions and 33 deletions.
2 changes: 1 addition & 1 deletion marimo/_ast/pytest.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def wrap_fn_for_pytest(
ast.arg(arg, lineno=args[arg].lineno, col_offset=args[arg].col_offset)
for arg in fixtures
]
(call_stub,) = call.keywords
(_call_stub,) = call.keywords
call.keywords = [
ast.keyword(
arg=arg,
Expand Down
10 changes: 6 additions & 4 deletions marimo/_cli/file_path.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

import abc
import json
import logging
import os
import pathlib
import urllib.parse
Expand All @@ -12,10 +11,13 @@
from typing import Optional, Tuple
from urllib.error import HTTPError

from marimo import _loggers
from marimo._cli.print import green
from marimo._utils.marimo_path import MarimoPath
from marimo._utils.url import is_url

LOGGER = _loggers.marimo_logger()


def is_github_src(url: str, ext: str) -> bool:
if not is_url(url):
Expand Down Expand Up @@ -99,7 +101,7 @@ def read(self, name: str) -> Tuple[str, str]:
@staticmethod
def _is_static_marimo_notebook_url(url: str) -> tuple[bool, str]:
def download(url: str) -> tuple[bool, str]:
logging.info("Downloading %s", url)
LOGGER.info("Downloading %s", url)
request = urllib.request.Request(
url,
# User agent to avoid 403 Forbidden some bot protection
Expand Down Expand Up @@ -278,11 +280,11 @@ def handle(
def _create_tmp_file_from_content(
content: str, name: str, temp_dir: TemporaryDirectory[str]
) -> str:
logging.info("Creating temporary file")
LOGGER.info("Creating temporary file")
path_to_app = os.path.join(temp_dir.name, name)
with open(path_to_app, "w") as f:
f.write(content)
logging.info("App saved to %s", path_to_app)
LOGGER.info("App saved to %s", path_to_app)
return path_to_app


Expand Down
4 changes: 2 additions & 2 deletions marimo/_output/formatting.py
Original file line number Diff line number Diff line change
Expand Up @@ -325,14 +325,14 @@ def plain(value: Any) -> Plain:
return Plain(value)


@dataclass
class Plain:
"""
Wrapper around a value to indicate that it should be displayed
without any opinionated formatting.
"""

def __init__(self, child: Any):
self.child = child
child: Any


@mddoc
Expand Down
7 changes: 4 additions & 3 deletions marimo/_plugins/ui/_impl/tables/format.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@

from typing import Callable, Dict, List, Union

from marimo import _loggers
from marimo._plugins.core.web_component import JSONType

LOGGER = _loggers.marimo_logger()

FormatMapping = Dict[str, Union[str, Callable[..., JSONType]]]


Expand Down Expand Up @@ -38,9 +41,7 @@ def format_value(
if callable(formatter):
return formatter(value)
except Exception as e:
import logging

logging.warning(
LOGGER.warning(
f"Error formatting for value {value} in column {col}: {str(e)}"
)
return value
Expand Down
2 changes: 1 addition & 1 deletion marimo/_save/ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def __init__(self, line: int, *arg: Any, **kwargs: Any) -> None:
super().__init__(*arg, **kwargs)
self.target_line = line

def generic_visit(self, node: ast.AST) -> tuple[ast.Module, ast.Module]: # type: ignore[override]
def generic_visit(self, node: ast.AST) -> tuple[ast.Module, ast.Module]: # type: ignore[override]
pre_block = []

# There are a few strange edges cases like:
Expand Down
2 changes: 1 addition & 1 deletion marimo/_save/hash.py
Original file line number Diff line number Diff line change
Expand Up @@ -968,7 +968,7 @@ def content_cache_attempt_from_base(
refs, scope, ctx
)

refs, content, tmp_stateful_refs = hasher.collect_for_content_hash(
refs, _content, tmp_stateful_refs = hasher.collect_for_content_hash(
refs, scope, ctx, scoped_refs, apply_hash=True
)
# If the execution block covers this variable, then that's OK
Expand Down
4 changes: 2 additions & 2 deletions marimo/_server/api/endpoints/terminal.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ async def websocket_endpoint(websocket: WebSocket) -> None:
child_pid, fd = pty.fork()
if child_pid == 0:
default_shell = os.environ.get("SHELL", "/bin/bash")
subprocess.run([default_shell], shell=True) ## noqa: ASYNC221
subprocess.run([default_shell], shell=True) # noqa: ASYNC221
return

reader_task = asyncio.create_task(_read_from_pty(fd, websocket))
Expand All @@ -118,5 +118,5 @@ async def websocket_endpoint(websocket: WebSocket) -> None:
if writer_task and not writer_task.done():
writer_task.cancel()
os.kill(child_pid, signal.SIGKILL)
os.waitpid(child_pid, 0) ## noqa: ASYNC222
os.waitpid(child_pid, 0) # noqa: ASYNC222
LOGGER.debug("Terminal websocket closed")
13 changes: 6 additions & 7 deletions marimo/_server/api/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from __future__ import annotations

import asyncio
from dataclasses import dataclass
from http.client import HTTPResponse, HTTPSConnection
from typing import (
TYPE_CHECKING,
Expand Down Expand Up @@ -162,14 +163,12 @@ async def dispatch(
return response


@dataclass
class _URLRequest:
def __init__(
self, url: str, method: str, headers: dict[str, str], data: Any
):
self.full_url = url
self.method = method
self.headers = headers
self.data = data
full_url: str
method: str
headers: dict[str, str]
data: Any


class _AsyncHTTPResponse:
Expand Down
2 changes: 1 addition & 1 deletion marimo/_server/export/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

def format_filename_title(filename: str) -> str:
basename = os.path.basename(filename)
name, ext = os.path.splitext(basename)
name, _ext = os.path.splitext(basename)
title = re.sub("[-_]", " ", name)
return title.title()

Expand Down
5 changes: 4 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -304,9 +304,11 @@ exclude = [
]

[tool.ruff.lint]
preview = true
ignore = [
"G004", # Logging statement uses f-string
"TC001", # Move application import into a type-checking block
"TC006", # Add quotes to type expression in typing.cast()
"D301", # Use r""" if any backslashes in a docstring
"PERF203", # try-except within a loop incurs performance overhead; not always possible
"PERF401", # Use {message_str} to create a transformed list; at the cost of readability
Expand All @@ -331,6 +333,7 @@ extend-select = [
"ARG",
# flake8-bugbear
"B",
# Performance
"PERF",
"ANN002", # missing-type-args
"ANN003", # missing-type-kwargs
Expand Down Expand Up @@ -386,7 +389,7 @@ unfixable = ["F401"]
convention = "google"

[tool.ruff.lint.per-file-ignores]
"**/{tests}/*" = ["ANN201", "ANN202", "T201", "D"]
"**/{tests}/*" = ["ANN201", "ANN202", "T201", "D", "F841"]
"dagger/*" = ["TID252"]

[tool.ruff.lint.isort]
Expand Down
2 changes: 1 addition & 1 deletion tests/_output/test_md.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def test_md_iconify() -> None:

# Test multiple icons
multiple_icons_input = "Icons: ::mdi:home:: ::fa:car:: ::lucide:settings::"
expected_output = '<span class="paragraph">Icons: <iconify-icon icon="mdi:home" inline=""></iconify-icon> <iconify-icon icon="fa:car" inline=""></iconify-icon> <iconify-icon icon="lucide:settings" inline=""></iconify-icon></span>' ## noqa: E501
expected_output = '<span class="paragraph">Icons: <iconify-icon icon="mdi:home" inline=""></iconify-icon> <iconify-icon icon="fa:car" inline=""></iconify-icon> <iconify-icon icon="lucide:settings" inline=""></iconify-icon></span>' # noqa: E501
assert (
_md(multiple_icons_input, apply_markdown_class=False).text
== expected_output
Expand Down
4 changes: 1 addition & 3 deletions tests/_output/test_try_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,9 +160,7 @@ def test_plain_object():
result = try_format(obj)
# Should fall back to string representation since we opted out of opinionated formatter
assert is_html(result)
assert result.data.startswith(
"<pre style='font-size: 12px'>&lt;marimo._output.formatting.Plain object"
)
assert result.data.startswith("<pre style='font-size: 12px'>Plain(")


def test_opinionated_formatter():
Expand Down
8 changes: 4 additions & 4 deletions tests/_save/test_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def one() -> tuple[int]:
with persistent_cache(name="one",
_loader=MockLoader(
data={"X": 7, "Y": 8})
) as cache: # noqa: E501
) as cache: # noqa: E501
Y = 9
X = 10
# fmt: on
Expand Down Expand Up @@ -114,8 +114,8 @@ def one() -> tuple[int]:
# fmt: off
b = [2]
if True:
with persistent_cache("if", _loader=_loader):
b = [
with persistent_cache("if", _loader=_loader): # noqa: E111
b = [ # noqa: E111
7
]
# fmt: on
Expand Down Expand Up @@ -222,7 +222,7 @@ def call(v):

_loader = MockLoader()
# fmt: off
with persistent_cache("else", _loader=_loader): call(False) # noqa: E701
with persistent_cache("else", _loader=_loader): call(False) # noqa: E701
# fmt: on

with pytest.raises(BlockException):
Expand Down
4 changes: 2 additions & 2 deletions tests/_server/api/endpoints/test_ai.py
Original file line number Diff line number Diff line change
Expand Up @@ -477,9 +477,9 @@ def no_google_ai_config(config: UserConfigManager):

class TestStreamResponse(unittest.TestCase):
def simulate_stream(self, contents: List[str]) -> Any:
@dataclass
class MockContent:
def __init__(self, content: str) -> None:
self.content = content
content: str

class MockDelta:
def __init__(self, content: str) -> None:
Expand Down

0 comments on commit 9acad2c

Please sign in to comment.