Skip to content

Adds introspection for class associated constants #5157

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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/5157.added.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Add support for class associated consts introspection.
20 changes: 20 additions & 0 deletions pyo3-macros-backend/src/pyimpl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ use std::collections::HashSet;
use crate::introspection::function_introspection_code;
#[cfg(feature = "experimental-inspect")]
use crate::method::{FnSpec, FnType};
#[cfg(feature = "experimental-inspect")]
use crate::pyfunction::FunctionSignature;
use crate::utils::{has_attribute, has_attribute_with_namespace, Ctx, PyO3CratePath};
use crate::{
attributes::{take_pyo3_options, CrateAttribute},
Expand Down Expand Up @@ -166,6 +168,8 @@ pub fn impl_methods(
attributes,
};
let attrs = get_cfg_attributes(&konst.attrs);
#[cfg(feature = "experimental-inspect")]
extra_fragments.push(class_const_introspection_code(&spec, ty, ctx));
let MethodAndMethodDef {
associated_method,
method_def,
Expand Down Expand Up @@ -391,3 +395,19 @@ fn method_introspection_code(spec: &FnSpec<'_>, parent: &syn::Type, ctx: &Ctx) -
Some(parent),
)
}

#[cfg(feature = "experimental-inspect")]
fn class_const_introspection_code(spec: &ConstSpec, parent: &syn::Type, ctx: &Ctx) -> TokenStream {
let Ctx { pyo3_path, .. } = ctx;

let name = spec.python_name().to_string();
function_introspection_code(
pyo3_path,
None,
&name,
&FunctionSignature::from_arguments(vec![]),
Some("cls"),
vec!["classmethod".into(), "property".into()], // TODO: this combination only works with Python 3.9-3.11 https://docs.python.org/3.11/library/functions.html#classmethod
Some(parent),
)
}
3 changes: 3 additions & 0 deletions pytests/src/pyclasses.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ struct EmptyClass {}

#[pymethods]
impl EmptyClass {
#[classattr]
const CLS_ATTRIBUTE: u32 = 42;

#[new]
fn new() -> Self {
EmptyClass {}
Expand Down
3 changes: 3 additions & 0 deletions pytests/stubs/pyclasses.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ class ClassWithDecorators:
class ClassWithoutConstructor: ...

class EmptyClass:
@classmethod
@property
def CLS_ATTRIBUTE(cls, /): ...
Copy link
Contributor

Choose a reason for hiding this comment

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

I think CLS_ATTRIBUTE: Final = 42 will work because EmptyClass().CLS_ATTRIBUTE works too.

We should maybe reuse the same code as for module constants.

def __len__(self, /): ...
def __new__(cls, /): ...
def method(self, /): ...
Expand Down
Loading