python / python/mypy

(🐞) `staticmethod` special-casing causes error to not be reported

Offen
#16,161 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

bug
Vorherrschende Sprache
Python
Sterne
20.6k
Forks
3.3k
PR-Merge-Kennzahlen
PR-Kennzahlen ausstehend

Beschreibung

If we copy the implementation of staticmethod from typeshed, mypy correctly reports the error here.

So it seems that mypys dedicated special-casing of staticmethod is causing this issue to be incorrectly not reported.

from collections.abc import Callable
from typing import Generic, ParamSpec, TypeVar, overload

P = ParamSpec("P")
T = TypeVar("T")
R_co = TypeVar("R_co", covariant=True)


def contextmanager(fn: Callable[P, R_co]) -> Callable[P, R_co]:
    def func(*args: P.args, **kwargs: P.kwargs) -> R_co:
        return fn(*args, **kwargs)
    return func


class staticmethod_copy(Generic[P, R_co]):
    @property
    def __func__(self) -> Callable[P, R_co]: ...
    @property
    def __isabstractmethod__(self) -> bool: ...
    def __init__(self, __f: Callable[P, R_co]) -> None: ...
    @overload
    def __get__(self, __instance: None, __owner: type) -> Callable[P, R_co]: ...
    @overload
    def __get__(self, __instance: T, __owner: type[T] | None = None) -> Callable[P, R_co]: ...
    __name__: str
    __qualname__: str
    @property
    def __wrapped__(self) -> Callable[P, R_co]: ...
    def __call__(self, *args: P.args, **kwargs: P.kwargs) -> R_co: ...

# Just the relevant part of staticmethod definition that is needed
#class staticmethod(Generic[P, R_co]):
#    def __init__(self, __f: Callable[P, R_co]) -> None: ...
#    def __call__(self, *args: P.args, **kwargs: P.kwargs) -> R_co: ...

class Foo:
    @contextmanager
    @staticmethod_copy
    def copy() -> None: ...

    @contextmanager
    @staticmethod
    def native() -> None: ...


Foo().copy()   # ✅ error: Attribute function "copy" with type "Callable[[], None]" does not accept self argument  [misc]
Foo().native()  # ❌no error

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

Das bereitgestellte Beispiel reproduzieren und die dedizierte Sonderbehandlung von staticmethod durch mypy mit der kopierten typeshed-Implementierung vergleichen. Nachverfolgen, wie die Decorators und Descriptor-Typen analysiert werden; abgeschlossen ist die Aufgabe, wenn mypy den missing-self-Fehler für Foo().native() ebenso meldet wie für Foo().copy().

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
45/100

Neue Issues direkt in Ihr Postfach

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