After a year of development, a significant release of Programming Language Python 3.11 has been published. This new branch will be supported for one and a half years, after which it will receive patches for vulnerability fixes for another three and a half years.
At the same time, alpha testing of the Python 3.12 branch has begun (according to the new development schedule, work on a new branch starts five months before the release of the previous branch and reaches the alpha testing stage by the time of the next release). The Python 3.12 branch will be in the alpha release stage for seven months, during which new features will be added and bugs will be fixed. After that, beta testing will be conducted for three months, during which the addition of new features will be prohibited and the focus will be entirely on bug fixes. The last two months before the release will be in the release candidate stage, where final stabilization will take place.
Among the new features added in Python 3.11:
- Significant work has been done to optimize performance. The new branch includes changes related to speeding up and inlining function call execution, applying fast interpreters for type operations (x+x, x*x, x-x, a[i], a[i] = z, f(arg) C(arg), o.method(), o.attr = z, *seq), as well as optimizations prepared by the Cinder and HotPy projects. Depending on the type of load, there is a speed increase of 10-60%. On average, performance during the pyperformance test suite increased by 25%.
The bytecode caching mechanism has been redesigned, allowing interpreter startup time to be reduced by 10-15%. Objects with code and bytecode are now statically allocated by the interpreter, which has eliminated the stages of demarshaling cached bytecode and transforming code objects for allocation in dynamic memory.
- When displaying call traces in diagnostic messages, information about the expression that caused the error is now provided (previously, only the line number was highlighted without detailing which part of the line was responsible for the error). Extended trace information can also be obtained via the API and used to map specific bytecode instructions to a particular position in the source code using the codeobject.co_positions() method or the C API function PyCode_Addr2Location(). This change significantly simplifies debugging issues related to nested dictionary objects, multiple function calls, and complex arithmetic expressions. Traceback (most recent call last): File "calculation.py", line 54, in result = (x / y / z) * (a / b / c) ~~~~~~^~~ ZeroDivisionError: division by zero
- Support for exception groups has been added, allowing the program to generate and handle multiple different exceptions simultaneously. New exception types, ExceptionGroup and BaseExceptionGroup, have been introduced for grouping multiple exceptions and invoking them together, while the expression "except*" has been added to extract individual exceptions from the group.
- A new method add_note() has been added to the BaseException class, allowing the attachment of a text note to an exception, for instance, to provide contextual information that was unavailable during the exception's generation.
- A special type Self has been introduced, representing the current class itself. Self can be used to annotate methods that return an instance of their class in a simpler way than using TypeVar. class MyLock: def __enter__(self) -> Self: self.lock() return self
- A special type LiteralString has been added, which can include only string literals compatible with the LiteralString type (i.e., raw strings and strings of the LiteralString type, but not arbitrary and not combined strings of the str type). The LiteralString type can be used to limit the passing of string arguments to functions, where arbitrary interpolation of string parts could lead to vulnerabilities, such as when forming strings for SQL queries or shell commands. def run_query(sql: LiteralString) -> … … def caller( arbitrary_string: str, query_string: LiteralString, table_name: LiteralString, ) -> None: run_query(«SELECT * FROM students») # ok run_query(literal_string) # ok run_query(«SELECT * FROM » + literal_string) # ok run_query(arbitrary_string) # Error run_query( # Error f»SELECT * FROM students WHERE name = {arbitrary_string}» )
- The TypeVarTuple type has been added, allowing for the use of variadic generics, unlike TypeVar which covers not one type but an arbitrary number of types.
- The standard library now includes the tomllib module with functions for parsing TOML format.
- The ability to mark individual elements of typed dictionaries (TypedDict) with Required and NotRequired labels has been provided to define mandatory and optional fields (by default, all declared fields are required unless the total parameter is set to False). class Movie(TypedDict): title: str year: NotRequired[int] m1: Movie = {«title»: «Black Panther», «year»: 2018} # OK m2: Movie = {«title»: «Star Wars»} # OK (year field is optional) m3: Movie = {«year»: 2022} # Error, mandatory field title not filled)
- A TaskGroup class has been added to the asyncio module, implementing an asynchronous context manager that waits for a group of tasks to complete. Adding tasks to the group is done using the create_task() method. async def main(): async with asyncio.TaskGroup() as tg: task1 = tg.create_task(some_coro(…)) task2 = tg.create_task(another_coro(…)) print(«Both tasks have completed now.»)
- A decorator for classes, methods, and functions @dataclass_transform has been added, which when specified makes the static type checker treat the object as if using the @dataclasses.dataclass decorator. In the example below, the CustomerModel class will be checked by types similarly to a class with the @dataclasses.dataclass decorator, i.e., as having an __init__ method that accepts variables id and name. @dataclass_transform() class ModelBase: … class CustomerModel(ModelBase): id: int name: str
- Regular expressions now support atomic grouping ((?>…)) and possessive quantifiers (*+, ++, ?+, {m,n}+).
- The command-line option "-P" and the environment variable PYTHONSAFEPATH have been added to disable the automatic attachment of potentially unsafe file paths to sys.path.
- The py.exe utility for Windows has been significantly improved, now supporting the syntax "-V:<company>/<tag>" in addition to "-<major>.<minor>".
- Many macros in the C API have been transformed into regular or static inline functions.
- The following modules have been deprecated and will be removed in the Python 3.13 release: uu, cgi, pipes, crypt, aifc, chunk, msilib, telnetlib, audioop, nis, sndhdr, imghdr, nntplib, spwd, xdrlib, cgitb, mailcap, ossaudiodev, and sunau. The functions PyUnicode_Encode* have been removed.
Source: opennet.ru
