python / python/typing

Does `field_specifier` support non-keyword arguments?

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

まだ誰も着手していません。

topic: documentation
主要言語
Python
スター
1.8k
フォーク
302
平均マージ
23時間
マージ済み PR(30日)
8

説明

I was reading through the specification of dataclass_transform and it doesn't really specify what field_specifiers looks like. It mostly just says:

field_specifiers (tuple[Callable[..., Any], ...]) – Specifies a static list of supported classes or functions that describe fields, similar to dataclasses.field(). Defaults to ().

But dataclasses.field takes keyword-only arguments, which leaves it a bit ambiguous about whether positional arguments are ok. For example:

from typing import dataclass_transform, Any

def custom_field(default: object, *, init: bool = True) -> Any: ...

@dataclass_transform(field_specifiers=(custom_field,))
def build(x): ...

@build
class A:
    x: int = custom_field(default=0)

@build
class B:
    x: int = custom_field(0)

A()
B()

pyrefly, pyright, and mypy all flag B() as an error while ty accepts it without complaint.

My (uninformed) take is that this should be allowed -- custom_field(default=0) and custom_field(0) have the same runtime effect, so it feels like they should have the same type-checking effect too. This also matches how pydantic and attrs works (e.g. you can do pydantic.Field(1) and attr.ib(0)) -- today I suspect the type-checkers are special-casing this to make pydantic and attrs (since I've definitely used both without running into static type-checking errors about missing default values before).

コントリビューションガイド

このリポジトリのコントリビューションガイドは索引されていません

はじめの一歩

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

調査の方向性

dataclass_transform 仕様と、その field_specifiers の定義から始め、pyrefly、pyright、mypy、ty における位置引数とキーワード引数の例を比較します。位置引数を許可すべきかどうかを判断し、その決定を typing specification に記載して、プロジェクトに対応する例または conformance test がある場合はそれを追加します。

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

評価

技術スタック
python
領域
documentation
issue の種類
ドキュメント
難易度
5/5
見積もり時間
1週間以上
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
35/100

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

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