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

Switch to use ruff #2795

Merged
merged 52 commits into from
Oct 3, 2023
Merged
Show file tree
Hide file tree
Changes from 39 commits
Commits
Show all changes
52 commits
Select commit Hold shift + click to select a range
828ff80
Switch to ruff and deal with the slight differences
CoolCat467 Sep 8, 2023
25dddba
Project must have a name
CoolCat467 Sep 8, 2023
228194d
Remove project specifier, still using setup.py
CoolCat467 Sep 8, 2023
a7df9b1
We have a py.typed file now
CoolCat467 Sep 8, 2023
25fe2fb
Update test requirements for python 3.8
CoolCat467 Sep 8, 2023
dfc8820
Add ruff and remove flake8 and isort
CoolCat467 Sep 8, 2023
78d67c1
Fix only posting fixes, check for all errors
CoolCat467 Sep 8, 2023
0a4486b
Merge branch 'master' into replace-with-ruff
CoolCat467 Sep 20, 2023
2a4aff1
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 20, 2023
1239a4b
Disable autofix and skip nothing
CoolCat467 Sep 20, 2023
8193e23
Fixes pointed out from reviews
CoolCat467 Sep 20, 2023
1a67d10
Enable `Error` rules
CoolCat467 Sep 20, 2023
06bcb00
Stop ignoring `undefined-name` errors
CoolCat467 Sep 21, 2023
1efd80e
Fix `_value_factory` change side effects
CoolCat467 Sep 21, 2023
59a0a8c
Change lint failure test with isort removed now
CoolCat467 Sep 21, 2023
2e72f74
Use `--format github` if on github actions
CoolCat467 Sep 21, 2023
dc76e17
Fix inverted boolean
CoolCat467 Sep 21, 2023
561453e
Remove newline
CoolCat467 Sep 23, 2023
9cf4394
Update import string to be what it will after ruff runs
CoolCat467 Sep 23, 2023
8f88afc
Make factory inlined
CoolCat467 Sep 23, 2023
43bec76
Merge branch 'master' into replace-with-ruff
CoolCat467 Sep 26, 2023
3024fbb
Ruff formatting
CoolCat467 Sep 26, 2023
b90c337
Update generated files
CoolCat467 Sep 26, 2023
f232ce2
Update ruff (v0.0.286 -> v0.0.291)
CoolCat467 Sep 27, 2023
f842f15
Ignore line too long instead of setting incredibly large
CoolCat467 Sep 27, 2023
a756800
Enable `RUFF` rule and apply fixes
CoolCat467 Sep 27, 2023
8b3ad2f
Split linting into two seperate functions so we can test them
CoolCat467 Sep 27, 2023
4f7621d
Remove unnecessary `list(...)`
CoolCat467 Sep 27, 2023
5023739
Fix comments that mentioned noqa (ruff regex bug)
CoolCat467 Sep 27, 2023
64ee1cd
Fix missing arguments
CoolCat467 Sep 27, 2023
b6b675e
Fix ruff test
CoolCat467 Sep 27, 2023
7e5122a
Attempt to fix again
CoolCat467 Sep 27, 2023
defb75b
Print stderr for debugging
CoolCat467 Sep 27, 2023
2d60883
Revert "Enable `RUFF` rule and apply fixes"
CoolCat467 Sep 27, 2023
1c1dbf7
Re-enable `RUFF` rule but without confusables change
CoolCat467 Sep 27, 2023
bef1141
Check response code differently
CoolCat467 Sep 27, 2023
961eb13
Messed up format argument, it's different in `check`
CoolCat467 Sep 27, 2023
6cc59b7
Fix comments that mentioned noqa (ruff regex bug) II
CoolCat467 Sep 27, 2023
211dd8d
Merge branch 'master' into replace-with-ruff
CoolCat467 Sep 28, 2023
d364693
Apply suggestions from code review
CoolCat467 Sep 29, 2023
8b8dc70
Fix explanation comments (more accurate information)
CoolCat467 Sep 29, 2023
1812bf7
Merge branch 'master' into replace-with-ruff
CoolCat467 Sep 29, 2023
7ed8e69
Merge branch 'master' into replace-with-ruff
CoolCat467 Sep 29, 2023
dcce938
Update ruff version requirement, `--format` is deprecated
CoolCat467 Sep 29, 2023
951d42a
Fix merge conflicts in gen_exports
CoolCat467 Sep 29, 2023
54ee74d
Update `test-requirements.txt`
CoolCat467 Sep 29, 2023
af0547a
Deduplicate sample test code segment
CoolCat467 Sep 30, 2023
18e7668
Update docstrings
CoolCat467 Oct 1, 2023
60fbe57
Put docstring ends on newlines
CoolCat467 Oct 1, 2023
ddd6bc5
Merge branch 'master' into replace-with-ruff
CoolCat467 Oct 1, 2023
90c5de7
Update `test-requirements.txt`
CoolCat467 Oct 1, 2023
c53f3d1
Add reference to ruff issue to explanation comments
CoolCat467 Oct 1, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 5 additions & 12 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,20 @@ repos:
rev: 23.9.1
hooks:
- id: black
- repo: https://github.com/pycqa/isort
rev: 5.12.0
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.0.291
hooks:
- id: isort
- repo: https://github.com/pycqa/flake8
rev: 6.1.0
hooks:
- id: flake8
additional_dependencies:
- "flake8-pyproject==1.2.3"
- id: ruff
types: [file]
types_or: [python, pyi]
types_or: [python, pyi, toml]
- repo: https://github.com/codespell-project/codespell
rev: v2.2.5
hooks:
- id: codespell

