Teks sumber — English
Terjemahan — Bahasa Indonesia
The language changes focus on usability, as :term:`f-strings <f-string>` have had many limitations removed and 'Did you mean ...' suggestions continue to improve. The new :ref:`type parameter syntax <whatsnew312-pep695>` and :keyword:`type` statement improve ergonomics for using :term:`generic types <generic type>` and :term:`type aliases <type alias>` with static type checkers.
The value of type aliases and the bound and constraints of type variables created through this syntax are evaluated only on demand (see :ref:`lazy evaluation <lazy-evaluation>`). This means type aliases are able to refer to other types defined later in the file.
:pep:`688` introduces a way to use the :ref:`buffer protocol <bufferobjects>` from Python code. Classes that implement the :meth:`~object.__buffer__` method are now usable as buffer types.
Improve the :exc:`SyntaxError` error message when the user types ``import x from y`` instead of ``from y import x``. (Contributed by Pablo Galindo in :gh:`98931`.)
:class:`types.MappingProxyType` instances are now hashable if the underlying mapping is hashable. (Contributed by Serhiy Storchaka in :gh:`87995`.)
types
types
Add :func:`types.get_original_bases` to allow for further introspection of :ref:`user-defined-generics` when subclassed. (Contributed by James Hilton-Balfe and Alex Waygood in :gh:`101827`.)
Use ``isinstance(obj, collections.abc.Buffer)`` to test if ``obj`` implements the :ref:`buffer protocol <bufferobjects>` at runtime. For use in type annotations, either use :class:`~collections.abc.Buffer` or a union that explicitly specifies the types your code supports (e.g., ``bytes | bytearray | memoryview``).
:class:`!ByteString` was originally intended to be an abstract class that would serve as a supertype of both :class:`bytes` and :class:`bytearray`. However, since the ABC never had any methods, knowing that an object was an instance of :class:`!ByteString` never actually told you anything useful about the object. Other common buffer types such as :class:`memoryview` were also never understood as subtypes of :class:`!ByteString` (either at runtime or by static type checkers).
:mod:`ctypes`:
:mod:`ctypes`:
The undocumented :func:`!ctypes.SetPointerType` function has been deprecated since Python 3.13.
:mod:`types`:
:class:`types.CodeType`: Accessing :attr:`!codeobject.co_lnotab` was deprecated in :pep:`626` since 3.10 and was planned to be removed in 3.12, but it only got a proper :exc:`DeprecationWarning` in 3.12. May be removed in 3.15. (Contributed by Nikita Sobolev in :gh:`101866`.)
Bitwise inversion on boolean types, ``~True`` or ``~False`` has been deprecated since Python 3.12, as it produces surprising and unintuitive results (``-2`` and ``-1``). Use ``not x`` instead for the logical negation of a Boolean. In the rare case that you need the bitwise inversion of the underlying integer, convert to ``int`` explicitly (``~int(x)``).
:mod:`mimetypes`:
:mod:`mimetypes`:
Valid extensions start with a '.' or are empty for :meth:`mimetypes.MimeTypes.add_type`. Undotted extensions are deprecated and will raise a :exc:`ValueError` in Python 3.16. (Contributed by Hugo van Kemenade in :gh:`75223`.)
Implicitly switching to the MSVC-compatible struct layout by setting :attr:`~ctypes.Structure._pack_` but not :attr:`~ctypes.Structure._layout_` on non-Windows platforms.
:mod:`ctypes`
:mod:`!ctypes.macholib`
``types.ModuleType(name)``
Remove ``_use_broken_old_ctypes_structure_semantics_`` flag from :mod:`ctypes` module. (Contributed by Nikita Sobolev in :gh:`99285`.)
The :mod:`os` module no longer accepts bytes-like paths, like :class:`bytearray` and :class:`memoryview` types: only the exact :class:`bytes` type is accepted for bytes strings. (Contributed by Victor Stinner in :gh:`98393`.)
:pep:`697`: Add an API for extending types whose instance memory layout is opaque:
:c:macro:`Py_TPFLAGS_ITEMS_AT_END` and :c:func:`PyObject_GetItemData` added to allow safely extending certain variable-sized types, including :c:var:`PyType_Type`.
The :c:macro:`Py_TPFLAGS_HAVE_VECTORCALL` flag is now removed from a class when the class's :py:meth:`~object.__call__` method is reassigned. This makes vectorcall safe to use with mutable types (i.e. heap types without the immutable flag, :c:macro:`Py_TPFLAGS_IMMUTABLETYPE`). Mutable types that do not override :c:member:`~PyTypeObject.tp_call` now inherit the ``Py_TPFLAGS_HAVE_VECTORCALL`` flag. (Contributed by Petr Viktorin in :gh:`93274`.)
``tp_weaklist`` for all static builtin types is always ``NULL``. This is an internal-only field on ``PyTypeObject`` but we're pointing out the change in case someone happens to be accessing the field directly anyway. To avoid breakage, consider using the existing public C-API instead, or, if necessary, the (internal-only) ``_PyObject_GET_WEAKREFS_LISTPTR()`` macro.
The :c:func:`PyUnicode_FSDecoder` function no longer accepts bytes-like paths, like :class:`bytearray` and :class:`memoryview` types: only the exact :class:`bytes` type is accepted for bytes strings. (Contributed by Victor Stinner in :gh:`98393`.)
Avoid ``PyType_From*`` functions: if you don't need C-specific features (slots or setting the instance size), create types by :ref:`calling <call>` the metaclass.
Creating :c:data:`immutable types <Py_TPFLAGS_IMMUTABLETYPE>` with mutable bases is deprecated and will be disabled in Python 3.14. (:gh:`95388`)
Creating :c:data:`immutable types <Py_TPFLAGS_IMMUTABLETYPE>` with mutable bases (:gh:`95388`).