Giter Club home page Giter Club logo

Comments (12)

itamarst avatar itamarst commented on June 3, 2024 1

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.

itamarst avatar itamarst commented on June 3, 2024

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.

arnimarj avatar arnimarj commented on June 3, 2024

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.

arnimarj avatar arnimarj commented on June 3, 2024

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.

itamarst avatar itamarst commented on June 3, 2024

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.

itamarst avatar itamarst commented on June 3, 2024

... but unfortunately that won't work due to python/typing#256, so probably will need to extend the plugin.

from crochet.

arnimarj avatar arnimarj commented on June 3, 2024

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.

arnimarj avatar arnimarj commented on June 3, 2024

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.

itamarst avatar itamarst commented on June 3, 2024

Happy to accept a PR, I don't have a lot of time to spend on writing this myself.

from crochet.

arnimarj avatar arnimarj commented on June 3, 2024

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.

arnimarj avatar arnimarj commented on June 3, 2024

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.

itamarst avatar itamarst commented on June 3, 2024

That MyPy bug is still open, alas.

from crochet.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.