ci:
autofix_commit_msg: "[pre-commit.ci] auto fixes from pre-commit.com hooks"
autofix_prs: true
autofix_prs: false
autoupdate_commit_msg: "[pre-commit.ci] pre-commit autoupdate"
autoupdate_schedule: weekly
skip: [black,isort]
submodules: false
38 changes: 20 additions & 18 deletions check.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

set -ex

ON_GITHUB_CI=true
EXIT_STATUS=0

# If not running on Github's CI, discard the summaries
if [ -z "${GITHUB_STEP_SUMMARY+x}" ]; then
GITHUB_STEP_SUMMARY=/dev/null
ON_GITHUB_CI=false
fi

# Test if the generated code is still up to date
Expand All @@ -22,7 +24,7 @@ echo "::endgroup::"
# pyupgrade --py3-plus $(find . -name "*.py")
echo "::group::Black"
if ! black --check setup.py trio; then
echo "* Black found issues" >> $GITHUB_STEP_SUMMARY
echo "* Black found issues" >> "$GITHUB_STEP_SUMMARY"
EXIT_STATUS=1
black --diff setup.py trio
echo "::endgroup::"
Expand All @@ -31,22 +33,22 @@ else
echo "::endgroup::"
fi

echo "::group::ISort"
if ! isort --check setup.py trio; then
echo "* isort found issues." >> $GITHUB_STEP_SUMMARY
# Run ruff, configured in pyproject.toml
echo "::group::Ruff"
if ! ruff check .; then
echo "* ruff found issues." >> "$GITHUB_STEP_SUMMARY"
EXIT_STATUS=1
isort --diff setup.py trio
if $ON_GITHUB_CI; then
ruff check --format github --diff .
else
ruff check --diff .
fi
echo "::endgroup::"
echo "::error:: isort found issues"
echo "::error:: ruff found issues"
else
echo "::endgroup::"
fi

# Run flake8, configured in pyproject.toml
echo "::group::Flake8"
flake8 trio/ || EXIT_STATUS=$?
echo "::endgroup::"

