python / python/mypy

Error with Protocols and AsyncContextManager

Offen
#8,276 10 Kommentare 1 Reaktion 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

documentation needs discussion topic-usability
Vorherrschende Sprache
Python
Sterne
20.6k
Forks
3.3k
PR-Merge-Kennzahlen
PR-Kennzahlen ausstehend

Beschreibung

  • Are you reporting a bug, or opening a feature request?
    A bug

  • Please insert below the code you are checking with mypy,

from __future__ import annotations

import asyncio
import typing as t
from contextlib import asynccontextmanager

import typing_extensions as te


class LockProto(te.Protocol):
    async def with_lock(self) -> t.AsyncContextManager[str]: ...


class Consumer:
    def __init__(self, locker: LockProto) -> None:
        self.locker = locker

    def run(self) -> None:
        async with self.locker.with_lock() as name:
            print(name)


class SockLock:
    @asynccontextmanager
    async def with_lock(self) -> t.AsyncGenerator[str, None]:
        yield 'some-string'


async def main() -> None:
    cons = Consumer(SockLock())
    await cons.run()


if __name__ == '__main__':
    asyncio.run(main())
  • What is the actual behavior/output?
bug.py:18: error: "Coroutine[Any, Any, AsyncContextManager[str]]" has no attribute "__enter__"
bug.py:18: error: "Coroutine[Any, Any, AsyncContextManager[str]]" has no attribute "__exit__"
bug.py:29: error: Argument 1 to "Consumer" has incompatible type "SockLock"; expected "LockProto"
bug.py:29: note: Following member(s) of "SockLock" have conflicts:
bug.py:29: note:     Expected:
bug.py:29: note:         def with_lock(self) -> Coroutine[Any, Any, AsyncContextManager[str]]
bug.py:29: note:     Got:
bug.py:29: note:         def with_lock(*Any, **Any) -> AsyncContextManager[Any]
  • What is the behavior/output you expect?
    No errors

  • What are the versions of mypy and Python you are using?
    0.761

  • What are the mypy flags you are using? (For example --strict-optional)
    No flags, just mypy bug.py

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginne damit, das bereitgestellte Beispiel als bug.py zu speichern und mypy bug.py mit der gemeldeten Version und ohne Flags auszuführen. Verfolge, wie mypy Protocol-Methoden, asynchrone Rückgabetypen und asynchrone Context Manager prüft. Als erledigt gilt die Aufgabe, wenn das Beispiel ohne Fehler ausgeführt wird und dabei das erwartete Verhalten der Typprüfung erhalten bleibt.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
python
Bereich
devtools
Issue-Typ
Bug
Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
30/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.