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/types.md
+12-13Lines changed: 12 additions & 13 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -35,28 +35,27 @@ SomeClass(a_number=42)
35
35
36
36
Even when going all-in on type annotations, you will need {func}`attrs.field` for some advanced features, though.
37
37
38
-
One of those features are decorator-based features like defaults.
39
-
It's important to remember that *attrs* doesn't do any magic behind your back here:
40
-
The decorators are implemented using an object that is returned by the call to {func}`attrs.field`.
41
-
42
-
Attributes that only carry a class annotation do not have that object so trying to call a method on it will inevitably fail.
38
+
One of those features is decorator-based functionality like defaults.
39
+
It's important to remember that *attrs* does not perform any hidden magic here:
40
+
The decorators are implemented using the object returned by the call to {func}`attrs.field`.
41
+
Attributes that only carry a class annotation do not have that object, so trying to call a method on it will inevitably fail.
43
42
44
43
45
44
## Forward references
46
45
47
46
Python doesn't allow referencing classes in type annotations that haven't been defined yet.
48
-
Since it's a common requirement in real-world code, though, there's been traditionally the workaround of defining them using string literals:
47
+
Since this is a common requirement in real-world code, the traditional workaround has been defining them using string literals:
49
48
50
49
```python
51
50
classC:
52
51
another_c: "C"
53
52
```
54
53
55
-
This is called a*forward references* ({pep}`526`) and can be enabled automatically for a whole file by using `from __future__ import annotations` ({pep}`563`).
54
+
These are called *forward references* ({pep}`526`) and can be enabled automatically for a whole file by using `from __future__ import annotations` ({pep}`563`).
56
55
57
-
As of Python 3.14 this is not necessary anymore since it introduced [*deferred evaluation of annotations*](https://docs.python.org/3/whatsnew/3.14.html#whatsnew314-deferred-annotations) ({pep}`649` and {pep}`749`) that has a more sophisticated system based on {class}`annotationlib.ForwardRef`s but serves to solve the same problem.
56
+
As of Python 3.14, this is no longer necessary because it introduced [*deferred evaluation of annotations*](https://docs.python.org/3/whatsnew/3.14.html#whatsnew314-deferred-annotations) ({pep}`649` and {pep}`749`) that has a more sophisticated system based on {class}`annotationlib.ForwardRef`s, but ultimately solves the same problem.
58
57
59
-
In both cases, if you need to resolve these to real types, you can call {func}`attrs.resolve_types` which will update the attributes in place.
58
+
In both cases, if you need to resolve these to real types, you can call {func}`attrs.resolve_types`, which will update the attributes in place.
The approach used for `list_of_numbers`one is only available in our [old-style API](names.md) which is why the example still uses it.
104
+
The approach used for `list_of_numbers` is only available in our [old-style API](names.md) which is why the example still uses it.
106
105
107
106
108
107
### Pyright / VS Code
109
108
110
109
*attrs* integrates with Microsoft's [Pyright] via {pep}`681`.
111
-
While Pyright's not as commonly used as a type-checker, it's widely used as the foundation of [VS Code](https://code.visualstudio.com/)'s proprietary [Pylance](https://github.com/microsoft/pylance-release) language server that powers [its Python support](https://marketplace.visualstudio.com/items?itemName=ms-python.python).
110
+
While Pyright is not as commonly used as a standalone typechecker, it's widely used as the foundation of [VS Code](https://code.visualstudio.com/)'s proprietary [Pylance](https://github.com/microsoft/pylance-release) language server that powers [its Python support](https://marketplace.visualstudio.com/items?itemName=ms-python.python).
112
111
113
112
Pyright has grown several *attrs*-specific features over the years, but its inferred types are still a tiny subset of those supported by Mypy, including:
114
113
@@ -121,7 +120,7 @@ Your constructive feedback is welcome in both [attrs#795](https://github.com/pyt
121
120
Keep in mind that the decision on improving *attrs* support in Pyright is entirely Microsoft's prerogative and they unequivocally indicated that they'll only add support for features that go through the PEP process.
122
121
We as the *attrs* project unfortunately have no influence over that.
123
122
124
-
Note that there's a community fork called [*basedpyright*](https://docs.basedpyright.com/) that implements some of Microsoft's closed-source Pylance features, so they're available in other editors like [Zed](https://zed.dev)or other VS Code forks that are not allowed to use Pylance.
123
+
Note that there's a community fork called [*basedpyright*](https://docs.basedpyright.com/) that implements some of Microsoft's closed-source Pylance features, so they're available in other editors like [Zed](https://zed.dev)and VS Code forks that are not allowed to use Pylance.
125
124
Unfortunately, better *attrs* support doesn't appear to be part of their goals.
126
125
127
126
@@ -134,7 +133,7 @@ Currently it only supports {pep}`681`, but [they intend](https://github.com/astr
134
133
### Pyrefly
135
134
136
135
[Pyrefly] is Meta's take on a Rust-based type checker for Python.
137
-
It also only implements {pep}`681` and based on the (lack of) activity on *attrs*-related issues on their bug tracker it doesn't seem like they have immediate ambition to do more than that at the moment.
136
+
It also only implements {pep}`681` and based on the (lack of) activity on *attrs*-related issues on their bug tracker there is currently no indication that they plan to support additional *attrs* features.
0 commit comments