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

Enable flake8-pytest-style #2822

Merged
merged 37 commits into from
Dec 13, 2023

Conversation

CoolCat467
Copy link
Member

This PR enables the flake8-pytest-style ruff rule.

@codecov
Copy link

codecov bot commented Oct 19, 2023

Codecov Report

Merging #2822 (7a8f3ab) into master (60b9e14) will increase coverage by 0.01%.
The diff coverage is 100.00%.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #2822      +/-   ##
==========================================
+ Coverage   99.54%   99.55%   +0.01%     
==========================================
  Files         115      115              
  Lines       17663    17658       -5     
  Branches     3158     3160       +2     
==========================================
- Hits        17583    17580       -3     
  Misses         52       52              
+ Partials       28       26       -2     
Files Coverage Δ
src/trio/_core/_tests/test_guest_mode.py 100.00% <100.00%> (ø)
src/trio/_core/_tests/test_instrumentation.py 100.00% <100.00%> (ø)
src/trio/_core/_tests/test_io.py 100.00% <100.00%> (ø)
src/trio/_core/_tests/test_local.py 100.00% <100.00%> (ø)
src/trio/_core/_tests/test_mock_clock.py 100.00% <100.00%> (ø)
src/trio/_core/_tests/test_multierror.py 100.00% <100.00%> (ø)
src/trio/_core/_tests/test_parking_lot.py 100.00% <100.00%> (ø)
src/trio/_core/_tests/test_run.py 100.00% <100.00%> (ø)
src/trio/_core/_tests/test_thread_cache.py 100.00% <100.00%> (ø)
src/trio/_core/_tests/test_windows.py 100.00% <100.00%> (ø)
... and 24 more

@TeamSpen210
Copy link
Contributor

Setting the fixture-parentheses option to false in pyproject.toml will make Ruff use @pytest.fixture, and instead warn if empty parentheses are used. It's probably better to be configuring linters to match project conventions, when the existing code is perfectly correct.

