You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/posts/2025/2025-02-01-python-type-hints.md
+150-3Lines changed: 150 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -490,7 +490,6 @@ If a directory contains both a `.py` and a `.pyi` file for the same module, the
490
490
491
491
Mypy also ships with two tools for making it easier to create and maintain stubs: [Automatic stub generation (stubgen)](https://mypy.readthedocs.io/en/stable/stubgen.html#stubgen) and [Automatic stub testing (stubtest)](https://mypy.readthedocs.io/en/stable/stubtest.html#stubtest).
492
492
493
-
494
493
```bash title="use stubgen to generate stub files for package my_pkg_dir"
495
494
# default output dir is: out, use -o to change it
496
495
stubgen my_pkg_dir -o stubs
@@ -501,16 +500,53 @@ stubgen my_pkg_dir -o stubs
501
500
pyright --createstub my_pkg_dir
502
501
```
503
502
504
-
A common problem with stub files is that they tend to diverge from the actual implementation. Mypy includes the stubtest tool that can automatically check for discrepancies between the stubs and the implementation at runtime.
503
+
A common problem with stub files is that they tend to diverge from the actual implementation. Mypy includes the [stubtest](https://mypy.readthedocs.io/en/stable/stubtest.html#stubtest) tool that can automatically check for discrepancies between the stubs and the implementation at runtime.
504
+
505
+
```bash
506
+
MYPYPATH=stubs stubtest my_pkg_dir --concise
507
+
```
508
+
509
+
## Generics
510
+
511
+
`list`, `set`, `dict`, etc, all the built-in collection classes are all Generics type, as they accept one or more type arguments within [...], which can be arbitrary types.
512
+
For example, the type `dict[int, str]` has the type arguments `int` and `str`, and `list[int]` has the type argument `int`.
Ref. MyPy in [this post](../2021/2021-01-04-python-lint-and-format.md#mypy).
513
545
546
+
While MyPy may not be the most performant type checker, particularly when integrated into pre-commit hooks, it remains an invaluable learning resource.
547
+
The [MyPy documentation](https://mypy.readthedocs.io/en/stable/index.html) provides comprehensive guidance on writing effective type hints. Understanding its development history and current maintainership adds valuable context to its role in the Python ecosystem.
548
+
And this posts is mainly based on MyPy documentation.
549
+
514
550
### Pyright && Pylance
515
551
516
552
Ref. Pyright in [this post](../2021/2021-01-04-python-lint-and-format.md#pyright).
@@ -540,4 +576,115 @@ While still in preview, it demonstrates the growing trend of high-performance Py
540
576
The tool integrates smoothly with modern development environments through its [VSCode extension refly-vscode](https://marketplace.visualstudio.com/items?itemName=meta.pyrefly), making it accessible to a wide range of developers.
541
577
Its backing by Meta suggests potential for robust development and long-term support.
542
578
543
-
Just a quick test, **pyrefly** seems to generate more typing errors than **ty**.
579
+
### ty vs pyrefly
580
+
581
+
**pyrefly** has very similar output format to **ty**, but after a quick test, it seems that it generates more alerts than **ty** for the same codebase. It doesn't mean **pyrefly** is more powerful or more strict. Sometimes it just generates more false positives as it's still in preview.
582
+
583
+
And test on a single [Generics](https://mypy.readthedocs.io/en/stable/generics.html#generics) type, both **ty** and **pyrefly** generate the same type errors, but **ty** can point to the exact position of the error, while **pyrefly** only points to a limited position.
584
+
585
+
Test code:
586
+
587
+
```python title="mypy_demo.py" linenums="1"
588
+
--8<--"scripts/2025/mypy_demo.py"
589
+
```
590
+
591
+
Test results:
592
+
593
+
=== "ty output"
594
+
595
+
```bash
596
+
$ ty --version
597
+
ty 0.0.1-alpha.14
598
+
599
+
$ ty check scripts/2025/mypy_demo.py
600
+
WARN ty is pre-release software and not ready for production use. Expect to encounter bugs, missing features, and fatal errors.
601
+
error[unsupported-operator]: Operator `+` is unsupported between objects of type `S` and `S`
0 commit comments