Support attrs 20.1's on_setattr=attr.setters.convert
Nessuno ha ancora preso questa issue.
- Lingua principale
- Python
- Stelle
- 20.6k
- Fork
- 3.3k
- Metriche di merge delle PR
- Metriche PR in attesa
Descrizione
Feature
Version 20.1 of attrs introduced support for running a field's converter whenever the field is set. Here's an example of it in action:
import re
import attr
@attr.s(auto_attribs=True)
class GHRepo:
owner: str
name: str
def parse_ghrepo(s: str) -> GHRepo:
if (m := re.fullmatch(r'([^/]+)/([^/]+)', s)) is not None:
return GHRepo(owner=m[1], name=m[2])
else:
raise ValueError(s)
@attr.s
class Foo:
repo: GHRepo = attr.ib(converter=parse_ghrepo, on_setattr=attr.setters.convert)
f = Foo("owner/name")
f.repo = "python/mypy" # mypy errors on this line
print(f)
This prints out Foo(repo=GHRepo(owner='python', name='mypy')), as the "python/mypy" assigned to f.repo is converted to a GHRepo with parse_ghrepo().
Unfortunately, mypy as of both version 0.812 and commit 28034cc does not recognize the typing implications of on_setattr=attr.setters.convert, as running it on the above code with the default settings gives the error Incompatible types in assignment (expression has type "str", variable has type "GHRepo") on the marked line. I would like to request that mypy recognize the setting's typing implications and allow thus-configured fields to be set to the input types of their converters.
Pitch
This feature would provide greater support for attrs and allow for defining fields that are initialized & set by converting from other types.
Guida per i contributori
Apri la guida per i contributori
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 riproducendo l’esempio Python fornito con mypy e verificando come vengono attualmente inferiti i tipi dei converter di attrs. L’issue non menziona file o test specifici, quindi il lavoro richiede di individuare il plugin attrs pertinente e i percorsi di verifica delle assegnazioni; è completato quando l’assegnazione mostrata viene accettata senza indebolire gli altri controlli dei tipi.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- python
- Ambito
- devtools
- Tipo di issue
- Funzionalità
- Difficoltà
- 5/5
- Tempo stimato
- Più di una settimana
- Stato di attività
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 35/100