python / python/mypy

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

Open
#17,043 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the dataclass usage in terminaltexteffects/utils/terminal.py and the ArgField definition in terminaltexteffects/utils/argsdataclass.py, then reproduce the assignment errors with mypy. Trace mypy's dataclass handling for dataclasses.Field and subclasses; done means the shown Field and ArgField assignments no longer produce false assignment errors.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.