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
+43-3Lines changed: 43 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -3,21 +3,28 @@ authors:
3
3
- copdips
4
4
categories:
5
5
- python
6
+
- linter
6
7
comments: true
7
8
date:
8
9
created: 2025-02-01
10
+
updated: 2025-06-28
9
11
---
10
12
11
13
# Python Type Hints
12
14
15
+
Python is a dynamic language, which means you don't need to declare the type of a variable. But in reality, once you go for some complexity previously (even for small project nowadays), you need to declare the type of a variable.
16
+
One of biggest focus of recent Python releases is to improve the type hints. My first memory of typing hint is back to Guido when I heard about he started developing the MyPy module for Dropbox, who needed to migrated code base from Python 2 to Python 3. (Most of this team moved to Microsoft now.)
17
+
Typing hint becomes more important when we start building applications (even scripts) with IDE, or using AI tools, typing hint help the IDE and AI to understand much better the code base, so provide better code completion, code analysis, etc.
18
+
We can see the same thing in TypeScript vs traditional JavaScript.
19
+
20
+
<!-- more -->
21
+
13
22
## typing module vs collections module
14
23
15
24
Since Python 3.9, most of types in `typing` module is [deprecated](https://docs.python.org/3/library/typing.html#deprecated-aliases), and `collections` module is recommended.
16
25
17
26
Some types like: `typing.Any`, `typing.Generic`, `typing.TypeVar`, etc. are still not deprecated.
18
27
19
-
<!-- more -->
20
-
21
28
### Aliases to Built-in Types
22
29
23
30
| Deprecated Alias | Replacement |
@@ -117,4 +124,37 @@ Some types like: `typing.Any`, `typing.Generic`, `typing.TypeVar`, etc. are stil
117
124
118
125
!!! note "(Python 3.9+) Both `typing.Sequence` and `typing.Collection` are [deprecated aliases](#typing-module-vs-collections-module)."
119
126
120
-
## To be continued
127
+
## Typing tools
128
+
129
+
### MyPy
130
+
131
+
Ref. MyPy in [this post](../2021/2021-01-04-python-lint-and-format.md#mypy).
132
+
133
+
### Pyright && Pylance
134
+
135
+
Ref. Pyright in [this post](../2021/2021-01-04-python-lint-and-format.md#pyright).
136
+
137
+
[Pylance](https://github.com/microsoft/pylance-release#readme) is the Microsoft backed Pyright extension for VSCode.
138
+
139
+
### RightTyper
140
+
141
+
During an internal tech demo at my working, I heard about [RightTyper](https://github.com/RightTyper/RightTyper), a Python tool that generates type annotations for function arguments and return values.
142
+
It’s important to note that RightTyper doesn’t statically parse your Python files to add types; instead, it needs to run your code to detect types on the fly. So, one of the best ways to use RightTyper is with python `-m pytest`, assuming you have good test coverage.
143
+
144
+
### ty
145
+
146
+
[ty](https://github.com/astral-sh/ty) represents the next generation of Python type checking tools. Developed by the team behind the popular [ruff](https://docs.astral.sh/ruff/) linter, ty is implemented in Rust for exceptional performance.
147
+
It functions both as a type checker and language server, offering seamless integration through its dedicated [VSCode extension ty-vscode](https://github.com/astral-sh/ty-vscode).
148
+
149
+
While Ruff excels at various aspects of Python linting, type checking remains outside its scope.
150
+
ty aims to fill this gap, though it's currently in preview and still evolving toward production readiness.
151
+
The combination of Ruff and ty promises to provide a comprehensive Python code quality toolkit.
152
+
153
+
### pyrefly
154
+
155
+
[pyrefly](https://pyrefly.org/) emerges as another promising entrant in the Python type checking landscape.
156
+
Developed by Meta and also written in Rust, pyrefly offers both type checking capabilities and language server functionality.
157
+
While still in preview, it demonstrates the growing trend of high-performance Python tooling implemented in Rust.
158
+
159
+
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.
160
+
Its backing by Meta suggests potential for robust development and long-term support.
0 commit comments