Skip to content

Add typing for _core/_asyncgens.py#2735

Merged
jakkdl merged 19 commits intopython-trio:masterfrom
CoolCat467:typing-core-asyncgens
Aug 10, 2023
Merged

Add typing for _core/_asyncgens.py#2735
jakkdl merged 19 commits intopython-trio:masterfrom
CoolCat467:typing-core-asyncgens

Conversation

@CoolCat467
Copy link
Member

This PR adds type annotations to _core/_asyncgens.py

I would love feedback and any comments on how this could be improved to be more accurate.

@codecov
Copy link

codecov bot commented Aug 4, 2023

Codecov Report

Merging #2735 (3767fc1) into master (722f1b5) will increase coverage by 0.00%.
The diff coverage is 100.00%.

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #2735   +/-   ##
=======================================
  Coverage   98.90%   98.90%           
=======================================
  Files         113      113           
  Lines       16686    16691    +5     
  Branches     3025     3025           
=======================================
+ Hits        16504    16509    +5     
  Misses        125      125           
  Partials       57       57           
Files Changed Coverage Δ
trio/_core/_asyncgens.py 100.00% <100.00%> (ø)

@CoolCat467 CoolCat467 added the typing Adding static types to trio's interface label Aug 4, 2023
@CoolCat467
Copy link
Member Author

I do have one issue currently, mypy says trio/_core/_asyncgens.py:111: error: "Awaitable[None]" has no attribute "send" [attr-defined]

@CoolCat467
Copy link
Member Author

Now we have these errors with those changes:

trio/_core/_asyncgens.py:30: error: Incompatible types in assignment (expression has type "WeakSet[_T]", variable has type "MutableSet[AsyncGeneratorType[object, NoReturn]]")  [assignment]
trio/_core/_asyncgens.py:37: error: Incompatible types in assignment (expression has type "Set[_T]", variable has type "Set[AsyncGeneratorType[object, NoReturn]]")  [assignment]
trio/_core/_asyncgens.py:184: error: Incompatible types in assignment (expression has type "Set[<nothing>]", variable has type "MutableSet[AsyncGeneratorType[object, NoReturn]]")  [assignment]

@CoolCat467 CoolCat467 requested a review from TeamSpen210 August 4, 2023 21:46
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 good overall.

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.

Small followups but looks good. I think we could probably get rid of the nasty type-check-only time type aliases in a couple months after tools get fixed.


self.prev_hooks = sys.get_asyncgen_hooks()
sys.set_asyncgen_hooks(firstiter=firstiter, finalizer=finalizer)
sys.set_asyncgen_hooks(firstiter=firstiter, finalizer=finalizer) # type: ignore[arg-type] # Finalizer doesn't use AsyncGeneratorType
Copy link
Contributor

Choose a reason for hiding this comment

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

Now that I look at the comment history I see why we use AsyncGeneratorType (that was my question).

However, if this is indeed guaranteed to be correct (I still don't understand but I haven't really looked too closely at docs), then can someone raise an error on typeshed too?

Copy link
Contributor

Choose a reason for hiding this comment

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

Well any library could manually do sys.get_asyncgen_hooks() then call it with whatever, but that’s rather esoteric and probably their fault if they pass something wrong. In the standard library, it’s only called deep in the async generator code, when they’re being destroyed.

We could check, but I think it might actually be better not to - the only type that might make sense to pass here would be something like Cython, which would probably duck-type well enough.

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.

Up the strictness in pyproject.toml for the file

@CoolCat467
Copy link
Member Author

Up the strictness in pyproject.toml for the file

Changed in cd19e84

@CoolCat467 CoolCat467 requested a review from jakkdl August 7, 2023 02:16
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.

Nothing to add aside from what's already up for discussion

@jakkdl jakkdl merged commit 4363a06 into python-trio:master Aug 10, 2023
@jakkdl
Copy link
Member

jakkdl commented Aug 10, 2023

🚀

@CoolCat467 CoolCat467 deleted the typing-core-asyncgens branch August 11, 2023 00:37
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