Skip to main content

PyTypeCheck

Trait PyTypeCheck 

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

    // Required methods
    fn type_check(object: &Bound<'_, PyAny>) -> bool;
    fn classinfo_object(py: Python<'_>) -> Bound<'_, PyAny>;
}
Available on crate feature tls only.
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§

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.

Required Methods§

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).

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§

§

impl PyTypeCheck for PyWeakref

§

const NAME: &'static str = "weakref"

§

impl PyTypeCheck for PyWeakrefProxy

§

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

§

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

§

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