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 all 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
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
6 changes: 3 additions & 3 deletions src/trio/_core/_generated_io_epoll.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
__all__ = ["notify_closing", "wait_readable", "wait_writable"]


async def wait_readable(fd: int | _HasFileNo) -> None:
async def wait_readable(fd: (int | _HasFileNo)) -> None:
"""Block until the kernel reports that the given object is readable.

On Unix systems, ``fd`` must either be an integer file descriptor,
Expand Down Expand Up @@ -47,7 +47,7 @@ async def wait_readable(fd: int | _HasFileNo) -> None:
raise RuntimeError("must be called from async context") from None


async def wait_writable(fd: int | _HasFileNo) -> None:
async def wait_writable(fd: (int | _HasFileNo)) -> None:
"""Block until the kernel reports that the given object is writable.

See `wait_readable` for the definition of ``fd``.
Expand All @@ -66,7 +66,7 @@ async def wait_writable(fd: int | _HasFileNo) -> None:
raise RuntimeError("must be called from async context") from None


def notify_closing(fd: int | _HasFileNo) -> None:
def notify_closing(fd: (int | _HasFileNo)) -> None:
"""Notify waiters of the given object that it will be closed.

Call this before closing a file descriptor (on Unix) or socket (on
Expand Down
6 changes: 3 additions & 3 deletions src/trio/_core/_generated_io_kqueue.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ async def wait_kevent(
raise RuntimeError("must be called from async context") from None


async def wait_readable(fd: int | _HasFileNo) -> None:
async def wait_readable(fd: (int | _HasFileNo)) -> None:
"""Block until the kernel reports that the given object is readable.

On Unix systems, ``fd`` must either be an integer file descriptor,
Expand Down Expand Up @@ -100,7 +100,7 @@ async def wait_readable(fd: int | _HasFileNo) -> None:
raise RuntimeError("must be called from async context") from None


async def wait_writable(fd: int | _HasFileNo) -> None:
async def wait_writable(fd: (int | _HasFileNo)) -> None:
"""Block until the kernel reports that the given object is writable.

See `wait_readable` for the definition of ``fd``.
Expand All @@ -119,7 +119,7 @@ async def wait_writable(fd: int | _HasFileNo) -> None:
raise RuntimeError("must be called from async context") from None


def notify_closing(fd: int | _HasFileNo) -> None:
def notify_closing(fd: (int | _HasFileNo)) -> None:
"""Notify waiters of the given object that it will be closed.

Call this before closing a file descriptor (on Unix) or socket (on
Expand Down
16 changes: 9 additions & 7 deletions src/trio/_core/_generated_io_windows.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
]


async def wait_readable(sock: _HasFileNo | int) -> None:
async def wait_readable(sock: (_HasFileNo | int)) -> None:
"""Block until the kernel reports that the given object is readable.

On Unix systems, ``sock`` must either be an integer file descriptor,
Expand Down Expand Up @@ -61,7 +61,7 @@ async def wait_readable(sock: _HasFileNo | int) -> None:
raise RuntimeError("must be called from async context") from None


async def wait_writable(sock: _HasFileNo | int) -> None:
async def wait_writable(sock: (_HasFileNo | int)) -> None:
"""Block until the kernel reports that the given object is writable.

See `wait_readable` for the definition of ``sock``.
Expand All @@ -80,7 +80,7 @@ async def wait_writable(sock: _HasFileNo | int) -> None:
raise RuntimeError("must be called from async context") from None


def notify_closing(handle: Handle | int | _HasFileNo) -> None:
def notify_closing(handle: (Handle | int | _HasFileNo)) -> None:
"""Notify waiters of the given object that it will be closed.

Call this before closing a file descriptor (on Unix) or socket (on
Expand Down Expand Up @@ -112,7 +112,7 @@ def notify_closing(handle: Handle | int | _HasFileNo) -> None:
raise RuntimeError("must be called from async context") from None


def register_with_iocp(handle: int | CData) -> None:
def register_with_iocp(handle: (int | CData)) -> None:
"""TODO: these are implemented, but are currently more of a sketch than
anything real. See `#26
<https://github.com/python-trio/trio/issues/26>`__ and `#52
Expand All @@ -125,7 +125,9 @@ def register_with_iocp(handle: int | CData) -> None:
raise RuntimeError("must be called from async context") from None


async def wait_overlapped(handle_: int | CData, lpOverlapped: CData | int) -> object:
async def wait_overlapped(
handle_: (int | CData), lpOverlapped: (CData | int)
) -> object:
"""TODO: these are implemented, but are currently more of a sketch than
anything real. See `#26
<https://github.com/python-trio/trio/issues/26>`__ and `#52
Expand All @@ -141,7 +143,7 @@ async def wait_overlapped(handle_: int | CData, lpOverlapped: CData | int) -> ob


async def write_overlapped(
handle: int | CData, data: Buffer, file_offset: int = 0
handle: (int | CData), data: Buffer, file_offset: int = 0
) -> int:
"""TODO: these are implemented, but are currently more of a sketch than
anything real. See `#26
Expand All @@ -158,7 +160,7 @@ async def write_overlapped(


async def readinto_overlapped(
handle: int | CData, buffer: Buffer, file_offset: int = 0
handle: (int | CData), buffer: Buffer, file_offset: int = 0
) -> int:
"""TODO: these are implemented, but are currently more of a sketch than
anything real. See `#26
Expand Down
2 changes: 1 addition & 1 deletion src/trio/_core/_generated_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ def spawn_system_task(
async_fn: Callable[[Unpack[PosArgT]], Awaitable[object]],
*args: Unpack[PosArgT],
name: object = None,
context: contextvars.Context | None = None,
context: (contextvars.Context | None) = None,
) -> Task:
"""Spawn a "system" task.

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
27 changes: 1 addition & 26 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 @@ -168,9 +144,8 @@ def test_run_ruff(tmp_path: Path) -> None:

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