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

Replace public usages of functools.wraps #2796

Merged
merged 20 commits into from
Oct 3, 2023

Conversation

A5rocks
Copy link
Contributor

@A5rocks A5rocks commented Sep 9, 2023

I'm not entirely sure this works right but I can try figuring out how to configure pyright in a day or something unless someone else wants to check!

Unfortunately yet another pyright incompatibility -- while we could just replace usages around methods this is probably easier?

@codecov
Copy link

codecov bot commented Sep 9, 2023

Codecov Report

Merging #2796 (93e7d80) into master (c30c76f) will increase coverage by 0.00%.
The diff coverage is 100.00%.

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #2796   +/-   ##
=======================================
  Coverage   99.13%   99.13%           
=======================================
  Files         115      115           
  Lines       17206    17207    +1     
  Branches     3082     3082           
=======================================
+ Hits        17057    17058    +1     
  Misses        104      104           
  Partials       45       45           
Files Coverage Δ
trio/_core/_run.py 100.00% <100.00%> (ø)
trio/_highlevel_open_tcp_stream.py 97.64% <100.00%> (ø)
trio/_path.py 100.00% <100.00%> (ø)
trio/_socket.py 100.00% <100.00%> (ø)
trio/_util.py 100.00% <100.00%> (ø)

@A5rocks
Copy link
Contributor Author

A5rocks commented Sep 9, 2023

Huh the typing impact is kinda shocking?

@jakkdl
Copy link
Member

jakkdl commented Sep 9, 2023

Huh the typing impact is kinda shocking?

whoa, great you took the initiative to do this! I didn't know @wraps was that broken in typeshed. I can look at the socket errors on monday or tuesday if you don't wanna try and delve into that thorny mess before then. Could maybe pull it into #2774 and handle it all there

@jakkdl
Copy link
Member

jakkdl commented Sep 13, 2023

Huh the typing impact is kinda shocking?

whoa, great you took the initiative to do this! I didn't know @wraps was that broken in typeshed. I can look at the socket errors on monday or tuesday if you don't wanna try and delve into that thorny mess before then. Could maybe pull it into #2774 and handle it all there

oh, so there was no problem and it was just a matter of your first stab at _wraps being incorrect?

@jakkdl jakkdl added the typing Adding static types to trio's interface label Sep 13, 2023
@A5rocks
Copy link
Contributor Author

A5rocks commented Sep 13, 2023

Eh, it's more about what you think the intention of wraps is: my first stab pretended wrapped functions are the thing they wrap. My second stab didn't do so.

@Hnasar
Copy link

Hnasar commented Sep 13, 2023

I didn't know @wraps was that broken in typeshed.

Yeah unfortunately the stubs break the wrapped function's signature. This comment explains the situation well: python/mypy#5107 (comment)

@jakkdl
Copy link
Member

jakkdl commented Sep 16, 2023

I didn't know @wraps was that broken in typeshed.

Yeah unfortunately the stubs break the wrapped function's signature. This comment explains the situation well: python/mypy#5107 (comment)

Eh, it's more about what you think the intention of wraps is: my first stab pretended wrapped functions are the thing they wrap. My second stab didn't do so.

thank you! It broke my brain a little bit, but I now I get the distinction.

@jakkdl
Copy link
Member

jakkdl commented Sep 16, 2023

no wait, I'm confused again. I think I'm getting different behavior (in pyright) if not renaming the import to _wraps. Could you try just doing a straight from functools import wraps and see if that makes a difference, and if not I'd love a repro of what exactly it is that breaks.

@TeamSpen210
Copy link
Contributor

Just checked, MyPy doesn't have a plugin for wraps, but it does cherry-pick an older implementation in its copy of typeshed, which just treats it as an identity function.

@jakkdl
Copy link
Member

jakkdl commented Sep 16, 2023

no wait, I'm confused again. I think I'm getting different behavior (in pyright) if not renaming the import to _wraps. Could you try just doing a straight from functools import wraps and see if that makes a difference, and if not I'd love a repro of what exactly it is that breaks.

oh derp, the repro is #2775 (comment)

