Comments (12)
Thanks for the reproducer!
@wait_for
stub will not need to change, cause the return function deliberately does not return coroutines, those get wrapped into a blocking call. Just need to unwrap the types somehow in the mypy plugin.
from crochet.
Ah, probably need to do a similar thing where we remove the Coroutine
wrapper which is being added erroneously.
Can you give me a small reproducer that mypy complains about?
from crochet.
This should do it:
$ more mypy.ini
[mypy]
plugins = crochet.mypy
$ cat wait_for_it.py
import crochet
@crochet.wait_for(10)
async def foo() -> str:
return ''
def bar() -> str:
return foo()
$ mypy --strict --config=mypy.ini wait_for_it.py
wait_for_it.py:10: error: Incompatible return value type (got "Coroutine[Any, Any, str]", expected "str")
Found 1 error in 1 file (checked 1 source file)
from crochet.
The stub for wait_for
may need to change as well, I guess something like:
@overload
def wait_for(timeout: float) -> Callable[[Callable[..., Coroutine[Any, Any, _T]]], _T]: ...
@overload
def wait_for(timeout: float) -> Callable[[_F], _F]: ...
from crochet.
Oh actually I'm probably wrong and it does need to change the typing stub, I forget it's a thing that returns a decorator.
from crochet.
... but unfortunately that won't work due to python/typing#256, so probably will need to extend the plugin.
from crochet.
I attempted to update the plugin with limited results. I did discover that the annotation for wait_for
needs to be:
@overload
def wait_for(timeout: float) -> Callable[[Callable[..., Coroutine[Any, Any, _T]]], Callable[..., _T]]: ...
@overload
def wait_for(timeout: float) -> Callable[[_F], _F]: ...
from crochet.
Hi again @itamarst, I was playing around with the new ParamSpec
support in mypy==0.950
and had success with this:
...
from typing_extensions import ParamSpec
...
@overload
def wait_for(timeout: float) -> Callable[
[Callable[_P, Deferred[_T]]],
Callable[_P, _T]
]: ...
@overload
def wait_for(timeout: float) -> Callable[
[Callable[_P, Coroutine[Any, Any, _T]]],
Callable[_P, _T]
]: ...
@overload
def wait_for(timeout: float) -> Callable[
[Callable[_P, _T]],
Callable[_P, _T]
]: ...
I suspect that the need for the mypy plugin is gone.
from crochet.
Happy to accept a PR, I don't have a lot of time to spend on writing this myself.
from crochet.
Happy to accept a PR, I don't have a lot of time to spend on writing this myself.
I started a test-bed at #144
from crochet.
Looks like we have to wait for python/mypy#12595 though...
I managed to get this to work well:
@overload
def wait_for_x(f: Callable[_P, Deferred[_T]]) -> Callable[_P, _T]:
...
@overload
def wait_for_x(f: Callable[_P, Coroutine[None, None, _T]]) -> Callable[_P, _T]:
...
@overload
def wait_for_x(f: Callable[_P, _T]) -> Callable[_P, _T]:
...
But (correctly) making wait_for
return a callable which returns a callable... did not:
@overload
def wait_for(f: float) -> Callable[[Callable[_P, Deferred[_T]]], Callable[_P, _T]]:
...
@overload
def wait_for(f: float) -> Callable[[Callable[_P, Coroutine[None, None, _T]]], Callable[_P, _T]]:
...
@overload
def wait_for(f: float) -> Callable[[Callable[_P, _T]], Callable[_P, _T]]:
...
from crochet.
That MyPy bug is still open, alas.
from crochet.
Related Issues (20)
- Document difference from async/await
- Add github starring thing to docs
- Error using "run_in_reactor" in 1.8.0 that worked well in 1.7.0 HOT 5
- Python 3.7 support
- Someday, switch to Python's SimpleQueue instead of reactor-based queue HOT 1
- Stop Iteration Error while using run_in_reactor HOT 6
- Python 3.8 support
- Use os.register_at_fork to make crochet work better with e.g. multiprocessing HOT 2
- i586-only test failures on openSUSE HOT 3
- Add coroutine support HOT 3
- threading.Thread.setDaemon has been deprecated in favor of setting daemon attribute directly in Python 3.10 HOT 1
- Switch to GitHub Actions
- Drop Python 2.7, 3.5, add Python 3.9
- Inability to detect partial async functions in Python < 3.8 HOT 1
- In async/await part of documentation, link to Django APIs HOT 1
- Replace deprecated imp module with importlib HOT 2
- 2.0.0: pytest is failing in 3 units HOT 3
- Automated uploads to PyPI
- Add .readthedocs.yaml
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from crochet.