Skip to content

Commit 82a81e0

Browse files
docs: add mypy plugin configuration note for param (#8614) (#8734)
Co-authored-by: Rimsha Fareed <rimsha152007.com>
1 parent 0ebba55 commit 82a81e0

3 files changed

Lines changed: 62 additions & 0 deletions

File tree

doc/how_to/index.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,13 @@ How to effectively develop apps in a notebook environment.
2121
How to effectively develop apps in a Python or Markdown file.
2222
:::
2323

24+
:::{grid-item-card} {octicon}`file-code;2.5em;sd-mr-1 sd-animate-grow50` Type check your code
25+
:link: typing/index
26+
:link-type: doc
27+
28+
How to configure static type checkers like mypy to work with Param and Panel.
29+
:::
30+
2431
::::
2532

2633
## Build apps

doc/how_to/prepare_to_develop.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,5 @@
1212
1313
Develop in a notebook<notebook/index>
1414
Develop in an editor<editor/index>
15+
Type check your code<typing/index>
1516
```

doc/how_to/typing/index.md

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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

Comments
 (0)