python / python/mypy

allow-redefinition-new for enum results in incompatible type of Literal[members]

Aperta
#19,894 2 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

bug topic-enum topic-inference topic-literal-types
Lingua principale
Python
Stelle
20.6k
Fork
3.3k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

Followup to the advice at https://github.com/python/mypy/issues/19280#issuecomment-2971970574 :)

Reduced from https://github.com/mesonbuild/meson/blob/5f3d12abc814b9ea99e56ea31b79a4cb3e8411fc/mesonbuild/dependencies/base.py#L605-L643

from enum import Enum
import typing as T

class DependencyMethods(Enum):
    # Auto means to use whatever dependency checking mechanisms in whatever order meson thinks is best.
    AUTO = 'auto'
    PKGCONFIG = 'pkg-config'
    CMAKE = 'cmake'
    # The dependency is provided by the standard library and does not need to be linked
    BUILTIN = 'builtin'
    # Just specify the standard link arguments, assuming the operating system provides the library.
    SYSTEM = 'system'
    # This is only supported on OSX - search the frameworks directory by name.
    EXTRAFRAMEWORK = 'extraframework'
    # Detect using the sysconfig module.
    SYSCONFIG = 'sysconfig'
    # Specify using a "program"-config style tool
    CONFIG_TOOL = 'config-tool'
    # For backwards compatibility
    SDLCONFIG = 'sdlconfig'
    CUPSCONFIG = 'cups-config'
    PCAPCONFIG = 'pcap-config'
    LIBWMFCONFIG = 'libwmf-config'
    QMAKE = 'qmake'
    # Misc
    DUB = 'dub'

def process_method_kw(possible: T.Iterable[DependencyMethods], kwargs: T.Dict[str, T.Any]) -> T.List[DependencyMethods]:
    method: T.Union[DependencyMethods, str] = kwargs.get('method', 'auto')
    method = DependencyMethods(method)

    # handle renames...

    # Set the detection method. If the method is set to auto, use any available method.
    # If method is set to a specific string, allow only that detection method.
    if method == DependencyMethods.AUTO:
        methods = list(possible)
    elif method in possible:
        methods = [method]
    else:
        raise Exception('argh')

    return methods

Expected Behavior

Passes type checking.

Actual Behavior

$ mypy --local-partial-types --allow-redefinition-new foo.py 
foo.py:43: error: Incompatible return value type (got "list[DependencyMethods] | list[Literal[DependencyMethods.PKGCONFIG, DependencyMethods.CMAKE, DependencyMethods.BUILTIN, DependencyMethods.SYSTEM, DependencyMethods.EXTRAFRAMEWORK, DependencyMethods.SYSCONFIG, DependencyMethods.CONFIG_TOOL, DependencyMethods.SDLCONFIG, DependencyMethods.CUPSCONFIG, DependencyMethods.PCAPCONFIG, DependencyMethods.LIBWMFCONFIG, DependencyMethods.QMAKE, DependencyMethods.DUB]]", expected "list[DependencyMethods]")  [return-value]
Found 1 error in 1 file (checked 1 source file)

It is being redefined as "Literal for every possible enum value". If I annotate the first assignment as T.List[DependencyMethods] then it works (the second assignment fits into the first!), but I feel like I ought not need this.

Your Environment

  • Mypy version used: mypy 1.18.2 (compiled: yes)
  • Mypy command-line flags: --local-partial-types --allow-redefinition-new

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 con l’esempio ridotto in foo.py ed esegui mypy 1.18.2 usando --local-partial-types e --allow-redefinition-new per riprodurre l’errore del tipo restituito. Traccia come viene inferita la variabile enum ridefinita e controlla i test di type checking corrispondenti o i punti di ingresso dell’implementazione. Il lavoro è completato quando l’esempio supera il type checking senza annotare la prima assegnazione.

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

Valutazione

Stack tecnologico
python
Ambito
devtools
Tipo di issue
Bug
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Ferma
Chiarezza
Specificata chiaramente
Idoneità per principianti
45/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.