grpc-stubs icon indicating copy to clipboard operation
grpc-stubs copied to clipboard

Type hints for abstract base classes are missing `abc.ABC`

Open bkeryan opened this issue 1 year ago • 0 comments

Description of issue

Most of the grpcio package's public classes are abstract base classes. There are over 20 ABCs in grpc/__init__.py alone.

However, the type hints for these classes don't specify a base class of abc.ABC or a metaclass of abc.ABCMeta, so the ABC-specific register class method for registering "virtual subclasses" exists at runtime but not when type checking.

Minimum Reproducible Example

main.py
from __future__ import annotations
import grpc
import typing

@grpc.Call.register
class CallProxy:
    def __init__(self, target: grpc.Call) -> None:
        self._target = target

    def __getattr__(self, name: str) -> typing.Any:
        return getattr(self._target, name)

print("main.py: No errors at runtime")
run.sh
#!/usr/bin/env bash                                                             
set -o errexit -o nounset -o pipefail
python -m venv venv
source ./venv/bin/activate
pip install grpcio grpc-stubs mypy
python main.py
mypy main.py
Full output
Requirement already satisfied: grpcio in ./venv/lib/python3.9/site-packages (1.57.0)
Requirement already satisfied: grpc-stubs in ./venv/lib/python3.9/site-packages (1.53.0.2)
Requirement already satisfied: mypy in ./venv/lib/python3.9/site-packages (1.5.0)
Requirement already satisfied: mypy-extensions>=1.0.0 in ./venv/lib/python3.9/site-packages (from mypy) (1.0.0)
Requirement already satisfied: typing-extensions>=4.1.0 in ./venv/lib/python3.9/site-packages (from mypy) (4.7.1)
Requirement already satisfied: tomli>=1.1.0 in ./venv/lib/python3.9/site-packages (from mypy) (2.0.1)
WARNING: You are using pip version 22.0.4; however, version 23.2.1 is available.
You should consider upgrading via the '/tmp/grpc_abc/venv/bin/python -m pip install --upgrade pip' command.
main.py: No errors at runtime
main.py:5: error: "type[Call]" has no attribute "register"  [attr-defined]
Found 1 error in 1 file (checked 1 source file)

bkeryan avatar Aug 10 '23 17:08 bkeryan