Type of default value for `attrs.field()` is revealed as `Union[builtins.int, None]`
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
Bug Report
Default (default=) value for attrs.field() type is revealed as Union[builtins.int, None] where the Union with None is unexpected. While this used to be revealed as Any, this shows up as a regression, bisected to https://github.com/python/mypy/commit/391ed853f (PR https://github.com/python/mypy/pull/15021, mypy master, unreleased).
To Reproduce
import attrs
@attrs.define()
class MyDataClass:
some_int_field: int = attrs.field(default=123)
# Somewhere else in the project I would like to reference that default:
reveal = attrs.fields(MyDataClass).some_int_field.default
reveal_type(reveal)
assignment_to_int_fails: int = attrs.fields(MyDataClass).some_int_field.default
Expected Behavior
$ mypy --strict myfile.py
myfile.py:9:13: note: Revealed type is "builtins.int"
Success: no issues found in 1 source file
Actual Behavior
on master (tested 9edda9a79790d8f7263234eca9509657ea0c37f0):
$ mypy --strict --show-error-context --show-column-numbers myfile.py
myfile.py:9:13: note: Revealed type is "Union[builtins.int, None]"
myfile.py:11:32: error: Incompatible types in assignment (expression has type "int | None", variable has type "int") [assignment]
Found 1 error in 1 file (checked 1 source file)
(my project fails, suspected mypy bug / imperfect type inference)
On 1.5.1:
$ mypy --strict myfile.py
myfile.py:9:13: note: Revealed type is "Any"
Success: no issues found in 1 source file
(my project passes, albeit without a proper type check)
Your Environment
- Mypy version used: master (1.5.1 = OK, introduced with https://github.com/python/mypy/commit/391ed853f)
- Mypy command-line flags:
--strict --show-error-context --show-column-numbers - Mypy configuration options from
mypy.ini(and other config files): n/a - Python version used: 3.11.5
Project to test on if you want (100% mypy-strict compatible on 1.5.1 and master otherwise): https://github.com/gertvdijk/PyKMP
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 con l’esempio minimo di attrs nell’issue ed eseguilo con mypy master usando --strict, confrontando il comportamento con quello della versione 1.5.1. Leggi il commit individuato tramite bisect, 391ed853f, e la PR #15021 per ricostruire la regressione. Il lavoro è completato quando il valore predefinito viene rivelato come int e l’assegnazione passa senza errori.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- python
- Ambito
- compilers, devtools
- Tipo di issue
- Bug
- Difficoltà
- 3/5
- Tempo stimato
- 1-2 giorni
- Stato di attività
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 38/100