python / python/mypy

Unsupported converter, only named functions, types and lambdas are currently supported with higher-order function

Aperta
#15,736 5 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

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

Descrizione

Bug Report

Mypy succeeds evaluating an attr converter that is an (Any) -> Any function, but fails to evaluate a converter that is returned by a higher-order function that returns an (Any) -> Any function.

To Reproduce

This works as expected:

from attr import define, field


def truncate(val):
    return val[:40]


@define
class Person:
    first_name: str = field(converter=truncate)
    last_name: str = field(converter=truncate)


reveal_type(truncate)

Output:

scratch.py:14: note: Revealed type is "def (val: Any) -> Any"
Success: no issues found in 1 source file

But this does not:

from collections.abc import Callable
from typing import Any

from attr import define, field


def truncate(length) -> Callable[[Any], Any]:
    def truncate_converter(val):
        return val[:length]

    return truncate_converter


@define
class Person:
    first_name: str = field(converter=truncate(40))
    last_name: str = field(converter=truncate(80))


reveal_type(truncate(40))
reveal_type(truncate(80))

Expected Behavior

Output:

scratch.py:20: note: Revealed type is "def (Any) -> Any"
scratch.py:21: note: Revealed type is "def (Any) -> Any"
Success: no issues found in 1 source file

Actual Behavior

Output:

scratch.py:16: error: Unsupported converter, only named functions, types and lambdas are currently supported  [misc]
scratch.py:17: error: Unsupported converter, only named functions, types and lambdas are currently supported  [misc]
scratch.py:20: note: Revealed type is "def (Any) -> Any"
scratch.py:21: note: Revealed type is "def (Any) -> Any"
Found 2 errors in 1 file (checked 1 source file)

Your Environment

  • Mypy version used: 1.4.1
  • Mypy command-line flags: just the file name, no extra flags.
  • Mypy configuration options from mypy.ini (and other config files):
    [tool.mypy]
    platform = "linux"
    python_version = "3.10"
    
  • Python version used: 3.10.12

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 riproduttore Python fornito con mypy 1.4.1 e conferma gli errori nei due utilizzi dei converter di attr. Traccia la gestione dei converter dei campi di attr per le funzioni denominate rispetto alle funzioni restituite da truncate, quindi aggiungi un test di regressione che copra entrambe le chiamate. Il lavoro è completato quando i converter di ordine superiore vengono accettati senza errori e i tipi callable rivelati rimangono come mostrato.

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

Valutazione

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

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.