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: doc/dev/static_type_checking.md
+42-43Lines changed: 42 additions & 43 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -130,30 +130,46 @@ Almost anything can be used as a type in annotations.
130
130
or [collections.abc](https://docs.python.org/3/library/collections.abc.html), or external packages
131
131
3) Types from the [typing](https://docs.python.org/3/library/typing.html)
132
132
or [typing_extensions](https://github.com/python/typing_extensions) modules
133
-
4) Built-in generic types, like `list` or `dict`*.
134
-
> *Note: Supported in Python 3.9+. For <3.9, You must include `from __future__ import annotations` import to be able to pass in generic `list[str]` as a type hint rather than `typing.List[str]`.
133
+
4) Built-in generic types, like `list` or `dict`.
135
134
136
-
Here are a few considerations and notes on Python version support:
135
+
Here are a few considerations and notes on Python version support. The Azure SDK for Python supports Python 3.10 and higher, so the examples in this guide assume Python 3.10 as the baseline.
137
136
138
137
- With [PEP585](https://peps.python.org/pep-0585/) and [PEP563](https://peps.python.org/pep-0563/), importing certain
139
138
generic collection types from `typing` has been [deprecated](https://peps.python.org/pep-0585/#implementation) in
140
139
favor for using the types in `collections.abc` or generic collection type hints (like `list`) from the standard library.
141
-
If you'd like to use types from `collections.abc`, you can check the Python version at runtime (`collections.abc`types did not have [] notation / indexing support added until 3.9).
140
+
Since Python 3.10 is the minimum supported version, you can import directly from `collections.abc`and use `[]`indexing without a version check:
142
141
143
142
```python
144
-
import sys
143
+
from collections.abc import Sequence
144
+
```
145
+
146
+
- The `typing-extensions` library backports types that are introduced only in later versions of Python so that they can be used in earlier Python versions. Since the Azure SDK for Python supports Python 3.10 and higher, most commonly used typing constructs (e.g. `Protocol`, `Literal`, `TypedDict`, `Final`, `runtime_checkable`, `TypeAlias`, `ParamSpec`, `Concatenate`) are available directly from `typing` and do not need `typing-extensions`.
145
147
146
-
if sys.version_info < (3, 9):
147
-
from typing import Sequence
148
-
else:
149
-
from collections.abc import Sequence
148
+
```python
149
+
from typing import Protocol
150
150
```
151
151
152
-
- The `typing-extensions` library backports types that are introduced only in later versions of Python (e.g. `Protocol`
153
-
which was introduced in Python 3.8) so that they can be used in earlier Python versions.
152
+
Features added after 3.10 still require `typing-extensions` on the Python 3.10 baseline, including:
153
+
154
+
-`Self` (3.11)
155
+
-`LiteralString` (3.11)
156
+
-`Required` / `NotRequired` for `TypedDict` (3.11)
- PEP 695 `type` statement / `TypeAliasType` (3.12)
163
+
-`ReadOnly` for `TypedDict` (3.13)
154
164
155
165
```python
156
-
from typing_extensions import Protocol
166
+
# Self was added to typing in 3.11, so import from typing_extensions on 3.10
167
+
from typing_extensions import Self
168
+
169
+
classTree:
170
+
@classmethod
171
+
defbuild(cls) -> Self:
172
+
returncls()
157
173
```
158
174
159
175
If using `typing-extensions`, you must add it to the install dependencies for your library (do not rely on install by `azure-core`)
@@ -163,8 +179,7 @@ from `typing` if supported, or `typing-extensions` if the Python version is too
163
179
check yourself.
164
180
165
181
See the [typing-extensions](https://github.com/python/typing_extensions) docs to check what has been
166
-
backported. This document calls out which types are needed through `typing-extensions` based on support of Python 3.7+.
167
-
Some commonly used types imported from `typing-extensions` are Literal, TypedDict, Protocol, runtime_checkable, and Final.
182
+
backported. This document calls out which types are needed through `typing-extensions` only when they are not yet available in the standard `typing` module on Python 3.10.
168
183
169
184
## Install and run type checkers on your client library code
170
185
@@ -382,7 +397,7 @@ def begin_export_project(
382
397
383
398
Per PEP 484, type checkers have moved towards requiring the Optional type to be made explicit when a default of None is provided.
384
399
385
-
> Note: The `X | None` syntax is only supported in Python 3.10+.
400
+
> Note: Since Python 3.10 is the minimum supported version, you can also use the `X | None` syntax (PEP 604) instead of `Optional[X]`.
386
401
387
402
### Typing for collections
388
403
@@ -422,11 +437,10 @@ specified a generic `typing.Dict` here, the `entity` passed must be a `dict` or
422
437
accepted.
423
438
424
439
With [PEP 589](https://peps.python.org/pep-0589/), a `TypedDict` was introduced in Python 3.8 which allows you to add
425
-
type hints for dictionaries with a fixed set of keys. The syntax for this looks similar to building a dataclass:
440
+
type hints for dictionaries with a fixed set of keys. It is available directly from `typing` in Python 3.10. The syntax for this looks similar to building a dataclass:
426
441
427
442
```python
428
-
# from typing import TypedDict # Python >= 3.8
429
-
from typing_extensions import TypedDict
443
+
from typing import TypedDict
430
444
431
445
432
446
classEmployee(TypedDict):
@@ -464,11 +478,10 @@ e.g. `isinstance(employee, Employee)` will throw a `TypeError`. Usage of `TypedD
464
478
type checkers and Intellisense how a specific dict should be constructed and help the type checkers warn users if
465
479
they try to access keys which don't exist.
466
480
467
-
Use of generic TypedDict is also supported via typing_extensions (>=4.3.0). For example,
481
+
Generic `TypedDict` is supported in Python 3.11+. On Python 3.10, use `typing_extensions` (>=4.3.0) to use generic `TypedDict`. For example,
468
482
469
483
```python
470
-
# from typing import TypedDict # Python >= 3.8
471
-
from typing_extensions import TypedDict
484
+
from typing_extensions import TypedDict # use typing_extensions for generic TypedDict on 3.10
Therefore, it is preferred to use `typing.Literal` in this situation to provide the best type checking experience for our users.
1363
1365
1364
-
> Note that Literal is backported to older versions of Python by using typing_extensions.
1365
-
1366
1366
### Use typing.NewType to restrict a type to a specific context
1367
1367
1368
1368
`NewType` can be useful to catch errors where a particular type is expected. `NewType` will take an existing type and
@@ -1398,8 +1398,7 @@ At runtime, `NewType` will return an object that returns its argument when calle
1398
1398
It is best used when a variable's scope spans a large amount of modules and you want to ensure that it stays immutable (i.e. no line of code tries to change it).
1399
1399
1400
1400
```python
1401
-
# from typing import Final Python >=3.8
1402
-
from typing_extensions import Final
1401
+
from typing import Final
1403
1402
1404
1403
MAX_BLOB_SIZE: Final =4*1024*1024
1405
1404
```
@@ -1414,7 +1413,7 @@ Found 1 error in 1 file (checked 1 source file)
1414
1413
Additionally, If you have a method that should not be overridden or a class that should not be subclassed, consider decorating with `@final`.
Copy file name to clipboardExpand all lines: doc/dev/static_type_checking_cheat_sheet.md
+12-7Lines changed: 12 additions & 7 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -85,14 +85,19 @@ class Tree:
85
85
- Do use the latest typing features available. If not supported by older versions of Python, consider taking a dependency and importing from `typing-extensions`.
86
86
87
87
```python
88
-
# from typing import TypedDict Python >3.8
89
-
from typing_extensions import TypedDict
88
+
# Self was added to typing in 3.11, so import from typing_extensions on 3.10
89
+
from typing_extensions import Self
90
+
91
+
classTree:
92
+
@classmethod
93
+
defbuild(cls) -> Self:
94
+
returncls()
90
95
```
91
96
92
97
### Importing types
93
98
94
99
- Do use only publicly exposed client library types in type hints.
95
-
- Do import types from modules such as `typing`, `typing_extensions`, `collections`, and `collections.abc`(Note that indexing support for generic collection types from `collections.abc`is only supported on Python 3.9+).
100
+
- Do import types from modules such as `typing`, `typing_extensions`, `collections`, and `collections.abc`. Generic collection types from `collections.abc`support `[]` indexing on Python 3.10 (the minimum supported version).
96
101
- Do not import regular type hints under a `typing.TYPE_CHECKING` block. You may use `TYPE_CHECKING` to fix a circular import or avoid importing a type only needed in type annotations that is otherwise costly to load at runtime.
97
102
98
103
```python
@@ -278,7 +283,7 @@ class Triangle:
278
283
- Mark your `Protocol`s as `@runtime_checkable` so users can use them in `isinstance` and `issubclass` checks.
Copy file name to clipboardExpand all lines: doc/python_version_support_policy.md
+9-7Lines changed: 9 additions & 7 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,16 +4,18 @@ This page describes the Python version support policy for the Azure SDK for Pyth
4
4
5
5
End of support means, in the SDK context, that new features will not be supported for those unsupported Python versions. It will still be possible to install older SDK versions from PyPI as necessary.
6
6
7
+
> **Note:** The "SDKs End Of Support" date is inclusive — the listed day is the last supported day, and the next day is the first unsupported day.
8
+
7
9
| Python Version | PSF End of Support | SDKs End Of Support |
0 commit comments