This was originally found when upgrading pip to mypy 0.770 in pypa/pip#8235
There was a change made to _csv.pyi in #3581 which hardcodes the input type of _Writer.write to str
|
class _Writer(Protocol): |
|
def write(self, s: str) -> Any: ... |
If I understand correctly, as per the csv.writer docs , csvfile can be any object with a write() method, which means that things like io.BytesIO whose write accepts bytes-like object will fail typechecking
A minimal reproducer for this is as follows, and the type checking was done against mypy 0.770
and python 3.8.2
import csv
import io
buffer = io.BytesIO()
wrt = csv.writer(buffer)
And the output is
$ mypy --version
mypy 0.770
$ python --version
Python 3.8.2
$ mypy scratch.py
scratch.py:5: error: Argument 1 to "writer" has incompatible type "BytesIO"; expected "_Writer"
scratch.py:5: note: Following member(s) of "BytesIO" have conflicts:
scratch.py:5: note: Expected:
scratch.py:5: note: def write(self, s: str) -> Any
scratch.py:5: note: Got:
scratch.py:5: note: def write(self, Union[bytes, bytearray]) -> int
Found 1 error in 1 file (checked 1 source file)
Also note that this worked in mypy 0.760
$ mypy --version
mypy 0.760
$ mypy scratch.py
Success: no issues found in 1 source file
This was originally found when upgrading pip to
mypy 0.770in pypa/pip#8235There was a change made to
_csv.pyiin #3581 which hardcodes the input type of_Writer.writetostrtypeshed/stdlib/2and3/_csv.pyi
Lines 44 to 45 in 6dca3f7
If I understand correctly, as per the csv.writer docs ,
csvfile can be any object with a write() method, which means that things like io.BytesIO whose write acceptsbytes-like objectwill fail typecheckingA minimal reproducer for this is as follows, and the type checking was done against
mypy 0.770and
python 3.8.2And the output is
Also note that this worked in
mypy 0.760