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 pyupgrade rule #2809

Merged
merged 13 commits into from
Oct 25, 2023
Merged

Conversation

CoolCat467
Copy link
Member

This PR enables the pyupgrade ruff rule and fixes all the existing issues that were raised.

@codecov
Copy link

codecov bot commented Oct 3, 2023

Codecov Report

Merging #2809 (641fa56) into master (9ec9d05) will not change coverage.
The diff coverage is 100.00%.

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #2809   +/-   ##
=======================================
  Coverage   99.16%   99.16%           
=======================================
  Files         115      115           
  Lines       17482    17482           
  Branches     3113     3113           
=======================================
  Hits        17336    17336           
  Misses        101      101           
  Partials       45       45           
Files Coverage Δ
trio/_channel.py 100.00% <100.00%> (ø)
trio/_core/_io_epoll.py 100.00% <100.00%> (ø)
trio/_core/_io_windows.py 98.80% <100.00%> (ø)
trio/_core/_run.py 100.00% <100.00%> (ø)
trio/_core/_windows_cffi.py 100.00% <100.00%> (ø)
trio/_file_io.py 100.00% <ø> (ø)
trio/_path.py 100.00% <ø> (ø)
trio/_subprocess.py 98.29% <ø> (ø)
trio/_subprocess_platform/kqueue.py 100.00% <100.00%> (ø)
trio/_sync.py 100.00% <100.00%> (ø)
... and 4 more

Copy link
Contributor

@A5rocks A5rocks left a comment

Choose a reason for hiding this comment

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

Couple of comments but overall looks good

@@ -201,7 +201,7 @@ class _EpollStatistics:
class EpollIOManager:
_epoll: select.epoll = attr.ib(factory=select.epoll)
# {fd: EpollWaiters}
_registered: DefaultDict[int, EpollWaiters] = attr.ib(
_registered: defaultdict[int, EpollWaiters] = attr.ib(
Copy link
Contributor

Choose a reason for hiding this comment

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

Isn't defaultdict a 3.9+ thing? Why is ruff doing this? Cause the from __future__ import annotations? or is this a bug.

Copy link
Contributor

Choose a reason for hiding this comment

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

It’s probably that yes. There’s an option to decide whether to preserve the hints making sense at runtime.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, it decides what's allowed based on both the version you specify and if a future annotations import exists.

Copy link
Member Author

Choose a reason for hiding this comment

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

There is a setting to make sure runtime typing works, mostly so a specific library can work properly, but in my opinion I agree with ruff's behavior here. Why have outdated versions of the type annotations if we don't have to worry about runtime type annotations?

Copy link
Member

Choose a reason for hiding this comment

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

It's been waffled about a lot in various PR's, without a central decision actually getting taken: #2687
IMHO if we want runtime typing we should have tests for it, and enforce it with linter rules.

current_op: Optional[AFDPollOp] = attr.ib(default=None)
read_task: _core.Task | None = attr.ib(default=None)
write_task: _core.Task | None = attr.ib(default=None)
current_op: AFDPollOp | None = attr.ib(default=None)
Copy link
Contributor

Choose a reason for hiding this comment

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

Eh, same as above. I won't make any more comments about 3.9+ features sneaking in.

Copy link
Member

Choose a reason for hiding this comment

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

If we are going to require runtime typing, on 3.8, and that disallowing using | in type hints, then I vehemently disagree with doing that. This is currently the status quo in a lot of the code base.

trio/_dtls.py Outdated
SSL.OP_NO_QUERY_MTU
| SSL.OP_NO_RENEGOTIATION # type: ignore[attr-defined]
)
SSL.OP_NO_QUERY_MTU | SSL.OP_NO_RENEGOTIATION # type: ignore[attr-defined]
Copy link
Contributor

Choose a reason for hiding this comment

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

Huh, why did ruff change this? I think it was intentionally formatted to showcase which specific attribute wasn't defined.

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 saw this and the diff and was a bit confused myself, not sure about this one.

Copy link
Member Author

Choose a reason for hiding this comment

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

Another one of my PRs hit this and I looked into it further, this change was by black, not ruff.

trio/_tests/type_tests/check_wraps.py Outdated Show resolved Hide resolved
" trio.run({async_fn.__name__}(...)) # incorrect!\n"
" nursery.start_soon({async_fn.__name__}(...)) # incorrect!\n"
f" trio.run({async_fn.__name__}(...)) # incorrect!\n"
f" nursery.start_soon({async_fn.__name__}(...)) # incorrect!\n"
Copy link
Contributor

Choose a reason for hiding this comment

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

Mind checking the commit that these came in with (or the PR more generally) and seeing if there's other stuff like this?

Copy link
Member Author

Choose a reason for hiding this comment

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

Same string concat weirdness (see other comment)

"library written for asyncio/twisted/tornado or similar? "
"That won't work without some sort of compatibility shim.".format(coro)
"That won't work without some sort of compatibility shim."
Copy link
Contributor

Choose a reason for hiding this comment

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

Wow messed up upgrade bugs everywhere. Mind checking the commit/PR that this came in with too?

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 don't think it's actually a bug, I think the format call would have worked for it previously because it was applying to the concatenation of all of the strings, since they have parenthesis wrapping them. It definitely looks kind of strange though, and I think using f-strings here makes it much more clear what the idea is than this format call that looks a bit out of place.

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.

imo we can merge this, but Somebody ™️ needs to take a final decision on runtime typing. That affects much more code than this though, so I don't think it should hold up this one.

@@ -201,7 +201,7 @@ class _EpollStatistics:
class EpollIOManager:
_epoll: select.epoll = attr.ib(factory=select.epoll)
# {fd: EpollWaiters}
_registered: DefaultDict[int, EpollWaiters] = attr.ib(
_registered: defaultdict[int, EpollWaiters] = attr.ib(
Copy link
Member

Choose a reason for hiding this comment

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

It's been waffled about a lot in various PR's, without a central decision actually getting taken: #2687
IMHO if we want runtime typing we should have tests for it, and enforce it with linter rules.

current_op: Optional[AFDPollOp] = attr.ib(default=None)
read_task: _core.Task | None = attr.ib(default=None)
write_task: _core.Task | None = attr.ib(default=None)
current_op: AFDPollOp | None = attr.ib(default=None)
Copy link
Member

Choose a reason for hiding this comment

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

If we are going to require runtime typing, on 3.8, and that disallowing using | in type hints, then I vehemently disagree with doing that. This is currently the status quo in a lot of the code base.

@jakkdl
Copy link
Member

jakkdl commented Oct 23, 2023

You can merge this after fixing B904.
The codecov fail looks dumb, windows pypy-3.9-nightly is still bad, and macOS 3.10 is hitting the timeout issues that have started plaguing us. So none of those are related to the PR.

@CoolCat467 CoolCat467 enabled auto-merge (squash) October 23, 2023 23:28
@CoolCat467
Copy link
Member Author

pre-commit.ci autofix

@CoolCat467 CoolCat467 enabled auto-merge (squash) October 24, 2023 00:04
@CoolCat467 CoolCat467 merged commit f85021e into python-trio:master Oct 25, 2023
31 of 32 checks passed
@CoolCat467 CoolCat467 deleted the enable-pyupgrade branch October 26, 2023 03:47
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