Does `field_specifier` support non-keyword arguments?
Nessuno ha ancora preso questa issue.
- Lingua principale
- Python
- Stelle
- 1.8k
- Fork
- 302
- Merge medio
- 23h
- PR unite (30g)
- 8
Descrizione
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).
Guida per i contributori
Nessuna guida per i contributori indicizzata per questo repository
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Inizia con la specifica di dataclass_transform e la relativa definizione di field_specifiers; confronta gli esempi di argomenti posizionali e con parola chiave in pyrefly, pyright, mypy e ty. Determina se gli argomenti posizionali debbano essere consentiti, quindi documenta la decisione nella typing specification e aggiungi un esempio corrispondente o un test di conformità se il progetto ne ha uno.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- python
- Ambito
- documentation
- Tipo di issue
- Documentazione
- Difficoltà
- 5/5
- Tempo stimato
- Più di una settimana
- Stato di attività
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 35/100