Skip to content
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

Type issues when using pyo3 function signatures #82

Open
CSNWEB opened this issue Sep 18, 2024 · 4 comments · May be fixed by #125
Open

Type issues when using pyo3 function signatures #82

CSNWEB opened this issue Sep 18, 2024 · 4 comments · May be fixed by #125

Comments

@CSNWEB
Copy link

CSNWEB commented Sep 18, 2024

Hello,

I faced an issue when using the generator together with pyo3 function signatures and Optional values. If I add the generator macro at the bottom I get useful types, but the optional arguments are missing a default value. For example this generates

#[pyfunction]
#[pyo3(signature = (inputs, output, size_dict, use_ssa=None))]
#[gen_stub_pyfunction]
fn optimize_simplify(
    inputs: Vec<Vec<char>>,
    output: Vec<char>,
    size_dict: Dict<char, f32>,
    use_ssa: Option<bool>,
) -> SSAPath {
}

this

def optimize_simplify(inputs:typing.Sequence[typing.Sequence[str]],output:typing.Sequence[str],size_dict:typing.Mapping[str, float],use_ssa:typing.Optional[bool]) -> list[list[int]]:
    ...

However this then throws an error in python when not specifiying all optional arguments. If I use the generator macro at the top the signature is fine, but the types are all gone:

#[gen_stub_pyfunction]
#[pyfunction]
#[pyo3(signature = (inputs, output, size_dict, use_ssa=None))]
...
def optimize_simplify(inputs,output,size_dict,use_ssa = ...) -> list[list[int]]:
    ...
@Tristramg
Copy link

I stumbled on the same problem.

As my functions are defined in an impl using #[gen_stub_pyclass], I get the second situation you describe

@ThatOneShortGuy
Copy link
Contributor

In my experience, #[gen_stub_pyfunction] (or similar) needs to be the first macro. Otherwise, it doesn't generate right or at all.

@CSNWEB
Copy link
Author

CSNWEB commented Sep 19, 2024

But in this case putting it first gives no types at all.

@flying-sheep
Copy link
Contributor

Also in case of an #[gen_stub_pymethods] impl ... block, the pyo3(signature = ...) is always on methods inside of the block.

@flying-sheep flying-sheep linked a pull request Dec 18, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants