j7s_diagnostics_py/vendor/pyo3/guide/pyclass-parameters.md

5.3 KiB

#[pyclass] can be used with the following parameters:

Parameter Description
constructor This is currently only allowed on variants of complex enums. It allows customization of the generated class constructor for each variant. It uses the same syntax and supports the same options as the signature attribute of functions and methods.
crate = "some::path" Path to import the pyo3 crate, if it's not accessible at ::pyo3.
dict Gives instances of this class an empty __dict__ to store custom attributes.
eq Implements __eq__ using the PartialEq implementation of the underlying Rust datatype.
eq_int Implements __eq__ using __int__ for simple enums.
extends = BaseType Use a custom baseclass. Defaults to PyAny
freelist = N Implements a free list of size N. This can improve performance for types that are often created and deleted in quick succession. Profile your code to see whether freelist is right for you.
from_py_object Implement FromPyObject for this pyclass. Requires the pyclass to be Clone.
frozen Declares that your pyclass is immutable. It removes the borrow checker overhead when retrieving a shared reference to the Rust struct, but disables the ability to get a mutable reference.
generic Implements runtime parametrization for the class following PEP 560.
get_all Generates getters for all fields of the pyclass.
hash Implements __hash__ using the Hash implementation of the underlying Rust datatype. Requires eq and frozen
immutable_type Makes the type object immutable. Supported on 3.14+ with the abi3 feature active, or 3.10+ otherwise.
mapping Inform PyO3 that this class is a Mapping, and so leave its implementation of sequence C-API slots empty.
module = "module_name" Python code will see the class as being defined in this module. Defaults to builtins.
name = "python_name" Sets the name that Python sees this class as. Defaults to the name of the Rust struct.
ord Implements __lt__, __gt__, __le__, & __ge__ using the PartialOrd implementation of the underlying Rust datatype. Requires eq
rename_all = "renaming_rule" Applies renaming rules to every getters and setters of a struct, or every variants of an enum. Possible values are: "camelCase", "kebab-case", "lowercase", "PascalCase", "SCREAMING-KEBAB-CASE", "SCREAMING_SNAKE_CASE", "snake_case", "UPPERCASE".
sequence Inform PyO3 that this class is a Sequence, and so leave its C-API mapping length slot empty.
set_all Generates setters for all fields of the pyclass.
new = "from_fields" Generates a default __new__ constructor with all fields as parameters in the new() method.
skip_from_py_object Prevents this PyClass from participating in the FromPyObject: PyClass + Clone blanket implementation. This allows a custom FromPyObject impl, even if self is Clone.
str Implements __str__ using the Display implementation of the underlying Rust datatype or by passing an optional format string str="<format string>". Note: The optional format string is only allowed for structs. name and rename_all are incompatible with the optional format string. Additional details can be found in the discussion on this PR.
subclass Allows other Python classes and #[pyclass] to inherit from this class. Enums cannot be subclassed.
unsendable Required if your struct is not Send. Rather than using unsendable, consider implementing your struct in a thread-safe way by e.g. substituting Rc with Arc. By using unsendable, your class will panic when accessed by another thread. Also note the Python's GC is multi-threaded and while unsendable classes will not be traversed on foreign threads to avoid UB, this can lead to memory leaks.
weakref Allows this class to be weakly referenceable.

All of these parameters can either be passed directly on the #[pyclass(...)] annotation, or as one or more accompanying #[pyo3(...)] annotations, e.g.:

// Argument supplied directly to the `#[pyclass]` annotation.
#[pyclass(name = "SomeName", subclass)]
struct MyClass {}

// Argument supplied as a separate annotation.
#[pyclass]
#[pyo3(name = "SomeName", subclass)]
struct MyClass {}