# Run mypy on all supported platforms
# MYPY is set if any of them fail.
MYPY=0
Expand All @@ -56,12 +58,12 @@ rm -f mypy_annotate.dat
# Pipefail makes these pipelines fail if mypy does, even if mypy_annotate.py succeeds.
set -o pipefail
mypy trio --show-error-end --platform linux | python ./trio/_tools/mypy_annotate.py --dumpfile mypy_annotate.dat --platform Linux \
|| { echo "* Mypy (Linux) found type errors." >> $GITHUB_STEP_SUMMARY; MYPY=1; }
|| { echo "* Mypy (Linux) found type errors." >> "$GITHUB_STEP_SUMMARY"; MYPY=1; }
# Darwin tests FreeBSD too
mypy trio --show-error-end --platform darwin | python ./trio/_tools/mypy_annotate.py --dumpfile mypy_annotate.dat --platform Mac \
|| { echo "* Mypy (Mac) found type errors." >> $GITHUB_STEP_SUMMARY; MYPY=1; }
|| { echo "* Mypy (Mac) found type errors." >> "$GITHUB_STEP_SUMMARY"; MYPY=1; }
mypy trio --show-error-end --platform win32 | python ./trio/_tools/mypy_annotate.py --dumpfile mypy_annotate.dat --platform Windows \
|| { echo "* Mypy (Windows) found type errors." >> $GITHUB_STEP_SUMMARY; MYPY=1; }
|| { echo "* Mypy (Windows) found type errors." >> "$GITHUB_STEP_SUMMARY"; MYPY=1; }
set +o pipefail
# Re-display errors using Github's syntax, read out of mypy_annotate.dat
python ./trio/_tools/mypy_annotate.py --dumpfile mypy_annotate.dat
Expand All @@ -85,9 +87,9 @@ echo "::endgroup::"
if git status --porcelain | grep -q "requirements.txt"; then
echo "::error::requirements.txt changed."
echo "::group::requirements.txt changed"
echo "* requirements.txt changed" >> $GITHUB_STEP_SUMMARY
echo "* requirements.txt changed" >> "$GITHUB_STEP_SUMMARY"
git status --porcelain
git --no-pager diff --color *requirements.txt
git --no-pager diff --color ./*requirements.txt
EXIT_STATUS=1
echo "::endgroup::"
fi
Expand All @@ -96,7 +98,7 @@ codespell || EXIT_STATUS=$?

python trio/_tests/check_type_completeness.py --overwrite-file || EXIT_STATUS=$?
if git status --porcelain trio/_tests/verify_types*.json | grep -q "M"; then
echo "* Type completeness changed, please update!" >> $GITHUB_STEP_SUMMARY
echo "* Type completeness changed, please update!" >> "$GITHUB_STEP_SUMMARY"
echo "::error::Type completeness changed, please update!"
git --no-pager diff --color trio/_tests/verify_types*.json
EXIT_STATUS=1
Expand All @@ -123,5 +125,5 @@ in your local checkout.
EOF
exit 1
fi
echo "# Formatting checks succeeded." >> $GITHUB_STEP_SUMMARY
echo "# Formatting checks succeeded." >> "$GITHUB_STEP_SUMMARY"
exit 0
3 changes: 0 additions & 3 deletions ci.sh
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,6 @@ else
INSTALLDIR=$(python -c "import os, trio; print(os.path.dirname(trio.__file__))")
cp ../pyproject.toml $INSTALLDIR

# TODO: remove this once we have a py.typed file
touch "$INSTALLDIR/py.typed"

Comment on lines -123 to -125
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If being a stickler I'd remove all the instances of generating/removing py.typed in a dedicated PR.

# get mypy tests a nice cache
MYPYPATH=".." mypy --config-file= --cache-dir=./.mypy_cache -c "import trio" >/dev/null 2>/dev/null || true

Expand Down
4 changes: 2 additions & 2 deletions docs/source/local_customization.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from docutils.parsers.rst import directives as directives # noqa: F401
from docutils.parsers.rst import directives as directives
from sphinx import addnodes
from sphinx.domains.python import PyClasslike
from sphinx.ext.autodoc import ( # noqa: F401
from sphinx.ext.autodoc import (
ClassLevelDocumenter as ClassLevelDocumenter,
FunctionDocumenter as FunctionDocumenter,
MethodDocumenter as MethodDocumenter,
Expand Down
3 changes: 1 addition & 2 deletions docs/source/typevars.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,12 @@
import re
from pathlib import Path

import trio
from sphinx.addnodes import Element, pending_xref
from sphinx.application import Sphinx
from sphinx.environment import BuildEnvironment
from sphinx.errors import NoUri

import trio


def identify_typevars(trio_folder: Path) -> None:
"""Record all typevars in trio."""
Expand Down
2 changes: 1 addition & 1 deletion notes-to-self/aio-guest-test.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ async def trio_main():
to_trio, from_aio = trio.open_memory_channel(float("inf"))
from_trio = asyncio.Queue()

asyncio.create_task(aio_pingpong(from_trio, to_trio))
_task_ref = asyncio.create_task(aio_pingpong(from_trio, to_trio))

from_trio.put_nowait(0)

Expand Down
2 changes: 1 addition & 1 deletion notes-to-self/atomic-local.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def f():
f.__code__.co_code,
f.__code__.co_consts,
f.__code__.co_names,
f.__code__.co_varnames + (sentinel,),
(*f.__code__.co_varnames, sentinel),
f.__code__.co_filename,
f.__code__.co_name,
f.__code__.co_firstlineno,
Expand Down
3 changes: 3 additions & 0 deletions notes-to-self/graceful-shutdown-idea.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import signal

import gsm
import trio


Expand Down
8 changes: 4 additions & 4 deletions notes-to-self/how-does-windows-so-reuseaddr-work.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,11 @@ def table_entry(mode1, bind_type1, mode2, bind_type2):
# default | wildcard | INUSE | Success | ACCESS | Success | INUSE | Success
)

for i, mode1 in enumerate(modes):
for j, bind_type1 in enumerate(bind_types):
for mode1 in modes:
for bind_type1 in bind_types:
row = []
for k, mode2 in enumerate(modes):
for l, bind_type2 in enumerate(bind_types):
for mode2 in modes:
for bind_type2 in bind_types:
entry = table_entry(mode1, bind_type1, mode2, bind_type2)
row.append(entry)
# print(mode1, bind_type1, mode2, bind_type2, entry)
Expand Down
2 changes: 2 additions & 0 deletions notes-to-self/manual-signal-handler.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# How to manually call the SIGINT handler on Windows without using raise() or
# similar.
import os
import sys

if os.name == "nt":
import cffi
Expand Down
1 change: 0 additions & 1 deletion notes-to-self/win-waitable-timer.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
from datetime import datetime, timedelta, timezone

import cffi

import trio
from trio._core._windows_cffi import ffi, kernel32, raise_winerror

Expand Down
55 changes: 39 additions & 16 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,50 @@ force-exclude = '''
[tool.codespell]
ignore-words-list = 'astroid,crasher,asend'

[tool.flake8]
extend-ignore = ['D', 'E', 'W', 'F403', 'F405', 'F821', 'F822']
per-file-ignores = [
'trio/__init__.py: F401',
'trio/_core/__init__.py: F401',
'trio/_core/_tests/test_multierror_scripts/*: F401',
'trio/abc.py: F401',
'trio/lowlevel.py: F401',
'trio/socket.py: F401',
'trio/testing/__init__.py: F401'
[tool.ruff]
target-version = "py38"
respect-gitignore = true
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note this is the default behavior

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought it would be better to have at least some of the defaults set manually for if said settings are ever changed in a future PR.

fix = true

allowed-confusables = ["–"]

# The directories to consider when resolving first vs. third-party imports.
# Does not control what files to include/exclude!
src = ["trio", "notes-to-self"]

select = [
"RUF", # Ruff-specific rules
"E", # Error
"F", # pyflakes
"I", # isort
"YTT", # flake8-2020
]
extend-ignore = [
'F403', # undefined-local-with-import-star
'F405', # undefined-local-with-import-star-usage
'E402', # module-import-not-at-top-of-file (usually OS-specific)
'E501', # line-too-long
]

include = ["*.py", "*.pyi", "**/pyproject.toml"]

