Skip to content

Commit 6a9135f

Browse files
committed
Add back PyTypeCheck::NAME
1 parent dd0d5c5 commit 6a9135f

File tree

5 files changed

+15
-2
lines changed

5 files changed

+15
-2
lines changed

newsfragments/5387.changed.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
Fetch type name dynamically on cast errors instead of using `PyTypeCheck::NAME`
1+
- Fetch type name dynamically on cast errors instead of using `PyTypeCheck::NAME`
2+
- Deprecate `PyTypeCheck::NAME`, please `TypeTypeCheck::classinfo_object` to get the expected type and format it at runtime.

newsfragments/5387.removed.md

Lines changed: 0 additions & 1 deletion
This file was deleted.

src/type_object.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,13 @@ pub unsafe trait PyTypeInfo: Sized {
8686

8787
/// Implemented by types which can be used as a concrete Python type inside `Py<T>` smart pointers.
8888
pub trait PyTypeCheck {
89+
/// Name of self. This is used in error messages, for example.
90+
#[deprecated(
91+
since = "0.27.0",
92+
note = "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."
93+
)]
94+
const NAME: &'static str;
95+
8996
/// Provides the full python type of the allowed values.
9097
#[cfg(feature = "experimental-inspect")]
9198
const PYTHON_TYPE: &'static str;
@@ -105,6 +112,8 @@ impl<T> PyTypeCheck for T
105112
where
106113
T: PyTypeInfo,
107114
{
115+
const NAME: &'static str = T::NAME;
116+
108117
#[cfg(feature = "experimental-inspect")]
109118
const PYTHON_TYPE: &'static str = <T as PyTypeInfo>::PYTHON_TYPE;
110119

src/types/weakref/anyref.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ pyobject_native_type_named!(PyWeakref);
1919
// pyobject_native_type_sized!(PyWeakref, ffi::PyWeakReference);
2020

2121
impl PyTypeCheck for PyWeakref {
22+
const NAME: &'static str = "weakref";
23+
2224
#[cfg(feature = "experimental-inspect")]
2325
const PYTHON_TYPE: &'static str =
2426
"weakref.ProxyType | weakref.CallableProxyType | weakref.ReferenceType";

src/types/weakref/proxy.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ pyobject_native_type_named!(PyWeakrefProxy);
2121
// pyobject_native_type_sized!(PyWeakrefProxy, ffi::PyWeakReference);
2222

2323
impl PyTypeCheck for PyWeakrefProxy {
24+
const NAME: &'static str = "weakref.ProxyTypes";
25+
2426
#[cfg(feature = "experimental-inspect")]
2527
const PYTHON_TYPE: &'static str = "weakref.ProxyType | weakref.CallableProxyType";
2628

0 commit comments

Comments
 (0)