python / python/mypy

dataclass: Incompatible types in assignment to Optional[List[Optional[int]]]

Open
#10,636 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Bug Report

In dataclasses, using default_factory with an Optional[List[Optional[int]]] field produces an "Incompatible types in assignment" error

To Reproduce

$ python --version
Python 3.9.5

$ mypy --version
mypy 0.902

$ cat tmp.py
from dataclasses import dataclass, field
from typing import Optional, List
@dataclass
class SubscriptedList:
    list: List[int] = field(default_factory=lambda: [1, 2])
    opt_list: Optional[List[int]] = field(default_factory=lambda: [1, 2])
    list_opt: List[Optional[int]] = field(default_factory=lambda: [1, 2])
    opt_list_opt: Optional[List[Optional[int]]] = field(default_factory=lambda: [1, 2])

$ mypy tmp.py
tmp.py:8: error: Incompatible types in assignment (expression has type "List[int]", variable has type "Optional[List[Optional[int]]]")
Found 1 error in 1 file (checked 1 source file)

This error does not come up for a normal variable assignment:

var: Optional[List[Optional[int]]] = [1,2]  # mypy has no issue with this

If the default is [1,2, None] or None, no error is produced, but using [None] as the default does produce an error.

Note that a very similar error presents for Optional[Dict[str, Optional[int]]]:

from dataclasses import dataclass, field
from typing import Dict, Optional
@dataclass
class SubscriptedDict:
    opt_dict_opt: Optional[Dict[str, Optional[int]]] = field(default_factory=lambda: {"foo": 123}) 

With the above, mypy gives:

tmp.py:5: error: Incompatible types in assignment (expression has type "Dict[str, int]", variable has type "Optional[Dict[str, Optional[int]]]")

Expected Behavior

I do not expect that assigning default_factory=lambda: [1,2] to an Optional[List[Optional[int]]] field produces an error.

Your Environment

  • Mypy version used: 0.902
  • Mypy command-line flags: None
  • Mypy configuration options from mypy.ini (and other config files): None
  • Python version used: 3.9.5
  • Operating system and version: Ubuntu 18.04

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 by reproducing the report in tmp.py with mypy 0.902, focusing on the dataclass fields using Optional[List[Optional[int]]] and Optional[Dict[str, Optional[int]]]. Compare these results with the normal variable assignment; done means valid default_factory expressions no longer produce an incompatible-assignment error while genuinely incompatible defaults remain errors.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
devtools, tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.