@@ -135,6 +136,9 @@ extend-exclude = [
[tool.ruff.isort]
combine-as-imports = true

[tool.ruff.flake8-pytest-style]
fixture-parentheses = false
Copy link
Member

Choose a reason for hiding this comment

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

🎉

I also like the setting that forces using a sequence of strings in parametrize rather than a comma-separated string...

@CoolCat467 CoolCat467 changed the title [WIP] Enable flake8-pytest-style Enable flake8-pytest-style Dec 1, 2023
Copy link
Member

@jakkdl jakkdl left a comment

Choose a reason for hiding this comment

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

A couple nits, but overall great!

Going to be a ton of merge conflicts though 🙃

src/trio/_tests/test_socket.py Outdated Show resolved Hide resolved
@@ -1116,7 +1132,8 @@ async def test_many_sockets() -> None:
try:
a, b = stdlib_socket.socketpair()
except OSError as e: # pragma: no cover
assert e.errno in (errno.EMFILE, errno.ENFILE)
# the noqa is "Found assertion on exception `e` in `except` block"
assert e.errno in (errno.EMFILE, errno.ENFILE) # noqa: PT017
Copy link
Member

Choose a reason for hiding this comment

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

.

Copy link
Member Author

@CoolCat467 CoolCat467 Dec 1, 2023

Choose a reason for hiding this comment

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

This one is quite interesting actually. This one is a loop that breaks prematurely only if the kernel responds with an Out of socket file ids or something error.

Copy link
Member

Choose a reason for hiding this comment

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

hmm, this is a weird test. it loops _x until it gets total//2-1 unless it throws an exception - in which case it'd break and then the test would fail later in the last nowait that's just a print! That print should totally be inside the except catcher and _x can be renamed to _. Probably also a pragma: no cover? codecov is acting weird so idk if it's getting covered or not.

Okay, so, it loops until it fails to create a socket - or it hits total//2. So supposedly it's not an error if it raises earlier, nor is it a problem if it doesn't raise, which means that pytest.raises is not relevant.

It'd maybe be nice to add some comments. Why pytest.raises isn't relevant, and maybe also nice to add that EMFILE is "Too many open files" and ENFILE is "File table overflow".

Copy link
Member Author

@CoolCat467 CoolCat467 Dec 6, 2023

Choose a reason for hiding this comment

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

Fixed I think with c683a7d

src/trio/_core/_tests/test_run.py Outdated Show resolved Hide resolved
Comment on lines -459 to +461
assert len(multi_exc.exceptions) == 4
# the noqa is for "Found assertion on exception `multi_exc` in `except` block"
assert len(multi_exc.exceptions) == 4 # noqa: PT017
Copy link
Member

Choose a reason for hiding this comment

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

oh derp, I should've thought of this one myself. You can leave this one as a noqa though and let me handle it in the multierror PRs

with pytest.raises(MultiError):
with pytest.raises(MultiError): # noqa: PT012
Copy link
Member

Choose a reason for hiding this comment

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

PT012 is a bit of a pain when trio so often tests context managers, so not entirely sure it's worth having enabled. It did catch a couple ugly cases this one time though.

Comment on lines 290 to 294
for code in [errno.EMFILE, errno.EFAULT, errno.ENOBUFS]:
with assert_checkpoints():
with pytest.raises(OSError) as excinfo:
with pytest.raises(OSError) as excinfo: # noqa: PT011 # missing `match`
await listener.accept()
assert excinfo.value.errno == code
Copy link
Member

Choose a reason for hiding this comment

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

for code, match in zip((errno.EMFILE, errno.EFAULT, errno.ENOBUFS), ("Out of file descriptors", "attempt to write to read-only memory", "out of buffers"):
  with assert_checkpoints():
    with pytest.raises(OSError, match=match) as excinfo:
      ...
    ...

should work?

Copy link
Member

Choose a reason for hiding this comment

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

By the way, it could be useful to have @pytest.mark.parametrize or pytest-subtests here so that separate checks would show up in the test report distinctly.

Copy link
Member Author

Choose a reason for hiding this comment

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

Changed in f89cd0a. I didn't do the parameterize because it would be a lot of setup code re-running, but if this is important I can still change it.

src/trio/_tests/test_socket.py Outdated Show resolved Hide resolved
src/trio/_tests/test_subprocess.py Outdated Show resolved Hide resolved
@jakkdl
Copy link
Member

jakkdl commented Dec 1, 2023

Interesting, the pypy-3.9 runner is segfaulting inside pytest from a from .test_socket import setsockopt_tests line. Any ideas?

Edit: https://github.com/python-trio/trio/actions/runs/7054572593/job/19203654087

it worked on a rerun... 🤷‍♀️

@webknjaz
Copy link
Member

webknjaz commented Dec 1, 2023

Interesting, the pypy-3.9 runner is segfaulting inside pytest from a from .test_socket import setsockopt_tests line. Any ideas?
Edit: python-trio/trio/actions/runs/7054572593/job/19203654087

it worked on a rerun... 🤷‍♀️

I've been observing flaky PyPy 3.9 segfaults on GHA in other projects FTR.

CoolCat467 and others added 3 commits December 1, 2023 15:30
Copy link
Member

@jakkdl jakkdl left a comment

Choose a reason for hiding this comment

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

Can probably ignore the codecov check failing, but it might be nice to make use of it to check out what's not properly getting run - and figure out if it should have pragma: no cover or if you messed with some check that now doesn't run anymore.

@@ -1116,7 +1132,8 @@ async def test_many_sockets() -> None:
try:
a, b = stdlib_socket.socketpair()
except OSError as e: # pragma: no cover
assert e.errno in (errno.EMFILE, errno.ENFILE)
# the noqa is "Found assertion on exception `e` in `except` block"
assert e.errno in (errno.EMFILE, errno.ENFILE) # noqa: PT017
Copy link
Member

Choose a reason for hiding this comment

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

hmm, this is a weird test. it loops _x until it gets total//2-1 unless it throws an exception - in which case it'd break and then the test would fail later in the last nowait that's just a print! That print should totally be inside the except catcher and _x can be renamed to _. Probably also a pragma: no cover? codecov is acting weird so idk if it's getting covered or not.

Okay, so, it loops until it fails to create a socket - or it hits total//2. So supposedly it's not an error if it raises earlier, nor is it a problem if it doesn't raise, which means that pytest.raises is not relevant.

It'd maybe be nice to add some comments. Why pytest.raises isn't relevant, and maybe also nice to add that EMFILE is "Too many open files" and ENFILE is "File table overflow".

@jakkdl
Copy link
Member

jakkdl commented Dec 9, 2023

EDIT: split off to a separate PR #2900 and reverted most of the above commit

@jakkdl
Copy link
Member

jakkdl commented Dec 9, 2023

ah no, it was codecov apparently being smart enough to figure out that the assertion in the test was irrelevant given that the raises now has a match= ?! ... or it being too dumb to understand any and accidentally being a genius.

@jakkdl
Copy link
Member

jakkdl commented Dec 11, 2023

I would like to merge this unless somebody else wants to review

Copy link
Contributor

@TeamSpen210 TeamSpen210 left a comment

Choose a reason for hiding this comment

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

Looks all good. Perhaps we might want to enable the other config too to prevent parentheses in @pytest.mark.whatever() also, but not important.

@CoolCat467
Copy link
Member Author

Perhaps we might want to enable the other config too to prevent parentheses in @pytest.mark.whatever() also, but not important.

I think this PR is a bit big, if we would like to later I think another PR would be best.

@CoolCat467
Copy link
Member Author

Merging, we have two approvals.

@CoolCat467 CoolCat467 merged commit f1cb241 into python-trio:master Dec 13, 2023
29 checks passed
@CoolCat467 CoolCat467 deleted the enable-flake8-pytest-style branch December 20, 2023 19:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants