Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions newsfragments/5641.changed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Introspection: add TYPE_HINT to PyTypeInfo manual implementations
6 changes: 5 additions & 1 deletion src/types/ellipsis.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#[cfg(feature = "experimental-inspect")]
use crate::inspect::TypeHint;
use crate::{
ffi, ffi_ptr_ext::FfiPtrExt, types::any::PyAnyMethods, Borrowed, Bound, PyAny, PyTypeInfo,
Python,
Expand Down Expand Up @@ -27,9 +29,11 @@ impl PyEllipsis {

unsafe impl PyTypeInfo for PyEllipsis {
const NAME: &'static str = "ellipsis";

const MODULE: Option<&'static str> = None;

#[cfg(feature = "experimental-inspect")]
const TYPE_HINT: TypeHint = TypeHint::module_attr("types", "EllipsisType");
Comment on lines +34 to +35
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had to double-check whether this and NotImplemented should be special-cased like None.

My conclusion (from the typing spec, and mypy playground) is that they should not be special cased and the code here is correct 👍


fn type_object_raw(_py: Python<'_>) -> *mut ffi::PyTypeObject {
unsafe { ffi::Py_TYPE(ffi::Py_Ellipsis()) }
}
Expand Down
5 changes: 5 additions & 0 deletions src/types/mapping.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use crate::conversion::IntoPyObject;
use crate::err::PyResult;
use crate::ffi_ptr_ext::FfiPtrExt;
#[cfg(feature = "experimental-inspect")]
use crate::inspect::TypeHint;
use crate::instance::Bound;
use crate::py_result_ext::PyResultExt;
use crate::sync::PyOnceLock;
Expand All @@ -25,6 +27,9 @@ unsafe impl PyTypeInfo for PyMapping {
const NAME: &'static str = "Mapping";
const MODULE: Option<&'static str> = Some("collections.abc");

#[cfg(feature = "experimental-inspect")]
const TYPE_HINT: TypeHint = TypeHint::module_attr("collections.abc", "Mapping");

#[inline]
#[allow(clippy::redundant_closure_call)]
fn type_object_raw(py: Python<'_>) -> *mut ffi::PyTypeObject {
Expand Down
6 changes: 5 additions & 1 deletion src/types/none.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use crate::ffi_ptr_ext::FfiPtrExt;
#[cfg(feature = "experimental-inspect")]
use crate::inspect::TypeHint;
use crate::{ffi, types::any::PyAnyMethods, Borrowed, Bound, PyAny, PyTypeInfo, Python};

/// Represents the Python `None` object.
Expand All @@ -25,9 +27,11 @@ impl PyNone {

unsafe impl PyTypeInfo for PyNone {
const NAME: &'static str = "NoneType";

const MODULE: Option<&'static str> = None;

#[cfg(feature = "experimental-inspect")]
const TYPE_HINT: TypeHint = TypeHint::builtin("None");

fn type_object_raw(_py: Python<'_>) -> *mut ffi::PyTypeObject {
unsafe { ffi::Py_TYPE(ffi::Py_None()) }
}
Expand Down
5 changes: 5 additions & 0 deletions src/types/notimplemented.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#[cfg(feature = "experimental-inspect")]
use crate::inspect::TypeHint;
use crate::{
ffi, ffi_ptr_ext::FfiPtrExt, types::any::PyAnyMethods, Borrowed, Bound, PyAny, PyTypeInfo,
Python,
Expand Down Expand Up @@ -29,6 +31,9 @@ unsafe impl PyTypeInfo for PyNotImplemented {
const NAME: &'static str = "NotImplementedType";
const MODULE: Option<&'static str> = None;

#[cfg(feature = "experimental-inspect")]
const TYPE_HINT: TypeHint = TypeHint::module_attr("types", "NotImplementedType");

fn type_object_raw(_py: Python<'_>) -> *mut ffi::PyTypeObject {
unsafe { ffi::Py_TYPE(ffi::Py_NotImplemented()) }
}
Expand Down
5 changes: 5 additions & 0 deletions src/types/sequence.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use crate::err::{self, PyErr, PyResult};
use crate::ffi_ptr_ext::FfiPtrExt;
#[cfg(feature = "experimental-inspect")]
use crate::inspect::TypeHint;
use crate::instance::Bound;
use crate::internal_tricks::get_ssize_index;
use crate::py_result_ext::PyResultExt;
Expand All @@ -24,6 +26,9 @@ unsafe impl PyTypeInfo for PySequence {
const NAME: &'static str = "Sequence";
const MODULE: Option<&'static str> = Some("collections.abc");

#[cfg(feature = "experimental-inspect")]
const TYPE_HINT: TypeHint = TypeHint::module_attr("collections.abc", "Sequence");

#[inline]
fn type_object_raw(py: Python<'_>) -> *mut ffi::PyTypeObject {
static TYPE: PyOnceLock<Py<PyType>> = PyOnceLock::new();
Expand Down
Loading