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
+44-3Lines changed: 44 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -3,21 +3,29 @@ 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 dynamically typed language, meaning variable types don't require explicit declaration. However, as projects grow in complexity, type annotations become increasingly valuable for code maintainability and clarity.
16
+
17
+
Type hints have been a major focus of recent Python releases, and I was particularly intrigued when I heard about [Guido van Rossum's work on MyPy at Dropbox](https://blog.dropbox.com/topics/company/thank-you--guido), where the team needed robust tooling to migrate their codebase from Python 2 to Python 3.
18
+
19
+
Today, type hints are essential for modern Python development. They significantly enhance IDE capabilities and AI-powered development tools by providing better code completion, static analysis, and error detection. This mirrors the evolution we've seen with TypeScript's adoption over traditional JavaScript—explicit typing leads to more reliable and maintainable code.
20
+
21
+
<!-- more -->
22
+
13
23
## typing module vs collections module
14
24
15
25
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
26
17
27
Some types like: `typing.Any`, `typing.Generic`, `typing.TypeVar`, etc. are still not deprecated.
18
28
19
-
<!-- more -->
20
-
21
29
### Aliases to Built-in Types
22
30
23
31
| Deprecated Alias | Replacement |
@@ -117,4 +125,37 @@ Some types like: `typing.Any`, `typing.Generic`, `typing.TypeVar`, etc. are stil
117
125
118
126
!!! note "(Python 3.9+) Both `typing.Sequence` and `typing.Collection` are [deprecated aliases](#typing-module-vs-collections-module)."
119
127
120
-
## To be continued
128
+
## Typing tools
129
+
130
+
### MyPy
131
+
132
+
Ref. MyPy in [this post](../2021/2021-01-04-python-lint-and-format.md#mypy).
133
+
134
+
### Pyright && Pylance
135
+
136
+
Ref. Pyright in [this post](../2021/2021-01-04-python-lint-and-format.md#pyright).
137
+
138
+
[Pylance](https://github.com/microsoft/pylance-release#readme) is the Microsoft backed Pyright extension for VSCode.
139
+
140
+
### RightTyper
141
+
142
+
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.
143
+
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.
144
+
145
+
### ty
146
+
147
+
[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.
148
+
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).
149
+
150
+
While Ruff excels at various aspects of Python linting, type checking remains outside its scope.
151
+
ty aims to fill this gap, though it's currently in preview and still evolving toward production readiness.
152
+
The combination of Ruff and ty promises to provide a comprehensive Python code quality toolkit.
153
+
154
+
### pyrefly
155
+
156
+
[pyrefly](https://pyrefly.org/) emerges as another promising entrant in the Python type checking landscape.
157
+
Developed by Meta and also written in Rust, pyrefly offers both type checking capabilities and language server functionality.
158
+
While still in preview, it demonstrates the growing trend of high-performance Python tooling implemented in Rust.
159
+
160
+
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.
161
+
Its backing by Meta suggests potential for robust development and long-term support.
0 commit comments