What variable type is returned from Open()?

Chris Angelico rosuav at gmail.com
Thu Apr 16 15:09:39 EDT 2020


On Fri, Apr 17, 2020 at 5:06 AM <dcwhatthe at gmail.com> wrote:
>
> On Thursday, April 16, 2020 at 2:37:42 PM UTC-4, Chris Angelico wrote:
> > On Fri, Apr 17, 2020 at 2:13 AM Richard Damon <Richard> wrote:
> > > I get the answer: <class '_io.TextIOWrapper'>
> > >
> > > So that is the name of the type that is returned, at least for that
> > > call. One key thing to note is that it begins with a _, so that type is
> > > actually an implementation detail, subject to change.
> >
> > This is somewhat true; but when the *module* begins with an
> > underscore, it often means it's a C accelerator for a Python module.
> > You can often find the same thing exposed in a more visible way:
> >
> > >>> import io
> > >>> io.TextIOWrapper
> > <class '_io.TextIOWrapper'>
> >
> > And in that location, it is fully documented:
> >
> > https://docs.python.org/3/library/io.html#io.TextIOWrapper
> >
> > The fact that it comes from the accelerator *is* an internal
> > implementation detail, but the class itself is a public one.
> >
> > That said, though: it is still very much incorrect to type hint in
> > this way. The correct type hint is a generic one:
> >
> > https://docs.python.org/3/library/typing.html#typing.IO
> >
> > And the correct way to encode this on a variable is to let type
> > inference figure it out. You'd use typing.IO or typing.TextIO to
> > annotate a function parameter, perhaps, but don't annotate your
> > variables at all.
> >
> > ChrisA
>
> "And the correct way to encode this on a variable is to let type
> inference figure it out. You'd use typing.IO or typing.TextIO to
> annotate a function parameter, perhaps, but don't annotate your
> variables at all."
>
> I don't know how many times I have to say this, but I've already decided that the types of variables are going to be part of my code base.  Same as every other language I've programmed in, whether statically or dynamically typed.
>
> I don't care if it's 'incorrect' for most purposes.  I'm doing it, for my specific purpose.
>

Oh! Okay then. Just annotate it as "int" - it's no more or less wrong
than any other annotation :) Problem solved!

ChrisA


More information about the Python-list mailing list