DEV Community

Wu Haotian
Wu Haotian

Posted on • Originally published at blog.whtsky.me on

2 1

Don't forget `py.typed` for your typed Python package

Intro

pixelmatch-py, a Python library I maintained for comparing images, received a Pull Request to add type hints about 10 months ago. After merging this PR & releasing a new version, I thought the library's users will magically get their type working.

Until today, I was reading cryptography's Changelog and a line got my attention:

Added a py.typed file so that mypy will know to use our type annotations.

After reading PEP-561 and mypy documentation I'm sure that I didn't publish the package right: I should include a py.typed file, or the type checker won't use the type hints provided by the package.

Adding py.typed

It's faily simple to include this file: just touch a py.typed file in your package directory and include it in your distribution.

I'm using poetry, so I added

packages = [
  {include = "pixelmatch"},
  {include = "pixelmatch/py.typed"},
]

Enter fullscreen mode Exit fullscreen mode

under the [tool.poetry] section of pyproject.toml.

If you're using setup.py, you can add package_data to setup call:

setup(
    package_data={"pixelmatch": ["py.typed"]},
)

Enter fullscreen mode Exit fullscreen mode

Release a new version for your package then type informations from your packages should works.

Fin

If you're a Python package maintainer, be sure to include py.typed file for your typed package!

Image of Datadog

How to Diagram Your Cloud Architecture

Cloud architecture diagrams provide critical visibility into the resources in your environment and how they’re connected. In our latest eBook, AWS Solution Architects Jason Mimick and James Wenzel walk through best practices on how to build effective and professional diagrams.

Download the Free eBook

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay