[Tutor] Type annotation errors

boB Stepp robertvstepp at gmail.com
Thu Jun 4 14:14:45 EDT 2020


On Thu, Jun 04, 2020 at 09:32:39AM -0600, Mats Wichmann wrote:
>On 6/3/20 10:30 PM, boB Stepp wrote:

>> One of the exercises is to return a list of dictionaries if a csv file
>> has headers, but if not to return a list of tuples.  mypy apparently
>> does not like this.
>
>I don't like it either :)   (I know, who am I to criticize dabeaz - even
>if I did so professionally for a very short period as one of his
>technical editors on Python Essential Reference 2:e way back in 2000).

The actual set of exercises is from "3.2 Moe on Functions" at
https://dabeaz-course.github.io/practical-python/Notes/03_Program_organization/02_More_functions.html

In this exercise set he wishes to create a module fileparse which can
accept a csv-like file and allow one to obtain a list of dictionaries (one
per file row) if the file has headers or a list of tuples if not,
representing the collection of records.  Additionally he wishes to allow
for setting arguments to choose only certain data columns (If the file has
headers), do type conversions by providing a list of functions to do the
conversions, and setting a different delimiter than the default comma.  At
the end of the exercises he comments:

"If you’ve made it this far, you’ve created a nice library function that’s
genuinely useful. You can use it to parse arbitrary CSV files, select out
columns of interest, perform type conversions, without having to worry too
much about the inner workings of files or the csv module."

>Functions/methods that return different types under different calling
>scenarios  are certainly possible in Python, but that doesn't mean it's
>a great idea for usability.  A contrived learning example is fine; In
>Real Life, I don't see why on earth you'd want this awkward setup. So
>worrying too much about twisting typing so mypy is happy about this
>doesn't seem terribly worthwhile... just sayin'.

However, I have to say, the end result *does* seem useful!

-- 
Wishing you only the best,

boB Stepp


More information about the Tutor mailing list