Adding type hints to OpenStack (starting with Oslo)

Stephen Finucane sfinucan at redhat.com
Mon Jan 28 10:18:41 UTC 2019


On Sat, 2019-01-26 at 23:27 +0000, Sean Mooney wrote:
> On Sat, 2019-01-26 at 13:29 -0500, Doug Hellmann wrote:
> > I agree with your concerns about code churn. I think it’s still useful to run a small experiment to demonstrate how we
> > might actually benefit from them though, which applying them to one library would let us do. 
> > 
> > On Jan 26, 2019, at 1:18 PM, Morgan Fainberg <morgan.fainberg at gmail.com> wrote:
> > 
> > > Honestly, I would wait for this for the U release. We can then go to the annotation style and if needed update
> > > docstrings at the same time. My concern with pushing this now is a high churn of code that should be duplicated once
> > > we drop PY2 support.
> > > 
> > > Other than my above concerns the type checking in mypy would be great to have. 
> > > 
> > > --Morgan
> > > 
> > > On Sat, Jan 26, 2019, 09:14 Sergey Vilgelm <sergey at vilgelm.info wrote:
> > > > Cyril,
> > > > 
> > > > We have lots of doc strings in rst format and many of them already have the `@param` and `@type` tags. Does the
> > > > mypy support this format or we should to rewrite doc string and add the `type:` tags just for mypy?
> > > > 
> > > > --
> > > > Sergey Vilgelm
> > > > https://www.vilgelm.info
> > > > On Jan 26, 2019, 9:07 AM -0600, Cyril Roelandt <cyril at redhat.com>, wrote:
> > > > > Hello Doug,
> > > > > 
> > > > > On 01/26/19 09:02, Doug Hellmann wrote:
> > > > > > We're still required to support python 2 through the beginning of the U
> > > > > > cycle [5]. Is it possible to apply the type hints in a way that allows
> > > > > > us to maintain that support?
> > > > > > 
> > > > > > [5] https://governance.openstack.org/tc/resolutions/20180529-python2-deprecation-timeline.html
> > > > >  
> > > > > There are two ways to apply type hints:
> > > > > 1) Use a new syntax that only works with Python 3
> > > > > 2) Use comments (see my github branch, where comments starting with
> > > > > "type:" can be found)
> there is also a third, looping stephen into the conversation.
> stephen did some experiments in this regard in the past.
> the commet form is backwards compatiabl but you can also put thetype hints in 
> a seperate .pyi file. The .pyi file version has the advantage of also working for c modules
> and not modifying any of the existing code.
> 
> we previously disscused the idea of using 
> https://github.com/Instagram/MonkeyType
> to auto discover the types of existing libs and generate the stub .pyi files.
> ideally we should just be able to run our existing unit/functional test under mockeytype 
> to discover the relevent types and create the pyi files.
> 
> i dont think stephen or i had the chance to persue that since we discuseed it in denver.

Indeed, I had some patches proposed against nova using the comment-
style syntax that were met with a resounding meh [1]. I also started
working on oslo changes but got stuck with 'oslo.versionedobject',
which does funky stuff with fields that seemed to break mypy's
introspection (or whatever it's doing). If you were to follow through
on this, I would personally start with that particular library (plus a
consumer of said library) to see if you could figure out some of the
quirks here. Personally, I've basically given up on all of this until U
when I can use the Python 3.2+ annotation syntax. I would be happy to
review any changes of yours though and I'm pretty sure we could script
the conversion from comment-type annotation to native annotations (I
think the Instagram or Dropbox devs published additional tools to do
just this).

Stephen

[1] https://review.openstack.org/#/q/topic:bp/integrate-mypy-type-checking

> > > > > I used the second approach in order to not break Python 2 compatibility.
> > > > > 
> > > > > 
> > > > > Regards,
> > > > > Cyril
> > > > > 





More information about the openstack-discuss mailing list