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

オープン
#4,990 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

@HarikumarS1106 がすでに取り組んでいます。

2026年10月5日 から。

評価

難易度
5/5
見積もり時間
1週間以上
初心者へのやさしさ
35/100
issue の種類
バグ
明瞭さ
説明が足りない
活発さ
活発
技術スタック
python, rust
領域
devtools

調査の方向性

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.

索引モデルが issue の本文から書いたものです。

説明

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

主要言語
Rust
スター
7.2k
フォーク
578
PR マージ指標
30日以内にマージされた PR はありません

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

facebook/pyrefly のほかの issue

facebook/pyrefly の issue をすべて見る

似ている issue

Rust の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。