Closed
Description
Bug Report
In a dataclass with an optional field with a generic as the default factory, mypy reports that the types are incompatible when there should be no compatibility issue between them.
from typing import List, Optional
from dataclasses import dataclass, field
@dataclass
class A:
x: Optional[List[int]] = field(default_factory=list)
Expected Behavior
A bare list is compatible with Optional[List[int]]
, so no error should be reported.
Actual Behavior
$ mypy ./optlist.py
optlist.py:7: error: Incompatible types in assignment (expression has type "List[_T]", variable has type "Optional[List[int]]")
Found 1 error in 1 file (checked 1 source file)
Your Environment
$ cat /etc/lsb-release
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=20.04
DISTRIB_CODENAME=focal
DISTRIB_DESCRIPTION="Ubuntu 20.04.3 LTS"
$ python --version
Python 3.8.10
$ mypy --version
mypy 0.930