[Python-ideas] Optional static typing -- late to the party

Steven D'Aprano steve at pearwood.info
Thu Aug 21 02:09:54 CEST 2014


On Thu, Aug 21, 2014 at 06:29:16AM +1000, Ben Finney wrote:
> Chris Angelico <rosuav at gmail.com> writes:
> 
> > On Wed, Aug 20, 2014 at 10:53 PM, Ben Finney <ben+python at benfinney.id.au> wrote:
> > > Steven D'Aprano <steve at pearwood.info> writes:
> > >
> > >> But remember that any annotations in docstrings may not be
> > >> available at runtime
> > >
> > > How so? What conformant Python implementation is discarding
> > > docstrings from code objects?
> >
> > CPython with the -OO flag. Or is that non-conformant?
> 
> Okay, one can deliberately remove docstrings with an option to the
> interpreter (the “-OO” option explicitly has that purpose). So a user
> wanting to check annotations in docstrings wouldn't use that option. So
> this case doesn't seem relevant to the discussion.

But "the user" can be two different people: the writer of the code using 
the annotations, and the person running the code that wants to use 
annotations. It's the *second* user, not the first, who decides whether 
or not to use -OO.

If you're the original developer, you might insist that your code is not 
compatible with -OO, but (in my opinion) that's a mean thing to do if it 
can be avoided. Better to not rely on docstrings for anything other than 
documentation.


> Remember that we're talking about type annotations that are for
> *static code checkers* to inspect. Docstrings will certainly be
> available there.

True, but runtime checks may (or may not) be a part of the code checker. 
At least, we should not rule that out.


-- 
Steven


More information about the Python-ideas mailing list