python-trio / trio

Trio – a friendly Python library for async concurrency and I/O
https://trio.readthedocs.io
Other
5.98k stars 325 forks source link

Support Pyright detection of constructor kwargs in `attrs`-based data classes #2945

Closed mikenerone closed 5 months ago

mikenerone commented 5 months ago

[The changes related to this update fall into three categories:

  1. attrs>=23.2.0 is now required (based on this comment thread plus needing to bump to at least 22.2.0 anyway to support Pyright).
  2. Adds non-underscored aliases for a few selected private (i.e. leading underscore) attributes in CancelStatus and CancelScope (this is required for Pyright to recognize private attributes as constructor kwargs).
  3. Switching to the "NG" interfaces in recent versions of attrs per previous chat discussion.
    • import attr -> import attrs everywhere
    • attr.ib -> attrs.field
    • attr.s -> attrs.define or attrs.frozen, depending on existing frozen kwarg and with adjustments made to all calls to account for slots=True now being the default).
    • attrs.field(factory=X) -> attrs.Factory(X)
    • attrs.field(default=X) -> simply assign X
  4. One typing correction that was uncovered by that last "NG" update (see related comment).

Note: there's no new logic to be tested. All existing tests pass unchanged (except for non-logic name changes resulting from item 3 above).

codecov[bot] commented 5 months ago

Codecov Report

All modified and coverable lines are covered by tests :white_check_mark:

Comparison is base (0dd5bc4) 99.64% compared to head (0b1b5f2) 99.64%.

Additional details and impacted files ```diff @@ Coverage Diff @@ ## master #2945 +/- ## ======================================= Coverage 99.64% 99.64% ======================================= Files 117 117 Lines 17564 17564 Branches 3166 3166 ======================================= Hits 17502 17502 Misses 43 43 Partials 19 19 ``` | [Files](https://app.codecov.io/gh/python-trio/trio/pull/2945?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=python-trio) | Coverage Δ | | |---|---|---| | [src/trio/\_channel.py](https://app.codecov.io/gh/python-trio/trio/pull/2945?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=python-trio#diff-c3JjL3RyaW8vX2NoYW5uZWwucHk=) | `100.00% <100.00%> (ø)` | | | [src/trio/\_core/\_asyncgens.py](https://app.codecov.io/gh/python-trio/trio/pull/2945?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=python-trio#diff-c3JjL3RyaW8vX2NvcmUvX2FzeW5jZ2Vucy5weQ==) | `100.00% <100.00%> (ø)` | | | [src/trio/\_core/\_entry\_queue.py](https://app.codecov.io/gh/python-trio/trio/pull/2945?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=python-trio#diff-c3JjL3RyaW8vX2NvcmUvX2VudHJ5X3F1ZXVlLnB5) | `100.00% <100.00%> (ø)` | | | [src/trio/\_core/\_io\_epoll.py](https://app.codecov.io/gh/python-trio/trio/pull/2945?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=python-trio#diff-c3JjL3RyaW8vX2NvcmUvX2lvX2Vwb2xsLnB5) | `100.00% <100.00%> (ø)` | | | [src/trio/\_core/\_io\_kqueue.py](https://app.codecov.io/gh/python-trio/trio/pull/2945?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=python-trio#diff-c3JjL3RyaW8vX2NvcmUvX2lvX2txdWV1ZS5weQ==) | `87.20% <100.00%> (ø)` | | | [src/trio/\_core/\_io\_windows.py](https://app.codecov.io/gh/python-trio/trio/pull/2945?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=python-trio#diff-c3JjL3RyaW8vX2NvcmUvX2lvX3dpbmRvd3MucHk=) | `98.80% <100.00%> (ø)` | | | [src/trio/\_core/\_ki.py](https://app.codecov.io/gh/python-trio/trio/pull/2945?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=python-trio#diff-c3JjL3RyaW8vX2NvcmUvX2tpLnB5) | `100.00% <100.00%> (ø)` | | | [src/trio/\_core/\_local.py](https://app.codecov.io/gh/python-trio/trio/pull/2945?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=python-trio#diff-c3JjL3RyaW8vX2NvcmUvX2xvY2FsLnB5) | `100.00% <100.00%> (ø)` | | | [src/trio/\_core/\_parking\_lot.py](https://app.codecov.io/gh/python-trio/trio/pull/2945?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=python-trio#diff-c3JjL3RyaW8vX2NvcmUvX3BhcmtpbmdfbG90LnB5) | `100.00% <100.00%> (ø)` | | | [src/trio/\_core/\_run.py](https://app.codecov.io/gh/python-trio/trio/pull/2945?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=python-trio#diff-c3JjL3RyaW8vX2NvcmUvX3J1bi5weQ==) | `99.66% <100.00%> (ø)` | | | ... and [19 more](https://app.codecov.io/gh/python-trio/trio/pull/2945?src=pr&el=tree-more&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=python-trio) | |
Fuyukai commented 5 months ago

If you're replacing ib -> define, you might as well go through and replace import attr with import attrs everywhere too.

mikenerone commented 5 months ago

If you're replacing ib -> define, you might as well go through and replace import attr with import attrs everywhere too.

Happy to do that, as well, but that really does require increasing the constraint to >=21.3.0, so I'll wait for consensus on what version to constrain to in the other thread. Edit: Ended up doing this.

mikenerone commented 5 months ago

Added two more commits for additional shorter syntaxes:

Along with a fix to a small typing error that this uncovered. The original line in src/trio/_core/_run.py was:

_next_send_fn: Callable[[Any], object] = attr.ib(default=None)

The type annotation didn't allow the default value. I updated the annotation.

trio-bot[bot] commented 5 months ago

Hey @mikenerone, it looks like that was the first time we merged one of your PRs! Thanks so much! :tada: :birthday:

If you want to keep contributing, we'd love to have you. So, I just sent you an invitation to join the python-trio organization on Github! If you accept, then here's what will happen:

If you want to read more, here's the relevant section in our contributing guide.

Alternatively, you're free to decline or ignore the invitation. You'll still be able to contribute as much or as little as you like, and I won't hassle you about joining again. But if you ever change your mind, just let us know and we'll send another invitation. We'd love to have you, but more importantly we want you to do whatever's best for you.

If you have any questions, well... I am just a humble Python script, so I probably can't help. But please do post a comment here, or in our chat, or on our forum, whatever's easiest, and someone will help you out!

mikenerone commented 5 months ago

Only skimmed the changes, but seems fine to merge. For ease of review it's perhaps not great to bundle a bugfix with a verbose style change, (and slots changes, with is coolcat's hobbyhorse :P) but probably not worth splitting it up at this point.

@jakkdl By way of explanation, because the Pyright fix required a bump to the attrs dep version, this PR really became an "update attrs and use all of the next-gen interfaces that are now prescribed in this new version." From that perspective, this really was just a style PR resulting from the dep update. The "bugfix" part we got for free with the update - there are zero extra lines here to accomplish that. Also, there are no slots changes here, and in fact intentionally zero changes in any functional way like that.