What variable type is returned from Open()?

boB Stepp robertvstepp at gmail.com
Sat Apr 18 17:43:21 EDT 2020


On Wed, Apr 15, 2020 at 11:27 AM Chris Angelico <rosuav at gmail.com> wrote:
>
> On Thu, Apr 16, 2020 at 12:01 AM <dcwhatthe at gmail.com> wrote:

> > config_file : file = open(config_file_s, "r")
> >
> >
> > What type of variable should config_file (above) be declared as?

> It returns a file-like object. Instead of annotating, just let the
> type hinting system figure it out - you initialized it with the result
> of an open() call, so it should be able to figure that out.
>
> Python is not C, you do not need to declare all your variable types.
>
> If you actually need a way to represent "file-like object" (maybe as a
> parameter to some other function), there are tools for that in the
> typing module, but just DON'T annotate every variable. Bad idea, will
> cause you a maintenance headache and won't help anything.

I have just started playing around with type annotations and mypy.  I
have still much to learn.  I take your point about "...cause you a
maintenance headache..."  I am guessing that part of what you are
saying is that if mypy (or whatever is actually being used) can
properly infer the type of an object then there is no point in
annotating it.  Can you give guidelines or point to a good article how
to best and most efficiently use type annotation without
over-annotating?  Bear in mind that I have yet to fully grasp all of
what mypy (What I am currently using.) can correctly infer from the
code.

-- 
boB


More information about the Python-list mailing list