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/5674.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Introspection: fix the return type annotation of `PyResult<()>` (must be `None` and not `tuple`)
8 changes: 1 addition & 7 deletions pyo3-macros-backend/src/introspection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,13 +112,7 @@ pub fn function_introspection_code(
} else {
match returns {
ReturnType::Default => PythonTypeHint::builtin("None"),
ReturnType::Type(_, ty) => match *ty {
Type::Tuple(t) if t.elems.is_empty() => {
// () is converted to None in return types
PythonTypeHint::builtin("None")
}
ty => PythonTypeHint::from_return_type(ty, parent),
},
ReturnType::Type(_, ty) => PythonTypeHint::from_return_type(*ty, parent),
}
.into()
},
Expand Down
11 changes: 10 additions & 1 deletion pyo3-macros-backend/src/type_hint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,16 @@ impl PythonTypeHint {
}
}
PythonTypeHintVariant::ReturnType(t) => {
quote! { <#t as #pyo3_crate_path::impl_::introspection::PyReturnType>::OUTPUT_TYPE }
quote! {{
#[allow(unused_imports)]
use #pyo3_crate_path::impl_::pyclass::Probe as _;
const TYPE: #pyo3_crate_path::inspect::TypeHint = if #pyo3_crate_path::impl_::pyclass::IsReturningEmptyTuple::<#t>::VALUE {
#pyo3_crate_path::inspect::TypeHint::builtin("None")
} else {
<#t as #pyo3_crate_path::impl_::introspection::PyReturnType>::OUTPUT_TYPE
};
TYPE
}}
}
PythonTypeHintVariant::Type(t) => {
quote! { <#t as #pyo3_crate_path::type_object::PyTypeCheck>::TYPE_HINT }
Expand Down
4 changes: 2 additions & 2 deletions pytests/stubs/pyclasses.pyi
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from _typeshed import Incomplete
from typing import Any, final
from typing import final

class AssertingBaseClass:
def __new__(cls, /, expected_type: type) -> AssertingBaseClass: ...
Expand Down Expand Up @@ -55,7 +55,7 @@ class PyClassThreadIter:

@final
class SubClassWithInit(dict):
def __init__(self, /, *args, **kwargs) -> Any: ...
def __init__(self, /, *args, **kwargs) -> None: ...
def __new__(cls, /, *args, **kwargs) -> SubClassWithInit: ...

def map_a_class(
Expand Down
3 changes: 3 additions & 0 deletions src/conversion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -595,6 +595,9 @@ impl<'py> IntoPyObject<'py> for () {
type Output = Bound<'py, Self::Target>;
type Error = Infallible;

#[cfg(feature = "experimental-inspect")]
const OUTPUT_TYPE: TypeHint = PyTuple::TYPE_HINT; // TODO(Tpt): should be tuple[()]

fn into_pyobject(self, py: Python<'py>) -> Result<Self::Output, Self::Error> {
Ok(PyTuple::empty(py))
}
Expand Down
10 changes: 10 additions & 0 deletions src/impl_/pyclass/probes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,16 @@ impl<T: Clone> IsClone<T> {
pub const VALUE: bool = true;
}

probe!(IsReturningEmptyTuple);

impl IsReturningEmptyTuple<()> {
pub const VALUE: bool = true;
}

impl<E> IsReturningEmptyTuple<Result<(), E>> {
pub const VALUE: bool = true;
}

#[cfg(test)]
macro_rules! value_of {
($probe:ident, $ty:ty) => {{
Expand Down
Loading