@@ -74,8 +74,11 @@ New syntax features:
7474
7575New typing features:
7676
77- * :pep: `673 `: ``Self `` Type.
78- (Contributed by James Hilton-Balfe and Pradeep Kumar in :issue: `30924 `.)
77+ * :pep: `646 `: Variadic generics.
78+ * :pep: `655 `: Marking individual TypedDict items as required or potentially-missing.
79+ * :pep: `673 `: ``Self `` type.
80+ * :pep: `675 `: Arbitrary literal string type.
81+
7982
8083New Features
8184============
@@ -167,6 +170,134 @@ default traceback. See :pep:`678` for more details. (Contributed by
167170Irit Katriel in :issue: `45607 `.)
168171
169172
173+ .. _new-feat-related-type-hints-311 :
174+
175+ New Features Related to Type Hints
176+ ==================================
177+
178+ This section covers major changes affecting :pep: `484 ` type hints and
179+ the :mod: `typing ` module.
180+
181+ PEP 646: Variadic generics
182+ --------------------------
183+
184+ :pep: `484 ` introduced :data: `~typing.TypeVar `, enabling creation
185+ of generics parameterised with a single type. :pep: `646 ` introduces
186+ :data: `~typing.TypeVarTuple `, enabling parameterisation
187+ with an *arbitrary * number of types. In other words,
188+ a :data: `~typing.TypeVarTuple ` is a *variadic * type variable,
189+ enabling *variadic * generics. This enables a wide variety of use cases.
190+ In particular, it allows the type of array-like structures
191+ in numerical computing libraries such as NumPy and TensorFlow to be
192+ parameterised with the array *shape *. Static type checkers will now
193+ be able to catch shape-related bugs in code that uses these libraries.
194+
195+ See :pep: `646 ` for more details.
196+
197+ (Contributed by Matthew Rahtz in :issue: `43224 `, with contributions by
198+ Serhiy Storchaka and Jelle Zijlstra. PEP written by Mark Mendoza, Matthew
199+ Rahtz, Pradeep Kumar Srinivasan, and Vincent Siles.)
200+
201+ PEP 655: Marking individual ``TypedDict `` items as required or not-required
202+ ---------------------------------------------------------------------------
203+
204+ :data: `~typing.Required ` and :data: `~typing.NotRequired ` provide a
205+ straightforward way to mark whether individual items in a
206+ :data: `~typing.TypedDict ` must be present. Previously this was only possible
207+ using inheritance.
208+
209+ Fields are still required by default, unless the ``total=False ``
210+ parameter is set.
211+ For example, the following specifies a dictionary with one required and
212+ one not-required key::
213+
214+ class Movie(TypedDict):
215+ title: str
216+ year: NotRequired[int]
217+
218+ m1: Movie = {"title": "Black Panther", "year": 2018} # ok
219+ m2: Movie = {"title": "Star Wars"} # ok (year is not required)
220+ m3: Movie = {"year": 2022} # error (missing required field title)
221+
222+ The following definition is equivalent::
223+
224+ class Movie(TypedDict, total=False):
225+ title: Required[str]
226+ year: int
227+
228+ See :pep: `655 ` for more details.
229+
230+ (Contributed by David Foster and Jelle Zijlstra in :issue: `47087 `. PEP
231+ written by David Foster.)
232+
233+ PEP 673: ``Self `` type
234+ ----------------------
235+
236+ The new :data: `~typing.Self ` annotation provides a simple and intuitive
237+ way to annotate methods that return an instance of their class. This
238+ behaves the same as the :data: `~typing.TypeVar `-based approach specified
239+ in :pep: `484 ` but is more concise and easier to follow.
240+
241+ Common use cases include alternative constructors provided as classmethods
242+ and :meth: `~object.__enter__ ` methods that return ``self ``::
243+
244+ class MyLock:
245+ def __enter__(self) -> Self:
246+ self.lock()
247+ return self
248+
249+ ...
250+
251+ class MyInt:
252+ @classmethod
253+ def fromhex(cls, s: str) -> Self:
254+ return cls(int(s, 16))
255+
256+ ...
257+
258+ :data: `~typing.Self ` can also be used to annotate method parameters
259+ or attributes of the same type as their enclosing class.
260+
261+ See :pep: `673 ` for more details.
262+
263+ (Contributed by James Hilton-Balfe in :issue: `46534 `. PEP written by
264+ Pradeep Kumar Srinivasan and James Hilton-Balfe.)
265+
266+ PEP 675: Arbitrary literal string type
267+ --------------------------------------
268+
269+ The new :data: `~typing.LiteralString ` annotation may be used to indicate
270+ that a function parameter can be of any literal string type. This allows
271+ a function to accept arbitrary literal string types, as well as strings
272+ created from other literal strings. Type checkers can then
273+ enforce that sensitive functions, such as those that execute SQL
274+ statements or shell commands, are called only with static arguments,
275+ providing protection against injection attacks.
276+
277+ For example, a SQL query function could be annotated as follows::
278+
279+ def run_query(sql: LiteralString) -> ...
280+ ...
281+
282+ def caller(
283+ arbitrary_string: str,
284+ query_string: LiteralString,
285+ table_name: LiteralString,
286+ ) -> None:
287+ run_query("SELECT * FROM students") # ok
288+ run_query(query_string) # ok
289+ run_query("SELECT * FROM " + table_name) # ok
290+ run_query(arbitrary_string) # type checker error
291+ run_query( # type checker error
292+ f"SELECT * FROM students WHERE name = {arbitrary_string}"
293+ )
294+
295+ See :pep: `675 ` for more details.
296+
297+ (Contributed by Jelle Zijlstra in :issue: `47088 `. PEP written by Pradeep
298+ Kumar Srinivasan and Graham Bleaney.)
299+
300+
170301Other Language Changes
171302======================
172303
0 commit comments