Teks sumber — English
Terjemahan — Bahasa Indonesia
``Callable`` cannot express complex signatures such as functions that take a variadic number of arguments, :ref:`overloaded functions <overload>`, or functions that have keyword-only parameters. However, these signatures can be expressed by defining a :class:`Protocol` class with a :meth:`~object.__call__` method:
``Callable`` tidak dapat mengekspresikan signatur kompleks seperti fungsi yang menerima sejumlah argumen variadik, :ref:`fungsi yang di-overload <overload>`, atau fungsi yang memiliki parameter khusus kata kunci (keyword-only). Namun, signatur ini dapat diekspresikan dengan mendefinisikan sebuah kelas :class:`Protocol` dengan metode :meth:`~object.__call__`:
The only legal parameters for :class:`type` are classes, :data:`Any`, :ref:`type variables <generics>`, and unions of any of these types. For example::
Satu-satunya parameter yang sah untuk :class:`type` adalah kelas, :data:`Any`, :ref:`variabel tipe <generics>`, dan gabungan (union) dari tipe-tipe tersebut. Sebagai contoh::
The ``SendType`` and ``ReturnType`` parameters default to :const:`!None`::
Parameter ``SendType`` dan ``ReturnType`` secara default bernilai :const:`!None`::
A generic type can have any number of type variables. All varieties of :class:`TypeVar` are permissible as parameters for a generic type::
Sebuah tipe generik dapat memiliki sejumlah variabel tipe. Semua jenis :class:`TypeVar` diizinkan sebagai parameter untuk sebuah tipe generik::
When inheriting from generic classes, some type parameters could be fixed::
Saat mewarisi dari kelas generik, beberapa parameter tipe dapat ditetapkan::
Using a generic class without specifying type parameters assumes :data:`Any` for each position. In the following example, ``MyIterable`` is not generic but implicitly inherits from ``Iterable[Any]``:
Menggunakan sebuah kelas generik tanpa menentukan parameter tipe mengasumsikan :data:`Any` untuk setiap posisi. Dalam contoh berikut, ``MyIterable`` bukan generik tetapi secara implisit mewarisi dari ``Iterable[Any]``:
Note that generics with :class:`ParamSpec` may not have correct ``__parameters__`` after substitution in some cases because they are intended primarily for static type checking.
Perlu diperhatikan bahwa generik dengan :class:`ParamSpec` mungkin tidak memiliki ``__parameters__`` yang benar setelah substitusi dalam beberapa kasus, karena generik tersebut terutama ditujukan untuk pemeriksaan tipe statis.
``Concatenate`` can be used in conjunction with :ref:`Callable <annotating-callables>` and :class:`ParamSpec` to annotate a higher-order callable which adds, removes, or transforms parameters of another callable. Usage is in the form ``Concatenate[Arg1Type, Arg2Type, ..., ParamSpecVariable]``. ``Concatenate`` is valid when used in :ref:`Callable <annotating-callables>` type hints and when instantiating user-defined generic classes with :class:`ParamSpec` parameters. The last parameter to ``Concatenate`` must be a :class:`ParamSpec` or ellipsis (``...``).
``Concatenate`` dapat digunakan bersama :ref:`Callable <annotating-callables>` dan :class:`ParamSpec` untuk menganotasi sebuah callable orde tinggi yang menambahkan, menghapus, atau mentransformasi parameter dari callable lain. Penggunaannya berbentuk ``Concatenate[Arg1Type, Arg2Type, ..., ParamSpecVariable]``. ``Concatenate`` valid bila digunakan dalam hint tipe :ref:`Callable <annotating-callables>` dan saat menginstansiasi kelas generik yang didefinisikan pengguna dengan parameter :class:`ParamSpec`. Parameter terakhir untuk ``Concatenate`` harus berupa sebuah :class:`ParamSpec` atau elipsis (``...``).
``Literal`` now de-duplicates parameters. Equality comparisons of ``Literal`` objects are no longer order dependent. ``Literal`` objects will now raise a :exc:`TypeError` exception during equality comparisons if one of their parameters are not :term:`hashable`.
``Literal`` kini menghapus parameter yang duplikat. Perbandingan kesetaraan objek ``Literal`` tidak lagi bergantung pada urutan. Objek ``Literal`` kini akan memunculkan eksepsi :exc:`TypeError` selama perbandingan kesetaraan jika salah satu parameternya bukan :term:`hashable`.
A generic type is typically declared by adding a list of type parameters after the class name::
Sebuah tipe generik biasanya dideklarasikan dengan menambahkan sebuah daftar parameter tipe setelah nama kelas::
For backwards compatibility, generic classes can also be declared by explicitly inheriting from ``Generic``. In this case, the type parameters must be declared separately::
Untuk kompatibilitas mundur, kelas generik juga dapat dideklarasikan dengan mewarisi secara eksplisit dari ``Generic``. Dalam kasus ini, parameter tipe harus dideklarasikan secara terpisah::
Type variables exist primarily for the benefit of static type checkers. They serve as the parameters for generic types as well as for generic function and type alias definitions. See :class:`Generic` for more information on generic types. Generic functions work as follows::
Variabel tipe ada terutama untuk kepentingan pemeriksa tipe statis. Variabel tipe berfungsi sebagai parameter untuk tipe generik serta untuk definisi fungsi generik dan alias tipe. Lihat :class:`Generic` untuk informasi lebih lanjut tentang tipe generik. Fungsi generik bekerja sebagai berikut::
However, note that at most one type variable tuple may appear in a single list of type arguments or type parameters::
Namun, perlu diperhatikan bahwa paling banyak satu tuple variabel tipe dapat muncul dalam satu daftar argumen tipe atau parameter tipe::
Added support for the ``bound``, ``covariant``, ``contravariant``, and ``infer_variance`` parameters.
Dukungan untuk parameter ``bound``, ``covariant``, ``contravariant``, dan ``infer_variance`` ditambahkan.
Parameter specification variables exist primarily for the benefit of static type checkers. They are used to forward the parameter types of one callable to another callable -- a pattern commonly found in higher order functions and decorators. They are only valid when used in ``Concatenate``, or as the first argument to ``Callable``, or as parameters for user-defined Generics. See :class:`Generic` for more information on generic types.
Variabel spesifikasi parameter ada terutama untuk kepentingan pemeriksa tipe statis. Variabel ini digunakan untuk meneruskan tipe parameter dari satu callable ke callable lain -- sebuah pola yang umum ditemukan dalam fungsi orde tinggi dan dekorator. Variabel ini hanya valid ketika digunakan dalam ``Concatenate``, atau sebagai argumen pertama untuk ``Callable``, atau sebagai parameter untuk Generics yang didefinisikan pengguna. Lihat :class:`Generic` untuk informasi lebih lanjut tentang tipe generik.
For example, to add basic logging to a function, one can create a decorator ``add_logging`` to log function calls. The parameter specification variable tells the type checker that the callable passed into the decorator and the new callable returned by it have inter-dependent type parameters::
Sebagai contoh, untuk menambahkan logging dasar ke sebuah fungsi, seseorang dapat membuat sebuah dekorator ``add_logging`` untuk mencatat pemanggilan fungsi. Variabel spesifikasi parameter memberi tahu pemeriksa tipe bahwa callable yang diteruskan ke dekorator dan callable baru yang dikembalikan olehnya memiliki parameter tipe yang saling bergantung::
Since ``ParamSpec`` captures both positional and keyword parameters, ``P.args`` and ``P.kwargs`` can be used to split a ``ParamSpec`` into its components. ``P.args`` represents the tuple of positional parameters in a given call and should only be used to annotate ``*args``. ``P.kwargs`` represents the mapping of keyword parameters to their values in a given call, and should be only be used to annotate ``**kwargs``. Both attributes require the annotated parameter to be in scope. At runtime, ``P.args`` and ``P.kwargs`` are instances respectively of :class:`ParamSpecArgs` and :class:`ParamSpecKwargs`.
Karena ``ParamSpec`` menangkap parameter posisional maupun keyword, ``P.args`` dan ``P.kwargs`` dapat digunakan untuk memecah sebuah ``ParamSpec`` menjadi komponen-komponennya. ``P.args`` merepresentasikan tuple parameter posisional dalam sebuah pemanggilan dan hanya boleh digunakan untuk menganotasi ``*args``. ``P.kwargs`` merepresentasikan pemetaan parameter keyword ke nilai-nilainya dalam sebuah pemanggilan, dan hanya boleh digunakan untuk menganotasi ``**kwargs``. Kedua atribut mengharuskan parameter yang dianotasi berada dalam cakupan. Pada runtime, ``P.args`` dan ``P.kwargs`` masing-masing adalah instance dari :class:`ParamSpecArgs` dan :class:`ParamSpecKwargs`.
The type parameters of the type alias, or an empty tuple if the alias is not generic:
Parameter tipe dari alias tipe, atau tuple kosong jika alias tersebut tidak generik:
Parameters
Parameter
Type checkers recognize the following optional parameters on field specifiers:
Pemeriksa tipe mengenali parameter opsional berikut pada penentu field:
**Recognised parameters for field specifiers**
**Parameter yang Dikenali untuk penentu field**
See the documentation for :meth:`annotationlib.ForwardRef.evaluate` for the meaning of the *owner*, *globals*, *locals*, *type_params*, and *format* parameters.
Lihat dokumentasi untuk :meth:`annotationlib.ForwardRef.evaluate` mengenai arti parameter *owner*, *globals*, *locals*, *type_params*, dan *format*.