On , I learnt ...
How to use type annotations with Python’s csv
module
Adding a type annotation for a “writer” object created by csv.writer(...)
is
fiddlier than you might think.
The type of the writer object is _csv.writer
which you verify with:
>>> import csv, sys
>>> type(csv.writer(sys.stdout))
<class '_csv.writer'>
but if you try this:
import _csv
def generate_report(writer: _csv.writer):
pass
mypy
complains that:
Function "_csv.writer" is not valid as a type [valid-type].
The correct approach is to use _csv._writer
as the type annotation. A string
literal is required otherwise you’ll get a
AttributeError: module '_csv' has no attribute '_writer'
exception.
import _csv
def generate_report(writer: '_csv._writer'):
pass