|
| 1 | +# Type Checking with Param and Panel |
| 2 | + |
| 3 | +This guide covers how to configure static type checkers (like mypy) when working with Panel and Param. |
| 4 | + |
| 5 | +## Why this matters |
| 6 | + |
| 7 | +Panel is built on top of [Param](https://param.holoviz.org), which uses a custom metaclass to route class-level parameter assignment through Python's descriptor protocol. Static type checkers like mypy don't understand this pattern out of the box, and will reject valid code such as: |
| 8 | + |
| 9 | +```python |
| 10 | +import panel as pn |
| 11 | + |
| 12 | +pn.chat.ChatMessage.show_reaction_icons = False |
| 13 | +``` |
| 14 | + |
| 15 | +Running mypy on this without the plugin produces an error: |
| 16 | + |
| 17 | +```console |
| 18 | +$ mypy script.py |
| 19 | +script.py:3: error: Incompatible types in assignment (expression has type "bool", variable has type "Boolean[bool]") [assignment] |
| 20 | +Found 1 error in 1 file (checked 1 source file) |
| 21 | +``` |
| 22 | + |
| 23 | +This is a real issue for projects that run mypy in CI/CD, since it can block releases or force you to spend time silencing false-positive errors. |
| 24 | + |
| 25 | +To fix this, Param ships a dedicated mypy plugin. |
| 26 | + |
| 27 | +## Enabling the Param mypy plugin |
| 28 | + |
| 29 | +Add the following to your `pyproject.toml`: |
| 30 | + |
| 31 | +```toml |
| 32 | +[tool.mypy] |
| 33 | +plugins = ["param.mypy_plugin"] |
| 34 | +``` |
| 35 | + |
| 36 | +Or, if you're using `mypy.ini` / `setup.cfg`: |
| 37 | + |
| 38 | +```ini |
| 39 | +[mypy] |
| 40 | +plugins = param.mypy_plugin |
| 41 | +``` |
| 42 | + |
| 43 | +With the plugin enabled, mypy correctly understands that assignments like `pn.chat.ChatMessage.show_reaction_icons = False` set the parameter's default value, and type-checks them accordingly — no more false-positive errors in CI. |
| 44 | + |
| 45 | +## Learn more |
| 46 | + |
| 47 | +Param's user guide has a full [Typing guide](https://param.holoviz.org/en/latest/user_guide/Typing.html) covering: |
| 48 | + |
| 49 | +- Type inference from Parameter types |
| 50 | +- Choice of type checker (mypy, basedpyright) |
| 51 | +- Known limitations |
| 52 | +- Practical recommendations |
| 53 | + |
| 54 | +If you're setting up type checking for a Panel project, that guide is the best place to go for details beyond the mypy plugin itself. |
0 commit comments