python / python/mypy

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

オープン
#17,299 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

bug
主要言語
Python
スター
20.6k
フォーク
3.3k
PR マージ指標
PR 指標を取得中

説明

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"

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

Python 3.8.10 と mypy 1.10.0 で、提供された decorator と decorated class の例から始め、示された設定で報告された診断を再現します。decorated class とその async call がどのように型付けされるかを追跡します。完了条件は、例で報告された引数および await のエラーが発生しなくなること、またはその動作が想定どおりであると文書化されることです。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
python
領域
devtools
issue の種類
バグ
難易度
4/5
見積もり時間
3〜5日
活発さ
停滞
明瞭さ
説明が足りない
初心者へのやさしさ
25/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。