python / python/mypy

Idiomatic tuple element replacement

Aperta
#16,948 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

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

Descrizione

Feature

To replace one element within a tuple (returning a modified copy), we're currently forced to write something like this:

[mypy-play.net]

from __future__ import annotations
from typing import (
        Any,
        Dict,
        Final,
        Tuple,
        )
from typing_extensions import (
        assert_type,
        )

ComplexTuple = Tuple[str, int, str, Dict[Any, Any], Dict[str, Any]]
        # (field0, lineno, field2, field3, field4)

def add_to_lineno(ctup: ComplexTuple, lineno_delta: int) -> ComplexTuple:

    # we want to represent the index to be replaced in one place, to
    # keep things DRY:
    index: Final = 1  # s.t. ctup[index] == lineno

    left = ctup[:index]  # element(s) left of the value being replaced

    lineno = ctup[index]
    lineno += lineno_delta
    lineno_frag = (lineno,)

    right = ctup[index:][1:]  # elements right of the value being
                              # replaced
            # NOTE: ↑ weird, inefficient phrasing of 'ctup[index+1:]'

    ret = left + lineno_frag + right
    return ret

Note the very odd phrasing of right = ... at line 27; this serves to work around incomplete #11990 (which appears to be stale).

It took me quite a while to identify this work-around; I'm sure others would struggle with the same issue.

Pitch

In my opinion, there would be value in doing one of the following:

  • document namedtuple._replace() as the preferred work-around for this case, in a way that aids discovery
  • (ideally) implement a subset of proposal #11990, specifically focused on basic arithmetic support for Literals with exactly one, integer value
  • implement a function to idiomatically replace elements of a tuple (that isn't a namedtuple)

Notes

I apologize if a "feature" isn't the right venue for discussing this.

This builds on #3078.

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

La issue non indica alcun file sorgente, test o punto di ingresso. Inizia esaminando la discussione relativa a #11990 e la gestione esistente dei tipi tuple, dell’aritmetica di Literal e di namedtuple._replace(). Il lavoro dovrebbe considerarsi completato quando sarà stato selezionato e implementato un approccio chiaramente delimitato, con test che coprano la sostituzione degli elementi di tuple e il relativo comportamento di typing.

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

Valutazione

Stack tecnologico
python
Ambito
tooling
Tipo di issue
Funzionalità
Difficoltà
5/5
Tempo stimato
Più di una settimana
Stato di attività
Ferma
Chiarezza
Da chiarire
Idoneità per principianti
25/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.