python / python/mypy

False "type assignment error" when assigning an instance of `Field` class or classes extended from it.

Offen
#17,043 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

bug
Vorherrschende Sprache
Python
Sterne
20.6k
Forks
3.3k
PR-Merge-Kennzahlen
PR-Kennzahlen ausstehend

Beschreibung

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

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginne mit der Verwendung von dataclass in terminaltexteffects/utils/terminal.py und der Definition von ArgField in terminaltexteffects/utils/argsdataclass.py, und reproduziere die Zuweisungsfehler mit mypy. Verfolge mypys Verarbeitung von dataclasses.Field und seinen Unterklassen; abgeschlossen ist die Aufgabe, wenn die gezeigten Field- und ArgField-Zuweisungen keine falschen Zuweisungsfehler mehr erzeugen.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
python
Bereich
devtools
Issue-Typ
Bug
Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
42/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.