False "type assignment error" when assigning an instance of `Field` class or classes extended from it.
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
1- As you must know, there is no standard way in python to use decorators for defining metadata for a field of a class and the only possible approach is assigning an instance of Field class from dataclass module to the field. I believe it's weird and irrational but anyway it's now the standard defined in pep 0557 so we expect mypy to follow it.
2- there was an issue that describe mypy raising a false "Incompatible types in assignment" error in the simple following code:
@dataclass
class A:
x: Optional[List[int]] = field(default_factory=list)
3- It seems Sometime around Oct 2023, the problem solved but unfortunately as I can see it solved just in an special condition. in the other words, just when using the field factory method.
To Reproduce
If you remove the # type: ignore[assignment] directive at the end of the following code:
@dataclass
class TerminalArgs(ArgsDataClass):
tab_width: int = ArgField(
cmd_name=["--tab-width"],
type_parser=argtypes.positive_int,
default=4,
help="Number of spaces to use for a tab character.",
)# type: ignore[assignment]
(Also ArgField is accessible here)
mypy will raise errors like:
terminaltexteffects/utils/terminal.py:21: error: Incompatible types in assignment (expression has type "ArgField", variable has type "int") [assignment]
terminaltexteffects/utils/terminal.py:27: error: Incompatible types in assignment (expression has type "ArgField", variable has type "bool") [assignment]
terminaltexteffects/utils/terminal.py:33: error: Incompatible types in assignment (expression has type "ArgField", variable has type "bool") [assignment]
terminaltexteffects/utils/terminal.py:39: error: Incompatible types in assignment (expression has type "ArgField", variable has type "int") [assignment]
terminaltexteffects/utils/terminal.py:45: error: Incompatible types in assignment (expression has type "ArgField", variable has type "float") [assignment]
Expected Behavior
Allow assigning instance of Field class or instance of any class extended from it to fields of a dataclass.
Actual Behavior
Raising a false Incompatible types in assignment error
Your Environment
- Mypy version used: mypy 1.9.0 (compiled: yes)
- Mypy command-line flags: nothing special
- Mypy configuration options from
mypy.ini(and other config files): nothing special - Python version used: Python 3.11.2
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’uso di dataclass in terminaltexteffects/utils/terminal.py e con la definizione di ArgField in terminaltexteffects/utils/argsdataclass.py, quindi riproduci gli errori di assegnazione con mypy. Traccia la gestione di dataclasses.Field e delle sue sottoclassi da parte di mypy; il lavoro è completo quando le assegnazioni Field e ArgField mostrate non producono più falsi errori di assegnazione.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- python
- Ambito
- devtools
- Tipo di issue
- Bug
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Stato di attività
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 42/100