Skip to content

#2683: Make _util.Final support static analysis by converting to a decorator.#2793

Merged
TeamSpen210 merged 19 commits into
python-trio:masterfrom
TeamSpen210:static-final
Sep 18, 2023
Merged

#2683: Make _util.Final support static analysis by converting to a decorator.#2793
TeamSpen210 merged 19 commits into
python-trio:masterfrom
TeamSpen210:static-final

Conversation

@TeamSpen210

@TeamSpen210 TeamSpen210 commented Sep 6, 2023

Copy link
Copy Markdown
Contributor

#2683: By converting _util.Final to be a decorator instead of a metaclass, we can alias it with the typing.final decorator. This will make type checkers also refuse to allow subclasses, and allow them to do type narrowing in some cases. Unfortunately I can't see a way to do the same with NoPublicConstructor, so instead I added a test which verifies all uses of that are also @final.

Aside from the static typing benefits, this simplifies the implementation a little and allows _util.final to be used on any class since we don't have metaclass conflicts.

There's a lot of changes here, since I modified every place Final or NoPublicConstructor is used. The actual important changes are in trio/_util.py and trio/_tests/test_util.py. The verify_types_*.json tables drop 2 private symbols because of the removal of _util.Final.

@TeamSpen210 TeamSpen210 added the typing Adding static types to trio's interface label Sep 6, 2023
@codecov

codecov Bot commented Sep 6, 2023

Copy link
Copy Markdown

Codecov Report

Merging #2793 (a949364) into master (a4f12b7) will increase coverage by 0.00%.
Report is 1 commits behind head on master.
The diff coverage is 100.00%.

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #2793   +/-   ##
=======================================
  Coverage   98.97%   98.97%           
=======================================
  Files         115      115           
  Lines       17053    17102   +49     
  Branches     3044     3079   +35     
=======================================
+ Hits        16878    16927   +49     
  Misses        121      121           
  Partials       54       54           
Files Changed Coverage Δ
trio/_channel.py 100.00% <100.00%> (ø)
trio/_core/_entry_queue.py 100.00% <100.00%> (ø)
trio/_core/_exceptions.py 100.00% <100.00%> (ø)
trio/_core/_local.py 100.00% <100.00%> (ø)
trio/_core/_mock_clock.py 100.00% <100.00%> (ø)
trio/_core/_parking_lot.py 100.00% <100.00%> (ø)
trio/_core/_run.py 99.44% <100.00%> (ø)
trio/_core/_unbounded_queue.py 100.00% <100.00%> (ø)
trio/_dtls.py 96.60% <100.00%> (+0.01%) ⬆️
trio/_highlevel_generic.py 100.00% <100.00%> (ø)
... and 13 more

@TeamSpen210 TeamSpen210 linked an issue Sep 6, 2023 that may be closed by this pull request
@TeamSpen210 TeamSpen210 requested a review from jakkdl September 6, 2023 04:12
Comment thread trio/_tests/test_util.py

@A5rocks A5rocks left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Mostly minor comments. RE: making a new PR, just rebasing so you have like 3 commits to show specifically which commit is adding new final classes would work too.

Comment thread trio/_core/_parking_lot.py Outdated
Comment thread trio/_core/_unbounded_queue.py Outdated
Comment thread trio/_tests/test_exports.py
Comment thread trio/_tests/test_exports.py Outdated
Comment thread trio/_util.py Outdated

@A5rocks A5rocks left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Any remaining threads from my past review are just opinions on implementation: choose what you want! I think anything sounds good and this PR looks good.

@A5rocks

A5rocks commented Sep 12, 2023

Copy link
Copy Markdown
Contributor

Not sure if you want this squashed and merged or if you want to rebase to have several commits!

@TeamSpen210 TeamSpen210 enabled auto-merge (squash) September 18, 2023 08:18
@TeamSpen210 TeamSpen210 merged commit 1e8327b into python-trio:master Sep 18, 2023
@TeamSpen210 TeamSpen210 deleted the static-final branch September 18, 2023 08:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

typing Adding static types to trio's interface

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add static analysis support to _util.Final

3 participants