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
2 changes: 2 additions & 0 deletions newsfragments/5710.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- Correct `PyTypeInfo::TYPE_HINT` on user exceptions that was swapping module and name
- Stub generation: tag modules containing exception as incomplete (we do not generate stubs for exceptions yet)
25 changes: 16 additions & 9 deletions pyo3-introspection/src/introspection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,18 +72,25 @@ fn convert_module(
id: &str,
name: &str,
members: &[String],
incomplete: bool,
mut incomplete: bool,
chunks_by_id: &HashMap<&str, &Chunk>,
chunks_by_parent: &HashMap<&str, Vec<&Chunk>>,
) -> Result<Module> {
let (modules, classes, functions, attributes) = convert_members(
members
.iter()
.filter_map(|id| chunks_by_id.get(id.as_str()).copied())
.chain(chunks_by_parent.get(&id).into_iter().flatten().copied()),
chunks_by_id,
chunks_by_parent,
)?;
let mut member_chunks = chunks_by_parent
.get(&id)
.into_iter()
.flatten()
.copied()
.collect::<Vec<_>>();
for member in members {
if let Some(c) = chunks_by_id.get(member.as_str()) {
member_chunks.push(*c);
} else {
incomplete = true; // We don't find an element
}
}
let (modules, classes, functions, attributes) =
convert_members(member_chunks, chunks_by_id, chunks_by_parent)?;

Ok(Module {
name: name.into(),
Expand Down
38 changes: 38 additions & 0 deletions pytests/src/exception.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
use pyo3::create_exception;
use pyo3::exceptions::PyValueError;
use pyo3::prelude::*;

create_exception!(pytests.exception, MyValueError, PyValueError);

#[pymodule(gil_used = false)]
pub mod exception {
use pyo3::exceptions::PyValueError;
use pyo3::prelude::*;

#[pymodule_export]
use super::MyValueError;

#[pyfunction]
fn raise_my_value_error() -> PyResult<()> {
Err(MyValueError::new_err("error"))
}

#[pyfunction]
fn return_value_error<'py>(py: Python<'py>) -> PyResult<Bound<'py, PyValueError>> {
Ok(PyValueError::new_err("error")
.into_pyobject(py)?
.cast_into()?)
}

#[pyfunction]
fn return_my_value_error<'py>(py: Python<'py>) -> PyResult<Bound<'py, MyValueError>> {
Ok(MyValueError::new_err("error")
.into_pyobject(py)?
.cast_into()?)
}

#[pyfunction]
fn return_pyerr() -> PyErr {
MyValueError::new_err("error")
}
}
5 changes: 3 additions & 2 deletions pytests/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ mod consts;
pub mod datetime;
pub mod dict_iter;
pub mod enums;
mod exception;
pub mod misc;
pub mod objstore;
pub mod othermod;
Expand All @@ -27,8 +28,8 @@ mod pyo3_pytests {
use buf_and_str::buf_and_str;
#[pymodule_export]
use {
comparisons::comparisons, consts::consts, enums::enums, pyclasses::pyclasses,
pyfunctions::pyfunctions, subclassing::subclassing,
comparisons::comparisons, consts::consts, enums::enums, exception::exception,
pyclasses::pyclasses, pyfunctions::pyfunctions, subclassing::subclassing,
};

// Inserting to sys.modules allows importing submodules nicely from Python
Expand Down
8 changes: 8 additions & 0 deletions pytests/stubs/exception.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from _typeshed import Incomplete
from pytests.exception import MyValueError

def raise_my_value_error() -> None: ...
def return_my_value_error() -> MyValueError: ...
def return_pyerr() -> BaseException: ...
def return_value_error() -> ValueError: ...
def __getattr__(name: str) -> Incomplete: ...
2 changes: 1 addition & 1 deletion src/exceptions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,8 @@ macro_rules! create_exception_type_object {
$crate::pyobject_native_type_core!(
$name,
$name::type_object_raw,
stringify!($name),
stringify!($module),
stringify!($name),
#module=::std::option::Option::Some(stringify!($module))
);

Expand Down
Loading