Pelokalan
Python ID
id en
Masuk
Proyek
whatsnew__3-10.po Berkas proyek · python-docs-id-3.14 · Bahasa Indonesia
Modul PO Hanya lihat
Teks sumber — English
Terjemahan — Bahasa Indonesia
:pep:`604`, Allow writing union types as X | Y
Multiple Exception types without parentheses:
>>> try: ... build_dyson_sphere() ... except NotEnoughScienceError, NotEnoughResourcesError: File "<stdin>", line 3 except NotEnoughScienceError, NotEnoughResourcesError: ^ SyntaxError: multiple exception types must be parenthesized
Structural pattern matching has been added in the form of a *match statement* and *case statements* of patterns with associated actions. Patterns consist of sequences, mappings, primitive data types as well as class instances. Pattern matching enables programs to extract information from complex data types, branch on the structure of data, and apply specific actions based on different forms of data.
In previous versions of Python, to apply a type hint for functions accepting arguments of multiple types, :class:`typing.Union` was used::
See :ref:`types-union` and :pep:`604` for more details.
The first is the parameter specification variable. They are used to forward the parameter types of one callable to another callable -- a pattern commonly found in higher order functions and decorators. Examples of usage can be found in :class:`typing.ParamSpec`. Previously, there was no easy way to type annotate dependency of parameter types in such a precise manner.
:pep:`484` introduced the concept of type aliases, only requiring them to be top-level unannotated assignments. This simplicity sometimes made it difficult for type checkers to distinguish between type aliases and ordinary assignments, especially when forward references or invalid types were involved. Compare::
The views returned by :meth:`dict.keys`, :meth:`dict.values` and :meth:`dict.items` now all have a ``mapping`` attribute that gives a :class:`types.MappingProxyType` object wrapping the original dictionary. (Contributed by Dennis Sweeney in :issue:`40890`.)
The ``__args__`` of the :ref:`parameterized generic <types-genericalias>` for :class:`collections.abc.Callable` are now consistent with :data:`typing.Callable`. :class:`collections.abc.Callable` generic now flattens type parameters, similar to what :data:`typing.Callable` currently does. This means that ``collections.abc.Callable[[int, str], str]`` will have ``__args__`` of ``(int, str, str)``; previously this was ``([int, str], str)``. To allow this change, :class:`types.GenericAlias` can now be subclassed, and a subclass will be returned when subscripting the :class:`collections.abc.Callable` type. Note that a :exc:`TypeError` may be raised for invalid forms of parameterizing :class:`collections.abc.Callable` which may have passed silently in Python 3.9. (Contributed by Ken Jin in :issue:`42195`.)
Add :func:`inspect.get_annotations`, which safely computes the annotations defined on an object. It works around the quirks of accessing the annotations on various types of objects, and makes very few assumptions about the object it examines. :func:`inspect.get_annotations` can also correctly un-stringize stringized annotations. :func:`inspect.get_annotations` is now considered best practice for accessing the annotations dict defined on any Python object; for more information on best practices for working with annotations, please see :ref:`annotations-howto`. Relatedly, :func:`inspect.signature`, :func:`inspect.Signature.from_callable`, and :func:`!inspect.Signature.from_function` now call :func:`inspect.get_annotations` to retrieve annotations. This means :func:`inspect.signature` and :func:`inspect.Signature.from_callable` can also now un-stringize stringized annotations. (Contributed by Larry Hastings in :issue:`43817`.)
The ssl module uses heap-types and multi-phase initialization. (Contributed by Christian Heimes in :issue:`42333`.)
types
types
Reintroduce the :class:`types.EllipsisType`, :class:`types.NoneType` and :class:`types.NotImplementedType` classes, providing a new set of types readily interpretable by type checkers. (Contributed by Bas van Beek in :issue:`41810`.)
``Literal`` comparisons now respect types. For example, ``Literal[0] == Literal[False]`` previously evaluated to ``True``. It is now ``False``. To support this change, the internally used type cache now supports differentiating types.
The :class:`types.FunctionType` constructor now inherits the current builtins if the *globals* dictionary has no ``"__builtins__"`` key, rather than using ``{"None": None}`` as builtins: same behavior as :func:`eval` and :func:`exec` functions. Defining a function with ``def function(...): ...`` in Python is not affected, globals cannot be overridden with this syntax: it also inherits the current builtins. (Contributed by Victor Stinner in :issue:`42990`.)
The :c:func:`PyType_GetSlot` function can accept :ref:`static types <static-types>`. (Contributed by Hai Shi and Petr Viktorin in :issue:`41073`.)
The deprecated :mod:`!mailcap` module now refuses to inject unsafe text (filenames, MIME types, parameters) into shell commands. Instead of using such text, it will warn and act as if a match was not found (or for test commands, as if the test failed). (Contributed by Petr Viktorin in :gh:`98966`.)