[tool.isort]
combine_as_imports = true
profile = "black"
skip_gitignore = true
skip_glob = [
extend-exclude = [
"docs/source/reference-*",
"docs/source/tutorial/*"
"docs/source/tutorial/*",
]
A5rocks marked this conversation as resolved.
Show resolved Hide resolved

[tool.ruff.per-file-ignores]
'trio/__init__.py' = ['F401']
'trio/_core/__init__.py' = ['F401']
'trio/_core/_tests/test_multierror_scripts/*' = ['F401']
'trio/abc.py' = ['F401']
'trio/lowlevel.py' = ['F401']
'trio/socket.py' = ['F401']
'trio/testing/__init__.py' = ['F401']

[tool.ruff.isort]
combine-as-imports = true

[tool.mypy]
python_version = "3.8"

Expand Down
2 changes: 2 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from setuptools import find_packages, setup

__version__ = "0.0.0" # Overwritten from _version.py below
CoolCat467 marked this conversation as resolved.
Show resolved Hide resolved

exec(open("trio/_version.py", encoding="utf-8").read())

LONG_DESC = """\
Expand Down
3 changes: 1 addition & 2 deletions test-requirements.in
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ cryptography>=41.0.0 # cryptography<41 segfaults on pypy3.10
black; implementation_name == "cpython"
mypy; implementation_name == "cpython"
types-pyOpenSSL; implementation_name == "cpython" # and annotations
flake8
flake8-pyproject
ruff
astor # code generation
pip-tools >= 6.13.0
codespell
Expand Down
17 changes: 3 additions & 14 deletions test-requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,6 @@ exceptiongroup==1.1.3 ; python_version < "3.11"
# via
# -r test-requirements.in
# pytest
flake8==6.1.0
# via
# -r test-requirements.in
# flake8-pyproject
flake8-pyproject==1.2.3
# via -r test-requirements.in
idna==3.4
# via
# -r test-requirements.in
Expand All @@ -59,9 +53,7 @@ jedi==0.19.0
lazy-object-proxy==1.9.0
# via astroid
mccabe==0.7.0
# via
# flake8
# pylint
# via pylint
mypy==1.5.1 ; implementation_name == "cpython"
# via -r test-requirements.in
mypy-extensions==1.0.0 ; implementation_name == "cpython"
Expand Down Expand Up @@ -90,12 +82,8 @@ platformdirs==3.10.0
# pylint
pluggy==1.3.0
# via pytest
pycodestyle==2.11.0
# via flake8
pycparser==2.21
# via cffi
pyflakes==3.1.0
# via flake8
pylint==2.17.5
# via -r test-requirements.in
pyopenssl==23.2.0
Expand All @@ -106,6 +94,8 @@ pyright==1.1.325
# via -r test-requirements.in
pytest==7.4.0
# via -r test-requirements.in
ruff==0.0.287
# via -r test-requirements.in
sniffio==1.3.0
# via -r test-requirements.in
sortedcontainers==2.4.0
Expand All @@ -114,7 +104,6 @@ tomli==2.0.1
# via
# black
# build
# flake8-pyproject
# mypy
# pip-tools
# pylint
Expand Down
2 changes: 1 addition & 1 deletion trio/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
# innocuous bits of the _core API + the higher-level tools from trio/*.py.
#
# Uses `from x import y as y` for compatibility with `pyright --verifytypes` (#2625)

CoolCat467 marked this conversation as resolved.
Show resolved Hide resolved
#
# must be imported early to avoid circular import
from ._core import TASK_STATUS_IGNORED as TASK_STATUS_IGNORED # isort: split

Expand Down
7 changes: 5 additions & 2 deletions trio/_channel.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
from collections import OrderedDict, deque
from math import inf
from types import TracebackType
from typing import Tuple # only needed for typechecking on <3.9
from typing import TYPE_CHECKING, Generic
from typing import (
TYPE_CHECKING,
Generic,
Tuple, # only needed for typechecking on <3.9
)

import attr
from outcome import Error, Value
Expand Down
3 changes: 1 addition & 2 deletions trio/_core/_generated_io_kqueue.py

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading