`TypedDict.copy()` falls back to `dict[str, object]`

Aperta
#4,990 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

@HarikumarS1106 ci sta già lavorando.

Dal 5/10/2026.

Valutazione

Difficoltà
5/5
Tempo stimato
Più di una settimana
Idoneità per principianti
35/100
Tipo di issue
Bug
Chiarezza
Da chiarire
Stato di attività
Attiva
Stack tecnologico
python, rust
Ambito
devtools

Direzione di ricerca

Start by reproducing the bound and constrained type-variable examples for TypedDict.copy() from the issue or linked sandbox, then read the referenced typing specification section. Determine the intended result for each example and make pyrefly's revealed type consistent with that decision, with coverage for the reported cases.

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

Descrizione

typechecking
Describe the Bug

Calling copy on a TypedDict is not described in https://typing.python.org/en/latest/spec/typeddict.html#specific-operations

I've found that pyrefly, pyright and ty all disagree on how this should work with type variables.

Show code block

from __future__ import annotations

from typing_extensions import TypedDict, assert_type, reveal_type


class A(TypedDict, closed=True):
    a: int


class B(TypedDict, closed=True):
    b: str


a = A(a=1)
b = B(b="b")

a_copy = a.copy()
b_copy = b.copy()

assert_type(a, A)
assert_type(b, B)


def copy_bound[T: A | B](obj: T) -> T:
    result = obj.copy()  # ty: ignore[invalid-argument-type]
    reveal_type(result)
    # pyright   'Type of "result" is "A* | B*"'
    # ty        'Revealed type:      `Unknown`'
    # pyrefly   'revealed type:      dict[str, object]'
    return result

def copy_constrained[T: (A, B)](obj: T) -> T:
    result = obj.copy()
    reveal_type(result)
    # pyright   'Type of "result" is  "A* | B*"'
    # ty        'Revealed type:       `T@copy_constrained`'
    # pyrefly   'revealed type:       dict[str, object] & T'
    return result

I'm reporting here as I found the pyrefly output the most surprising.
Maybe this needs to be defined in the typing spec?

My actual use case for this was a bound type variable, over a union with 20-30 members that all use closed=True.

Sandbox Link

https://pyrefly.org/sandbox/?project=v2.pVbRboIwFP2VBpNBFpXslcRk8AXLwp7EIIWqZK41o2zz73duaQno5sz0qSk97e2555x6MTLzfNNCA9QTZ20qrrBWtuHQBWsuvrSQdPE-IgapNgqwk8ByvoyDAaDcq0ZUC-qf02QRoeF6iEj-QHCbB0aZoC8OisUDtMExTgK-yDyeeUZSRV6qwxHTxZwGpDPupng_Rfru7xEUUxZjcjjFpywx6zonEy7nqpXVMo1YDAsnqwCujVhqnJE6jYmm3WschW_2MMYmIBY3NqpY1hI2rKsZYqJ9E1LP6DioqUP3dAbdTiiBPkwYBFpvdxpjn5hiagMJdWsyj5Ii8-J7Kus-83wH0kdncP_ZbC0q6jGSyPzWL_JVqk-57gHWBgToahkDKrSHIhhvlgmslQXafLHlDCkrISIN40lhiAtiovVa5m4n5T-ssHX6eFr71QydU8TuWPoTTeehM2FPdnPrX1CItNq2eNRhxnkmJ3jPBdtpfWiiMHRw9b4NhQwrVTbhCBEyytfio6jNX4XxdkyZkGyw7S1p53eyxg1_zzqfog4rLsbXNw

(Only applicable for extension issues) IDE Information

No response

Lingua principale
Rust
Stelle
7.2k
Fork
578
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Preparare l'ambiente

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.

Altre issue di facebook/pyrefly

Tutte le issue di facebook/pyrefly

Issue simili

Altre issue su Rust

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.