Trio-typing: new package of static type hints for Trio and friends

I’ve been writing Trio-using code using static type annotations (PEP 484 / mypy / etc) for a few months, and have finally gotten around to packaging the Trio stubs I wrote with a mypy plugin that smooths over some pitfalls. You can get it on PyPI: pip install trio-typing. It comes with stubs for trio, outcome, and async_generator, a package trio_typing from which you can import type names like Nursery and TaskStatus, and a mypy plugin trio_typing.plugin. See https://github.com/python-trio/trio-typing/blob/master/README.rst for more details.

In the long run I suspect we’d like to have these integrated into Trio, but Trio is a little too dynamic for that to be an easy task at the moment, and I wanted to let other folks start building statically-typed Trio applications in the meantime. Feedback and experience reports would be much appreciated!

You’ll need trio v0.11.0 (released yesterday) or later, to support writing trio.CancelScope and trio.abc.SendChannel[SomeObjectType].

3 Likes

Wow, that’s really impressive, especially the plugin.

I was surprised to learn that mypy doesn’t understand functools.partial… it looks like they they’d be happy to add a plugin if someone submitted one.

Do you want to move this into the python-trio org?

I started working on a plugin to support partial but ran into too many issues for the time I had available. I do hope to add that support eventually!

I’d be happy to move this into python-trio if there’s someone else who’s willing to review changes to the mypy plugin that might be needed or wanted in the future. I wasn’t sure if such a person existed, and I didn’t want to sign anyone up for the “self-taught familiarization with mypy internals” crash course without their consent. :slight_smile:

Eh, I wouldn’t worry about it. It’s surprising how much better even perfunctory review is compared to no review :slight_smile: And if there’s really no-one willing to review, then we can always change the rules; the rules are supposed to help us, not hamstring us.

Sounds good - I’ve moved it over!

2 Likes

trio-typing v0.3.0 has been released to PyPI with support for APIs added in Trio 0.12.0. It also contains some updates to support new versions of mypy; it now targets mypy 0.740, released today.

3 Likes