-
-
Notifications
You must be signed in to change notification settings - Fork 11
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
type hint handling #4
Comments
After thinking about this, I'm not even sure how async def gen():
yield
def f() -> AsyncGenerator[None, None]:
return gen() |
I don't have much in the way of conclusions here myself; I haven't played with type hints yet. sphinxcontrib-trio obviously needs a better story for handling them, since they're trying to provide overlapping information in overlapping space. (And also, it looks like the stdlib might start using them to communicate with sphinxcontrib-trio in some cases.) But I'm sort of hoping that someone who actually uses them both will tell me how this should work :-) I would really like to stay out of the docstring parsing business if possible... |
It looks like there are some standalone extensions for making type hint information more readable:
I don't know what the difference between them is (if any). It'd be interesting to know whether they play well with sphinxcontrib-trio, since we all need to hook into some similar bits of sphinx and could potentially collide. Actually, from a quick look @agronholm's version ("sphinx-autodoc-typehints") looks more likely to work with sphinxcontrib-trio... the other one ("sphinx-autodoc-annotation") is trying to replace the same classes that sphinxcontrib-trio does, so I doubt they can work together at all. On further thought, the overlap here is really only in the return type: the information that sphinxcontrib-trio's trying to communicate is all about return type, and the only annotations its text is likely to collide with are return type annotations. So maybe the first thing to try is to start looking at |
Yeah, that's more approaching what I hoped the default behavior would be:
I think ideally sphinxcontrib-trio would warn when there is conflicting information, so that Otherwise, I think having the return type in both places is fine, as long as they match. |
What is that screenshot? Is that using
I think you might be overestimating the reliability of sphinxcontrib-trio's heuristics 😄 (Generally they're pretty good, but when you're trying to figure out the return type of a function that has a stack of decorators on top of it, you inevitably have to make some guesses. So from this point of view, an explicit annotation is great – we can use it instead of guessing!) |
Speaking of guessing, I already found a bug in
async def test() -> str:
return 'foo'
from typing import Coroutine
async def test() -> Coroutine[None, None, str]:
return 'foo' $ mypy --strict test.py
$ mypy --strict test2.py
test2.py:3: error: Incompatible return value type (got "str", expected Coroutine[None, None, str]) This was confusing until I read typing#119
And in that discussion, that's what they agreed on, though the documentation doesn't explain this very well. The problem, is that The expected output, if no additional information is formatted, is:
This could probably be made prettier if desired; something like:
|
After thinking about this, it's probably better that that be left to a real static type checker anyway, so I guess the conclusion is that |
Hmm, I think |
For this to be consistent, I think this transformation would need to be applied to all "extended functions":
I think only if the transformation were explicitly:
Otherwise:
I guess because we know coroutine functions are the only "extended function" that has sugar (I think), there would just need to be transformations for all of the other extended functions. |
We've generally stopped giving The one thing missing is that with a pretty signature, the type hints for complex expressions end up at the wrong position. For example, I currently have this (shortened) signature async def map(function: Callable[[T], R], iterable: AsyncIterable[T]) -> AsyncIterator[R]:
... which gets rendered with
where the "return type" both duplicates and collides with the
but that just gets us back to the same confusion. Is It would be nicer if the return types where included at the correct positions, say
|
I tried to
autofunction
this function:Result:
There are a few problems with this:
QueueItem
isn't expanded like it is in the function definition)Returns: AsyncGenerator[…]
is probably more accurate than saying thisyields
, but I'm not sure.Do you have any ideas on how/if return types should be reconciled with the implied return types from the
await
andasync for …
prefixes that sphinxcontrib-trio adds?Is PEP484 and/or modifying docstring parsing behavior in-scope for this extension at all?
The text was updated successfully, but these errors were encountered: