Skip to content

Commit d8018d7

Browse files
committed
Introspection: add TYPE_HINT to PyTypeInfo manual implementations
1 parent e4f90c6 commit d8018d7

File tree

6 files changed

+26
-2
lines changed

6 files changed

+26
-2
lines changed

newsfragments/5641.changed.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Introspection: add TYPE_HINT to PyTypeInfo manual implementations

src/types/ellipsis.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#[cfg(feature = "experimental-inspect")]
2+
use crate::inspect::TypeHint;
13
use crate::{
24
ffi, ffi_ptr_ext::FfiPtrExt, types::any::PyAnyMethods, Borrowed, Bound, PyAny, PyTypeInfo,
35
Python,
@@ -27,9 +29,11 @@ impl PyEllipsis {
2729

2830
unsafe impl PyTypeInfo for PyEllipsis {
2931
const NAME: &'static str = "ellipsis";
30-
3132
const MODULE: Option<&'static str> = None;
3233

34+
#[cfg(feature = "experimental-inspect")]
35+
const TYPE_HINT: TypeHint = TypeHint::module_attr("types", "EllipsisType");
36+
3337
fn type_object_raw(_py: Python<'_>) -> *mut ffi::PyTypeObject {
3438
unsafe { ffi::Py_TYPE(ffi::Py_Ellipsis()) }
3539
}

src/types/mapping.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
use crate::conversion::IntoPyObject;
22
use crate::err::PyResult;
33
use crate::ffi_ptr_ext::FfiPtrExt;
4+
#[cfg(feature = "experimental-inspect")]
5+
use crate::inspect::TypeHint;
46
use crate::instance::Bound;
57
use crate::py_result_ext::PyResultExt;
68
use crate::sync::PyOnceLock;
@@ -25,6 +27,9 @@ unsafe impl PyTypeInfo for PyMapping {
2527
const NAME: &'static str = "Mapping";
2628
const MODULE: Option<&'static str> = Some("collections.abc");
2729

30+
#[cfg(feature = "experimental-inspect")]
31+
const TYPE_HINT: TypeHint = TypeHint::module_attr("collections.abc", "Mapping");
32+
2833
#[inline]
2934
#[allow(clippy::redundant_closure_call)]
3035
fn type_object_raw(py: Python<'_>) -> *mut ffi::PyTypeObject {

src/types/none.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
use crate::ffi_ptr_ext::FfiPtrExt;
2+
#[cfg(feature = "experimental-inspect")]
3+
use crate::inspect::TypeHint;
24
use crate::{ffi, types::any::PyAnyMethods, Borrowed, Bound, PyAny, PyTypeInfo, Python};
35

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

2628
unsafe impl PyTypeInfo for PyNone {
2729
const NAME: &'static str = "NoneType";
28-
2930
const MODULE: Option<&'static str> = None;
3031

32+
#[cfg(feature = "experimental-inspect")]
33+
const TYPE_HINT: TypeHint = TypeHint::builtin("None");
34+
3135
fn type_object_raw(_py: Python<'_>) -> *mut ffi::PyTypeObject {
3236
unsafe { ffi::Py_TYPE(ffi::Py_None()) }
3337
}

src/types/notimplemented.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#[cfg(feature = "experimental-inspect")]
2+
use crate::inspect::TypeHint;
13
use crate::{
24
ffi, ffi_ptr_ext::FfiPtrExt, types::any::PyAnyMethods, Borrowed, Bound, PyAny, PyTypeInfo,
35
Python,
@@ -29,6 +31,9 @@ unsafe impl PyTypeInfo for PyNotImplemented {
2931
const NAME: &'static str = "NotImplementedType";
3032
const MODULE: Option<&'static str> = None;
3133

34+
#[cfg(feature = "experimental-inspect")]
35+
const TYPE_HINT: TypeHint = TypeHint::module_attr("types", "NotImplementedType");
36+
3237
fn type_object_raw(_py: Python<'_>) -> *mut ffi::PyTypeObject {
3338
unsafe { ffi::Py_TYPE(ffi::Py_NotImplemented()) }
3439
}

src/types/sequence.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
use crate::err::{self, PyErr, PyResult};
22
use crate::ffi_ptr_ext::FfiPtrExt;
3+
#[cfg(feature = "experimental-inspect")]
4+
use crate::inspect::TypeHint;
35
use crate::instance::Bound;
46
use crate::internal_tricks::get_ssize_index;
57
use crate::py_result_ext::PyResultExt;
@@ -24,6 +26,9 @@ unsafe impl PyTypeInfo for PySequence {
2426
const NAME: &'static str = "Sequence";
2527
const MODULE: Option<&'static str> = Some("collections.abc");
2628

29+
#[cfg(feature = "experimental-inspect")]
30+
const TYPE_HINT: TypeHint = TypeHint::module_attr("collections.abc", "Sequence");
31+
2732
#[inline]
2833
fn type_object_raw(py: Python<'_>) -> *mut ffi::PyTypeObject {
2934
static TYPE: PyOnceLock<Py<PyType>> = PyOnceLock::new();

0 commit comments

Comments
 (0)