PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, August 29, 2022

[FIXED] How to type-annotate object returned by csv.writer?

 August 29, 2022     csv, python, type-hinting     No comments   

Issue

I want to apply type annotation to the returned object of csv.writer in order to comply with a larger codebase. Unfortunately I can not figure out the fitting return type.

>>> import csv
>>> writer = csv.writer(open('outfile.csv', 'w'))
>>> type(writer)
<class '_csv.writer'>

If I try to use this classname:

>>> import _csv
>>> writer: _csv.writer = csv.writer(open('outfile.csv', 'w'))

I get the following mypy error:

Invalid type "_csv.writer"

Does someone know which type to use in this case. Of course I could use typing.Any but this nullify the sense of a type annotation.


Solution

Often when things are acting weird it's a sign typeshed doesn't exactly map to runtime. If you look at _csv in typeshed you will see the type is named _writer. So you should can the annotation to _csv._writer.



Answered By - ethanhs
Answer Checked By - Willingham (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing