PyTypeCheck

Trait PyTypeCheck 

Source
pub unsafe trait PyTypeCheck {
    const NAME: &'static str;
    const PYTHON_TYPE: &'static str;

    // Required methods
    fn type_check(object: &Bound<'_, PyAny>) -> bool;
    fn classinfo_object(py: Python<'_>) -> Bound<'_, PyAny>;
}
Expand description

Implemented by types which can be used as a concrete Python type inside Py<T> smart pointers.

§Safety

This trait is used to determine whether Bound::cast and similar functions can safely cast to a concrete type. The implementor is responsible for ensuring that type_check only returns true for objects which can safely be treated as Python instances of Self.

Required Associated Constants§

Source

const NAME: &'static str

👎Deprecated since 0.27.0: Use ::classinfo_object() instead and format the type name at runtime. Note that using built-in cast features is often better than manual PyTypeCheck usage.

Name of self. This is used in error messages, for example.

Source

const PYTHON_TYPE: &'static str

Available on crate feature experimental-inspect only.

Provides the full python type of the allowed values.

Required Methods§

Source

fn type_check(object: &Bound<'_, PyAny>) -> bool

Checks if object is an instance of Self, which may include a subtype.

This should be equivalent to the Python expression isinstance(object, Self).

Source

fn classinfo_object(py: Python<'_>) -> Bound<'_, PyAny>

Returns the expected type as a possible argument for the isinstance and issubclass function.

It may be a single type or a tuple of types.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§

Source§

impl PyTypeCheck for PyWeakref

Source§

const NAME: &'static str = "weakref"

Source§

const PYTHON_TYPE: &'static str = "weakref.ProxyType | weakref.CallableProxyType | weakref.ReferenceType"

Source§

impl PyTypeCheck for PyWeakrefProxy

Source§

const NAME: &'static str = "weakref.ProxyTypes"

Source§

const PYTHON_TYPE: &'static str = "weakref.ProxyType | weakref.CallableProxyType"

Source§

impl<T> PyTypeCheck for T
where T: PyTypeInfo,

Source§

const NAME: &'static str = T::NAME

Source§

const PYTHON_TYPE: &'static str = <T as PyTypeInfo>::PYTHON_TYPE