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
* First attempt at supporting pyclass(generic) * Include class_geitem in macro * Minor formatting * Pass py argument to new() * Add tests covering pyclass(generic) * Add changelog entry * Add test covering __getitem__ * Fix compilation error tests * Fix Alias -> TypingAlias * Make test case slightly stronger * Add compilation error test * Do not allow pyclass(generic) for enums * Remove unused import * Add import only to 3.9+ * Add documentation * Ignore Python typing hints newly added Rust code * Fix clippy warnings for the test * Add newline * Update TRYBUILD tests * Move `generic` in table * TypingAlias is only available in 3.10+ * Update trybuild test
Copy file name to clipboardExpand all lines: guide/pyclass-parameters.md
+1Lines changed: 1 addition & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -10,6 +10,7 @@
10
10
| <spanstyle="white-space: pre">`extends = BaseType`</span> | Use a custom baseclass. Defaults to [`PyAny`][params-1]|
11
11
| <spanstyle="white-space: pre">`freelist = N`</span> | Implements a [free list][params-2] of size N. This can improve performance for types that are often created and deleted in quick succession. Profile your code to see whether `freelist` is right for you. |
12
12
| <spanstyle="white-space: pre">`frozen`</span> | Declares that your pyclass is immutable. It removes the borrow checker overhead when retrieving a shared reference to the Rust struct, but disables the ability to get a mutable reference. |
13
+
|`generic`| Implements runtime parametrization for the class following [PEP 560](https://peps.python.org/pep-0560/). |
13
14
|`get_all`| Generates getters for all fields of the pyclass. |
14
15
|`hash`| Implements `__hash__` using the `Hash` implementation of the underlying Rust datatype. |
15
16
|`immutable_type`| Makes the type object immutable. Supported on 3.14+ with the `abi3` feature active, or 3.10+ otherwise. |
Copy file name to clipboardExpand all lines: guide/src/python-typing-hints.md
+77-1Lines changed: 77 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -41,7 +41,7 @@ As we can see, those are not full definitions containing implementation, but jus
41
41
42
42
### What do the PEPs say?
43
43
44
-
At the time of writing this documentation, the `pyi` files are referenced in three PEPs.
44
+
At the time of writing this documentation, the `pyi` files are referenced in four PEPs.
45
45
46
46
[PEP8 - Style Guide for Python Code - #Function Annotations](https://www.python.org/dev/peps/pep-0008/#function-annotations) (last point) recommends all third party library creators to provide stub files as the source of knowledge about the package for type checker tools.
47
47
@@ -55,6 +55,8 @@ It contains a specification for them (highly recommended reading, since it conta
55
55
56
56
[PEP561 - Distributing and Packaging Type Information](https://www.python.org/dev/peps/pep-0561/) describes in detail how to build packages that will enable type checking. In particular it contains information about how the stub files must be distributed in order for type checkers to use them.
57
57
58
+
[PEP560 - Core support for typing module and generic types](https://www.python.org/dev/peps/pep-0560/) describes the details on how Python's type system internally supports generics, including both runtime behavior and integration with static type checkers.
59
+
58
60
## How to do it?
59
61
60
62
[PEP561](https://www.python.org/dev/peps/pep-0561/) recognizes three ways of distributing type information:
@@ -165,3 +167,77 @@ class Car:
165
167
:return: the name of the color our great algorithm thinks is the best for this car
166
168
"""
167
169
```
170
+
171
+
### Supporting Generics
172
+
173
+
Type annotations can also be made generic in Python. They are useful for working
174
+
with different types while maintaining type safety. Usually, generic classes
175
+
inherit from the `typing.Generic` metaclass.
176
+
177
+
Take for example the following `.pyi` file that specifies a `Car` that can
0 commit comments