ray-project / ray

Ray is an AI compute engine. Ray consists of a core distributed runtime and a set of AI Libraries for accelerating ML workloads.
https://ray.io
Apache License 2.0
33.62k stars 5.71k forks source link

`mypy` doesn not recognise that classes annotated with `@serve.deployment` should have a `bind` method. #40018

Open JakeSummers opened 1 year ago

JakeSummers commented 1 year ago

What happened + What you expected to happen

Given this code:

from ray import serve

@serve.deployment(num_replicas=2, ray_actor_options={"num_cpus": 0.2, "num_gpus": 0})
class SayHi:
    def __init__(self, greeting: str):
        # Load model
        self.message = greeting

    def say_hi(self, name: str) -> str:
        # Run inference
        model_output = f"{self.message} {name}"
        return model_output

    async def __call__(self, name: str) -> str:
        return self.say_hi(name)

hi_app = SayHi.bind("Hi") # <------------------- MYPY COMPLAINS HERE

And run:

mypy .

This raises the following error:

my_project/ray_serve/say_hi.py:19: error: "type[SayHi]" has no attribute "bind"  [attr-defined]

This should raise no error.

Versions / Dependencies

ray / ray[serve]

$ poetry show ray
 name         : ray
 version      : 2.7.0
 description  : Ray provides a simple, universal API for building distributed applications.

dependencies
 - aiohttp >=3.7
 - aiohttp-cors *
 - aiorwlock *
 - aiosignal *
 - click >=7.0
 - colorful *
 - fastapi *
 - filelock *
 - frozenlist *
 - gpustat >=1.0.0
 - grpcio >=1.32.0
 - grpcio >=1.42.0
 - jsonschema *
 - msgpack >=1.0.0,<2.0.0
 - numpy >=1.19.3
 - opencensus *
 - packaging *
 - prometheus-client >=0.7.1
 - protobuf >=3.15.3,<3.19.5 || >3.19.5
 - py-spy >=0.2.0
 - pydantic <2
 - pyyaml *
 - requests *
 - smart-open *
 - starlette *
 - uvicorn *
 - virtualenv >=20.0.24,<20.21.1
 - watchfiles *

mypy:

$ poetry show mypy
 name         : mypy
 version      : 1.5.1
 description  : Optional static typing for Python

dependencies
 - mypy-extensions >=1.0.0
 - tomli >=1.1.0
 - typing-extensions >=4.1.0

Reproduction script

See above section :)

Issue Severity

Medium: It is a significant difficulty but I can work around it.

JakeSummers commented 1 year ago

This seems similar to #25657

chasleslr commented 6 months ago

This seems related to an ongoing Mypy limitation described here: https://github.com/python/mypy/issues/3135