python / python/mypy

Idiomatic tuple element replacement

Open
#16,948 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

feature
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

The issue does not name a source file, test, or entry point. Begin by reviewing the discussion around #11990 and the existing handling of tuple types, Literal arithmetic, and namedtuple._replace(). Done should mean selecting and implementing one clearly scoped approach, with tests covering tuple element replacement and its typing behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
tooling
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.