Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Format using Ruff instead of Black. #2988

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions .git-blame-ignore-revs
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
# sorting all imports with isort
933f77b96f0092e1baab4474a9208fc2e379aa32
# reformatting using ruff
1507cca4728ffcd9ded19c410bb134cd96df522a
5 changes: 1 addition & 4 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,10 @@ repos:
- id: check-case-conflict
- id: sort-simple-yaml
files: .pre-commit-config.yaml
- repo: https://github.com/psf/black-pre-commit-mirror
rev: 24.3.0
hooks:
- id: black
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.3.5
hooks:
- id: ruff-format
- id: ruff
types: [file]
types_or: [python, pyi, toml]
Expand Down
10 changes: 5 additions & 5 deletions check.sh
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ echo "::endgroup::"
# see https://forum.bors.tech/t/pre-test-and-pre-merge-hooks/322)
# autoflake --recursive --in-place .
# pyupgrade --py3-plus $(find . -name "*.py")
echo "::group::Black"
if ! black --check src/trio; then
echo "* Black found issues" >> "$GITHUB_STEP_SUMMARY"
echo "::group::Ruff-Format"
if ! ruff format --check src/trio; then
echo "* Ruff found issues" >> "$GITHUB_STEP_SUMMARY"
EXIT_STATUS=1
black --diff src/trio
ruff format --diff src/trio
echo "::endgroup::"
echo "::error:: Black found issues"
echo "::error:: Ruff found issues"
else
echo "::endgroup::"
fi
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ force-exclude = '''
ignore-words-list = 'astroid,crasher,asend'

[tool.ruff]
target-version = "py38"
Fuyukai marked this conversation as resolved.
Show resolved Hide resolved
respect-gitignore = true
fix = true

Expand Down
3 changes: 1 addition & 2 deletions src/trio/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
"""Trio - A friendly Python library for async concurrency and I/O
"""
"""Trio - A friendly Python library for async concurrency and I/O"""

from __future__ import annotations

Expand Down
3 changes: 2 additions & 1 deletion src/trio/_channel.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,8 @@ def _open_memory_channel(
# Need to use Tuple instead of tuple due to CI check running on 3.8
class open_memory_channel(Tuple["MemorySendChannel[T]", "MemoryReceiveChannel[T]"]):
def __new__( # type: ignore[misc] # "must return a subtype"
cls, max_buffer_size: int | float # noqa: PYI041
cls,
max_buffer_size: int | float, # noqa: PYI041
) -> tuple[MemorySendChannel[T], MemoryReceiveChannel[T]]:
return _open_memory_channel(max_buffer_size)

Expand Down
5 changes: 4 additions & 1 deletion src/trio/_core/_parking_lot.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,10 @@ def unpark_all(self) -> list[Task]:

@_core.enable_ki_protection
def repark(
self, new_lot: ParkingLot, *, count: int | float = 1 # noqa: PYI041
self,
new_lot: ParkingLot,
*,
count: int | float = 1, # noqa: PYI041
) -> None:
"""Move parked tasks from one :class:`ParkingLot` object to another.

Expand Down
2 changes: 1 addition & 1 deletion src/trio/_core/_traps.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ async def permanently_detach_coroutine_object(


async def temporarily_detach_coroutine_object(
abort_func: Callable[[RaiseCancelT], Abort]
abort_func: Callable[[RaiseCancelT], Abort],
) -> Any:
"""Temporarily detach the current coroutine object from the Trio
scheduler.
Expand Down
2 changes: 1 addition & 1 deletion src/trio/_highlevel_open_tcp_stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ def reorder_for_rfc_6555_section_5_4(
str,
Any,
]
]
],
) -> None:
# RFC 6555 section 5.4 says that if getaddrinfo returns multiple address
# families (e.g. IPv4 and IPv6), then you should make sure that your first
Expand Down
2 changes: 1 addition & 1 deletion src/trio/_path.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@


def _wraps_async(
wrapped: Callable[..., Any]
wrapped: Callable[..., Any],
) -> Callable[[Callable[P, T]], Callable[P, Awaitable[T]]]:
def decorator(fn: Callable[P, T]) -> Callable[P, Awaitable[T]]:
async def wrapper(*args: P.args, **kwargs: P.kwargs) -> T:
Expand Down
1 change: 1 addition & 0 deletions src/trio/_tests/check_type_completeness.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

If this check is giving you false alarms, you can ignore them by adding logic to `has_docstring_at_runtime`, in the main loop in `check_type`, or by updating the json file.
"""

from __future__ import annotations

# this file is not run as part of the tests, instead it's run standalone from check.sh
Expand Down
4 changes: 3 additions & 1 deletion src/trio/_tests/test_highlevel_open_tcp_stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,9 @@ def socket(
self.socket_count += 1
return FakeSocket(self, family, type_, proto)

def _ip_to_gai_entry(self, ip: str) -> tuple[
def _ip_to_gai_entry(
self, ip: str
) -> tuple[
AddressFamily,
SocketKind,
int,
Expand Down
2 changes: 1 addition & 1 deletion src/trio/_tests/test_socket.py
Original file line number Diff line number Diff line change
Expand Up @@ -587,7 +587,7 @@ async def res(
| tuple[str, str]
| tuple[str, str, int]
| tuple[str, str, int, int]
)
),
) -> Any:
return await sock._resolve_address_nocp(
args,
Expand Down
28 changes: 18 additions & 10 deletions src/trio/_tests/test_ssl.py
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,9 @@
MemoryStapledStream: TypeAlias = StapledStream[MemorySendStream, MemoryReceiveStream]


def ssl_memory_stream_pair(client_ctx: SSLContext, **kwargs: Any) -> tuple[
def ssl_memory_stream_pair(

Check warning on line 412 in src/trio/_tests/test_ssl.py

View check run for this annotation

Codecov / codecov/patch

src/trio/_tests/test_ssl.py#L412

Added line #L412 was not covered by tests
client_ctx: SSLContext, **kwargs: Any
) -> tuple[
SSLStream[MemoryStapledStream],
SSLStream[MemoryStapledStream],
]:
Expand All @@ -420,7 +422,9 @@
MyStapledStream: TypeAlias = StapledStream[SendStream, ReceiveStream]


def ssl_lockstep_stream_pair(client_ctx: SSLContext, **kwargs: Any) -> tuple[
def ssl_lockstep_stream_pair(

Check warning on line 425 in src/trio/_tests/test_ssl.py

View check run for this annotation

Codecov / codecov/patch

src/trio/_tests/test_ssl.py#L425

Added line #L425 was not covered by tests
client_ctx: SSLContext, **kwargs: Any
) -> tuple[
SSLStream[MyStapledStream],
SSLStream[MyStapledStream],
]:
Expand Down Expand Up @@ -837,20 +841,24 @@
async def test_SSLStream_generic(
client_ctx: SSLContext, https_compatible: bool
) -> None:
async def stream_maker() -> tuple[
SSLStream[MemoryStapledStream],
SSLStream[MemoryStapledStream],
]:
async def stream_maker() -> (

Check warning on line 844 in src/trio/_tests/test_ssl.py

View check run for this annotation

Codecov / codecov/patch

src/trio/_tests/test_ssl.py#L844

Added line #L844 was not covered by tests
tuple[
SSLStream[MemoryStapledStream],
SSLStream[MemoryStapledStream],
]
):
return ssl_memory_stream_pair(
client_ctx,
client_kwargs={"https_compatible": https_compatible},
server_kwargs={"https_compatible": https_compatible},
)

async def clogged_stream_maker() -> tuple[
SSLStream[MyStapledStream],
SSLStream[MyStapledStream],
]:
async def clogged_stream_maker() -> (

Check warning on line 856 in src/trio/_tests/test_ssl.py

View check run for this annotation

Codecov / codecov/patch

src/trio/_tests/test_ssl.py#L856

Added line #L856 was not covered by tests
tuple[
SSLStream[MyStapledStream],
SSLStream[MyStapledStream],
]
):
client, server = ssl_lockstep_stream_pair(client_ctx)
# If we don't do handshakes up front, then we run into a problem in
# the following situation:
Expand Down
20 changes: 12 additions & 8 deletions src/trio/_tests/test_testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -602,10 +602,12 @@ async def one_way_stream_maker() -> tuple[MemorySendStream, MemoryReceiveStream]

await check_one_way_stream(one_way_stream_maker, None)

async def half_closeable_stream_maker() -> tuple[
StapledStream[MemorySendStream, MemoryReceiveStream],
StapledStream[MemorySendStream, MemoryReceiveStream],
]:
async def half_closeable_stream_maker() -> (
tuple[
StapledStream[MemorySendStream, MemoryReceiveStream],
StapledStream[MemorySendStream, MemoryReceiveStream],
]
):
return memory_stream_pair()

await check_half_closeable_stream(half_closeable_stream_maker, None)
Expand All @@ -617,10 +619,12 @@ async def one_way_stream_maker() -> tuple[SendStream, ReceiveStream]:

await check_one_way_stream(one_way_stream_maker, one_way_stream_maker)

async def two_way_stream_maker() -> tuple[
StapledStream[SendStream, ReceiveStream],
StapledStream[SendStream, ReceiveStream],
]:
async def two_way_stream_maker() -> (
tuple[
StapledStream[SendStream, ReceiveStream],
StapledStream[SendStream, ReceiveStream],
]
):
return lockstep_stream_pair()

await check_two_way_stream(two_way_stream_maker, two_way_stream_maker)
Expand Down
25 changes: 0 additions & 25 deletions src/trio/_tests/tools/test_gen_exports.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
create_passthrough_args,
get_public_methods,
process,
run_black,
run_linters,
run_ruff,
)
Expand Down Expand Up @@ -93,12 +92,6 @@ def test_create_pass_through_args() -> None:
@skip_lints
@pytest.mark.parametrize("imports", [IMPORT_1, IMPORT_2, IMPORT_3])
def test_process(tmp_path: Path, imports: str) -> None:
try:
import black # noqa: F401
# there's no dedicated CI run that has astor+isort, but lacks black.
except ImportError as error: # pragma: no cover
skip_if_optional_else_raise(error)

modpath = tmp_path / "_module.py"
genpath = tmp_path / "_generated_module.py"
modpath.write_text(SOURCE, encoding="utf-8")
Expand All @@ -123,23 +116,6 @@ def test_process(tmp_path: Path, imports: str) -> None:
assert excinfo.value.code == 1


@skip_lints
def test_run_black(tmp_path: Path) -> None:
"""Test that processing properly fails if black does."""
try:
import black # noqa: F401
except ImportError as error: # pragma: no cover
skip_if_optional_else_raise(error)

file = File(tmp_path / "module.py", "module")

success, _ = run_black(file, "class not valid code ><")
assert not success

success, _ = run_black(file, "import waffle\n;import trio")
assert not success


@skip_lints
def test_run_ruff(tmp_path: Path) -> None:
"""Test that processing properly fails if ruff does."""
Expand Down Expand Up @@ -170,7 +146,6 @@ def test_run_ruff(tmp_path: Path) -> None:
def test_lint_failure(tmp_path: Path) -> None:
"""Test that processing properly fails if black or ruff does."""
Fuyukai marked this conversation as resolved.
Show resolved Hide resolved
try:
import black # noqa: F401
import ruff # noqa: F401
except ImportError as error: # pragma: no cover
skip_if_optional_else_raise(error)
Expand Down
6 changes: 4 additions & 2 deletions src/trio/_tests/type_tests/raisesgroup.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ def check_inheritance_and_assignments() -> None:
# https://github.com/agronholm/exceptiongroup/pull/101
# once fixed we'll get errors for unnecessary-pyright-ignore and can clean up
a = BaseExceptionGroup(
"", (BaseExceptionGroup("", (ValueError(),)),) # pyright: ignore
"",
(BaseExceptionGroup("", (ValueError(),)),), # pyright: ignore
)
assert a

Expand Down Expand Up @@ -129,7 +130,8 @@ def check_nested_raisesgroups_matches() -> None:
# https://github.com/agronholm/exceptiongroup/pull/101
# once fixed we'll get errors for unnecessary-pyright-ignore and can clean up
exc: ExceptionGroup[ExceptionGroup[ValueError]] = ExceptionGroup(
"", (ExceptionGroup("", (ValueError(),)),) # pyright: ignore
"",
(ExceptionGroup("", (ValueError(),)),), # pyright: ignore
)
# has the same problems as check_nested_raisesgroups_contextmanager
if RaisesGroup(RaisesGroup(ValueError)).matches(exc):
Expand Down
6 changes: 3 additions & 3 deletions src/trio/_threads.py
Original file line number Diff line number Diff line change
Expand Up @@ -464,9 +464,9 @@ def abort(raise_cancel: RaiseCancelT) -> trio.lowlevel.Abort:

while True:
# wait_task_rescheduled return value cannot be typed
msg_from_thread: outcome.Outcome[RetT] | Run[object] | RunSync[object] = (
await trio.lowlevel.wait_task_rescheduled(abort)
)
msg_from_thread: (
outcome.Outcome[RetT] | Run[object] | RunSync[object]
) = await trio.lowlevel.wait_task_rescheduled(abort)
if isinstance(msg_from_thread, outcome.Outcome):
return msg_from_thread.unwrap()
elif isinstance(msg_from_thread, Run):
Expand Down
41 changes: 3 additions & 38 deletions src/trio/_tools/gen_exports.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
Code generation script for class methods
to be exported as public API
"""

from __future__ import annotations

import argparse
Expand Down Expand Up @@ -103,37 +104,6 @@
return "({})".format(", ".join(call_args))


def run_black(file: File, source: str) -> tuple[bool, str]:
"""Run black on the specified file.

Returns:
Tuple of success and result string.
ex.:
(False, "Failed to run black!\nerror: cannot format ...")
(True, "<formatted source>")

Raises:
ImportError: If black is not installed.
"""
# imported to check that `subprocess` calls will succeed
import black # noqa: F401

# Black has an undocumented API, but it doesn't easily allow reading configuration from
# pyproject.toml, and simultaneously pass in / receive the code as a string.
# https://github.com/psf/black/issues/779
result = subprocess.run(
# "-" as a filename = use stdin, return on stdout.
[sys.executable, "-m", "black", "--stdin-filename", file.path, "-"],
input=source,
capture_output=True,
encoding="utf8",
)

if result.returncode != 0:
return False, f"Failed to run black!\n{result.stderr}"
return True, result.stdout


def run_ruff(file: File, source: str) -> tuple[bool, str]:
"""Run ruff on the specified file.

Expand Down Expand Up @@ -174,7 +144,7 @@


def run_linters(file: File, source: str) -> str:
"""Format the specified file using black and ruff.
"""Format the specified file using ruff.
Fuyukai marked this conversation as resolved.
Show resolved Hide resolved

Returns:
Formatted source code.
Expand All @@ -184,12 +154,7 @@
SystemExit: If either failed.
"""
Fuyukai marked this conversation as resolved.
Show resolved Hide resolved

success, response = run_black(file, source)
if not success:
print(response)
sys.exit(1)

success, response = run_ruff(file, response)
success, response = run_ruff(file, source)

Check warning on line 157 in src/trio/_tools/gen_exports.py

View check run for this annotation

Codecov / codecov/patch

src/trio/_tools/gen_exports.py#L157

Added line #L157 was not covered by tests
if not success: # pragma: no cover # Test for run_ruff should catch
print(response)
sys.exit(1)
Expand Down