python / python/mypy

With `--disallow-any-decorated`, `Any` detection logic changes w/wo decorator

Aperta
#15,211 3 commenti 2 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

bug topic-disallow-any
Lingua principale
Python
Stelle
20.6k
Fork
3.3k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

Bug Report

(A clear and concise description of what the bug is.)

We expect that adding a decorator to a method will never produce a new mypy error unless the decorator itself introduces a new Any annotation, i.e. expect no new error if the decorator itself does not introduce any additional Any.

We also believe that it is difficult to call this behavior a bug. The behavior of the option itself seems self-evident in its own way.

But we would expect a situation where simply applying a harmless decorator would not change the presence or absence of errors.

To Reproduce

Here is a test case.

[case testDisallowAnyDecoratedShouldNeverAffect]
# flags: --disallow-any-decorated

import dataclasses
from typing import Any, Callable

from typing_extensions import TypedDict, TypeVar

class CtxAny(TypedDict):
    place: Any

CallableT = TypeVar("CallableT")

def custom_decorator(func: CallableT) -> CallableT:
    return func

class Something:
    # No error
    def do(self, ctx: CtxAny) -> None:
        pass

    @classmethod
    # This emits an error, unexpected.
    def do_classmethod(cls, ctx: CtxAny) -> None:  # E: Type of decorated function contains type "Any" ("Callable[[Type[Something], CtxAny], None]")
        pass

    @custom_decorator
    # This emits an error, unexpected.
    def do_custom_decorator(self, ctx: CtxAny) -> None:  # E: Type of decorated function contains type "Any" ("Callable[[Something, CtxAny], None]")
        pass
[builtins fixtures/classmethod.pyi]

Expected Behavior

We expect do and others to be the same error-condition.

i.e. one of:

  • No mypy errors emitted.
  • All (containing do method) emits an error.
    • If some option allows us to emit an error on do method, it will be fine.

Actual Behavior

As shown in reproduction code.

Your Environment

  • Mypy version used:current master
  • Mypy command-line flags:--disallow-any-decorated
  • Mypy configuration options from mypy.ini (and other config files):
  • Python version used: 3.8.16, 3.11.3

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia eseguendo il caso di test testDisallowAnyDecoratedShouldNeverAffect con --disallow-any-decorated e il fixture fornito builtins fixtures/classmethod.pyi. Confronta i casi senza decoratore, con classmethod e con decoratore personalizzato, quindi rendi coerente il comportamento e copri la condizione di errore scelta con test di regressione.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
python
Ambito
compilers
Tipo di issue
Bug
Difficoltà
3/5
Tempo stimato
1-2 giorni
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
38/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.