python / python/mypy

The attrs.field converter parameter does not support higher-order functions.

Aperta
#16,860 1 commento 1 reazione 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

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

Descrizione

Bug Report

mypy marks a field conversion higher-order function as unsupported when it is correctly written with the annotations.
the message is: Unsupported converter, only named functions, types and lambdas are currently supported

To Reproduce

from typing import Any, Callable, List, TypeAlias, TypeVar, cast
from attrs import define, field

T = TypeVar('T')
ItemConv: TypeAlias = Callable[[Any], T]

@define   # positional only  # class that resembles a mutable NamedTuple
class Media:
    mediaType: int
    name: str
    ...   # more methods

def media_list_converter(raw: List[List[Any]]) -> List[Media]:
    return [Media(*value) for value in raw]

def not_none(factory: Callable[[], Any], callback: ItemConv[T]) -> ItemConv[T]:
    def converter(raw: Any) -> T:
        if raw is None:
            return cast(T, factory())
        return callback(raw)
    return converter

@define(kw_only=True)
class Foo:
    mediaList: List[Media] = field(factory=list, converter=not_none(list, media_list_converter))  # error

obj = Foo(mediaList=[
    [1, 'the filename']  # more values ...
])

print(obj)  # output: Foo(mediaList=[Media(mediaType=1, name='the filename')])

Expected Behavior

I was hoping mypy would support the nested converter function that returns not_none.

Actual Behavior

>>> mypy media-test.py --strict
media-test.py:25: error: Unsupported converter, only named functions, types and lambdas are currently supported  [misc]
Found 1 error in 1 file (checked 1 source file)

Your Environment

  • Mypy version used: 1.8.0
  • Mypy command-line flags: --strict
  • Mypy configuration options from mypy.ini (and other config files): not used
  • Python version used: 3.11.4

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 riproducendo l'errore dell'esempio media-test.py fornito con mypy --strict e Python 3.11.4. Traccia il modo in cui mypy valida l'espressione del converter di attrs.field, in particolare la funzione annidata restituita da not_none. Il lavoro è completo quando l'esempio supera il controllo dei tipi senza l'errore unsupported-converter, preservando il controllo dei tipi del converter.

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

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.