Python type hints in Plone

I'm distracted and wanted to see if anyone was masochistic enough to even begin to ask this question - can Plone Code use Python Type Hints?

If you're not sure what I'm talking about, then you may want to read about it....

https://docs.python.org/3/library/typing.html

Now, I actually have put a whole 10 minutes worth of thought into this topic, and my conclusion was hell no. But I want to know your opinion.

I searched this forum and found something I thought was a glimmer of hope. @jensens working on type hints in the restAPI? Could it be?

Nope. Stupid search engine.

So, ultimately, I'm thinking when we use native python types - lists, strings, dicts and stuff - yea, this is easy. But when you start getting into OFS or btrees or lazy maps... then into ZCA, and eventually to Dexterity Types and z3c.formgen and oh my god....

Has anyone even thought about how to even begin thinking about typehints in Plone? Typehints with ZCA?

To me, it looks like a big 'nope, nope, Nope' or a graduate thesis.

Maybe a fun Google Summer of Code project to throw at an eager young programmer?

Thoughts?

Imho type hints make not much sense when the underlying framework/libraries are not typed.

This is my experience from applying type hints in my Flask app and my library before Flask was typed, and from using type hints for my Zope app, as Zope is not typed.

Once Flask provided type annotations, I had to revise all my type hints as most were wrong then.

And oh wow, would I have loved type hints when migrating my Zope app from Python 2 to 3. Zope's paths are sometimes str and sometimes bytes.

And as you mentioned, this would be both an extreme huge and complex task.

1 Like

You make a really good point here, and I kind of saw it myself, but didn't connect the dots until you said it.

Typing is, by it's very nature, a bottom-up job.

Plone will never have type hints, and shouldn't even think about it, until Zope (specifically the ZCA and Zope.Interface I think) dig in to type hinting. And that's just one piece of the framework. I'm fairly certain that Plone relies on other "High Level Types" from other dependencies I don't even know about.

I think the waters are also muddied because of the ZCA / MetaClass schism that seemed to have occurred on or around PEP 245 https://www.python.org/dev/peps/pep-0245/#id9

The Python Types library probably plays very well with 'abc' and ZCA wasn't even considered. I don't know. I'm just guessing here.


Edit:
Woah, have you read PEP 544 – Protocols: Structural subtyping (static duck typing) | peps.python.org

On the other hand, Zope interfaces are conceptually a superset of protocols defined here, but using an incompatible syntax to define them, because before PEP 526 there was no straightforward way to annotate attributes. In the 3.6+ world, zope.interface might potentially adopt the Protocol syntax. In this case, type checkers could be taught to recognize interfaces as protocols and make simple structural checks with respect to them.

While I see the problems @jugmac00 wrote about, I also feel here a bit different about it.

I think all packages for Plone 6 only may introduce type hints (Python 3.7+ style) at least for their internal structures. I started using type hints for some add-ons and it helped detecting problems early already in the IDE. Indeed, it would not help with outside APIs and type hints won't unleash full power.

Also, Plone has some very basic packages where we can start.

Question: Any good idea how to type-hint ZCA?

1 Like

"Protocols".
Note that I am proudly using this as a buzzword without any of the understanding behind it.

I saw PEP 544 as an interesting read here, but it's more of a ZCA re-write to cure the Zope-Python Rift that formed.