But replacing _wraps with wraps does seem to fix it for me? Which implies that pyright has some workaround they're using.
It'd be great to add pyright to CI, though we might want to wait with it until we don't have to duplicate the blacklist between mypy & pyright config.

@A5rocks
Copy link
Contributor Author

A5rocks commented Sep 16, 2023

Just quick comment: I don't think it's worthwhile having both in CI as they have many intentional discrepancies. The only benefit we would have is that our public API would be guaranteed to work... which is probably something we can guarantee another way? (Just typecheck a subset of tests? Type tests that work in both mypy and pyright? Something like that)

@jakkdl
Copy link
Member

jakkdl commented Sep 17, 2023

Just quick comment: I don't think it's worthwhile having both in CI as they have many intentional discrepancies. The only benefit we would have is that our public API would be guaranteed to work... which is probably something we can guarantee another way? (Just typecheck a subset of tests? Type tests that work in both mypy and pyright? Something like that)

yeah agreed, we'd probably want to strip one of them down a lot. But I've been running both in flake8-trio without too many problems, though that project is definitely doing a lot fewer complex things type-wise - and started with pyright.

@A5rocks
Copy link
Contributor Author

A5rocks commented Sep 22, 2023

Trying out pyright locally, I cannot reproduce wraps working! Not sure what's different about my setup, just using VSCode, ctrl+click then editing the file (e.g. replacing our def _wraps with the import from functools broke things with Argument of type "list[bytes]" cannot be assigned to parameter "self" of type "_SocketType" in function "__call__", then renaming _wraps -> wraps then getting rid of the as didn't fix anything).

@jakkdl
Copy link
Member

jakkdl commented Sep 22, 2023

I don't know what I did last time, but I made sure to do everything properly now ... and you are indeed right! Sorry for the confusion

We should totally add a test for this though, and even if properly setting up pyright is a bigger task - we could for the moment create a dedicated test file with the contents of #2775 (comment), and for the moment run pyright only on that file in check.sh and/or pre-commit. Maybe a directory trio/_tests/typed_files/ or even more specifically trio/_tests/pyright_type_checking/.

@A5rocks
Copy link
Contributor Author

A5rocks commented Sep 26, 2023

Well, that's probably not the implementation you wanted but I went ahead and added a regression test @jakkdl

@TeamSpen210
Copy link
Contributor

With wraps, instead of duplicating the definition it would be better to just put that in _utils or something, then import.
For the type-testing framework, it might be a little easier to just add a type_tests/ folder or the like, then just run each checker over that whole folder in the regular CI.

@jakkdl
Copy link
Member

jakkdl commented Sep 26, 2023

Well, that's probably not the implementation you wanted but I went ahead and added a regression test @jakkdl

lol yeah, I'm not a fan of tests with code in strings. Teamspen210's idea is the same as mine.

@A5rocks
Copy link
Contributor Author

A5rocks commented Sep 30, 2023

Well hopefully that works, I should learn my lesson at this point about not using the web editor ever... It's just too tempting :(

@@ -72,7 +72,7 @@
DEADLINE_HEAP_MIN_PRUNE_THRESHOLD: Final = 1000

# Passed as a sentinel
_NO_SEND: Final = cast("Outcome[Any]", object())
_NO_SEND: Final["Outcome[Any]"] = cast("Outcome[Any]", object())
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This should probably be reported as an issue but gehhhhh

Copy link
Member

@CoolCat467 CoolCat467 left a comment

Choose a reason for hiding this comment

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

I think this makes sense, looks good to me.

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.

Other than small question about check.sh this looks great!

check.sh Outdated Show resolved Hide resolved
@A5rocks
Copy link
Contributor Author

A5rocks commented Oct 3, 2023

I'm going to merge this. Pypy 3.9 nightly failing on Windows happens elsewhere and this way we can ask for more testing of the types!

I think any further changes can be done in a new PR.

@A5rocks A5rocks merged commit 170642e into python-trio:master Oct 3, 2023
27 of 28 checks passed
@A5rocks A5rocks deleted the use-update-wrapper branch October 3, 2023 01:51
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.

5 participants