#4632·ty

Incorrect typing inference from NewType slice (tuple)

Author: Corentin-proCreated Oct 1, 2026Updated Oct 1, 2026

Summary

Type of tuple derivated NewType slices are incorrect.

This bug was found while trying to build a new "typed" tuple from an previous one changing only 1 value (hence the example code doing this). As there is no easy way to do this in Python constructing a new tuple with the previous one sliced is a common solution.

python
from typing import NewType

MyTuple = NewType('MyTuple', tuple[int, str, None, int, str, None, int])
test = MyTuple((0, '', None, 1, 'a', None, 2))

test_slice: tuple[int, str, None, int] = test[:4] # Incompatible value of type `tuple[int | str | None, ...]`
test_error = MyTuple((*test[:4], 'b', *test[5:])) # invalid-argument-tyype
test_ok = MyTuple((*tuple(test)[:4], 'b', *tuple(test)[5:]))  # OK

Simple = tuple[int, str, None, int, str, None, int]
simple: Simple = (0, '', None, 1, 'a', None, 2)

simple_slice: tuple[int, str, None, int] = simple[:4]  # OK
simple_ok: Simple = (*simple[:4], 'b', *simple[5:])  # OK

Slices of tuple from NewType seems to have an ellipsis in their displayed type. Their is a fix for now with using a tuple constructor but this is only to please ty thus not feeling OK (could be a small performance hit at runtime).

This is vaguely related to #2173 but on the opposite: a slice should remove the NewType-ness of the variable an correctly infer the new type without the need of tuple.

Version

ty 0.0.81