python / python/mypy

decorated classes trigger Incompatible types in "await" (actual type "DecoratedClass", expected type "Awaitable[Any]")

Open
#17,299 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

I have a program who defined a decorator , and decorated to a classes.
Its design intention is to implement network requests through direct class calls.

eg: RequietApiClass(data), then run RequietApiClass.call.inner_implement_func to complete HTTP request.
The following code is an example of a problem display。It run OK, but mypy check types error. But, i dont know how to fixed it.
please help me, thanks!

import asyncio
from typing import Any, Awaitable, Type

def apiwrapper(cls: Type):
    return cls()

@apiwrapper
class RequestApiClass:
    def __init__(self):
        print("aaa")

    async def __call__(self, data):
        return data

async def test_api_cfg_ok():
    testdata = {"a": "a"}
    ret = DecoratedClass(testdata)
    result = await ret
    print(result)

asyncio.run(test_api_cfg_ok())

problem descript:

Too many arguments for "DecoratedClass"

Incompatible types in "await" (actual type "DecoratedClass", expected type "Awaitable[Any]")

if i conver class to sync, then will trigger new problem.

async_to_sync(DecoratedClass)({"a": "a"})

problem descript

Argument 1 to "async_to_sync" has incompatible type "Type[DecoratedClass]"; expected "Union[Callable[[], Coroutine[Any, Any, Never]], Callable[[], Awaitable[Never]]]"

environment:
python:3.8.10
mypy:1.10.0

mypy config:

[tool.mypy]
mypy_path = "./oemhasten/apps"
plugins = ["mypy_django_plugin.main", "mypy_drf_plugin.main"]
exclude = ["pyenv"]
explicit_package_bases = true
check_untyped_defs = true
disallow_untyped_decorators = true

[[tool.mypy.overrides]]
module = ["treebeard.*"]
ignore_missing_imports = true

[tool.django-stubs]
django_settings_module = "oemhasten.settings"

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the provided decorator and decorated-class example under Python 3.8.10 and mypy 1.10.0, then reproduce the reported diagnostics with the shown configuration. Trace how the decorated class and its async call are typed; done means the example no longer produces the reported argument and await errors, or the behavior is documented as expected.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.