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

kwargs isn't working. #318

Closed
rodyzi opened this issue Jan 3, 2019 · 0 comments
Closed

kwargs isn't working. #318

rodyzi opened this issue Jan 3, 2019 · 0 comments
Labels

Comments

@rodyzi
Copy link

rodyzi commented Jan 3, 2019

https://stackoverflow.com/questions/54015328/calling-from-python-rust-function-with-kwargs-fails

🌍 Environment

  • Your operating system and version: Arch Linux 4.19.12
  • Your python version: 3.7.1
  • How did you install python (e.g. apt or pyenv)? Did you use a virtualenv?: system package manager - pacman
  • Your rust version (rustc --version): 1.33.0-nightly 2018-12-22
  • Are you using the latest pyo3 version? Have you tried using latest master? yes

💥 Reproducing

#![feature(custom_attribute)]
#![feature(specialization)]

use pyo3::prelude::*;
use pyo3::types::{PyDict, PyTuple};

#[pyclass]
struct MyClass {}

#[pymethods]
impl MyClass {
    #[staticmethod]
    #[args(kwargs = "**")]
    fn test1(kwargs: Option<&PyDict>) -> PyResult<()> {
        if let Some(kwargs) = kwargs {
            for kwarg in kwargs {
                println!("{:?}", kwarg);
            }
        } else {
            println!("kwargs is none");
        }
        Ok(())
    }

    #[staticmethod]
    #[args(args = "*")]
    fn test2(args: &PyTuple) -> PyResult<()> {
        for arg in args {
            println!("{:?}", arg);
        }
        Ok(())
    }
}


#[pymodule]
fn test123(_py: Python, m: &PyModule) -> PyResult<()> {
    m.add_class::<MyClass>()?;
    Ok(())
}

Install using pyo3-pack.

pyo3-pack build --interpreter python3.7
cd target/wheels
sudo pip install --force-reinstall test123-0.1.0-cp37-cp37m-manylinux1_x86_64.whl
import test123
test123.MyClass.test1(test=1) // fails with `MyClass.test1()() takes at most 0 argument (1 given)`
test123.MyClass.test2(1) // success
@konstin konstin added the bug label Jan 4, 2019
Alexander-N added a commit to Alexander-N/pyo3 that referenced this issue Jan 24, 2019
kwargs was broken by a check for the number of given arguments. Only
apply this check if no arbitary number of keyword arguments are allowed
by a "**" parameter of `#[args(...)`.

Closes PyO3#318
Alexander-N added a commit to Alexander-N/pyo3 that referenced this issue Jan 24, 2019
kwargs was broken by a check for the number of given arguments. Only
apply this check if no arbitary number of keyword arguments are allowed
by a "**" parameter of `#[args(...)`.

Closes PyO3#318
Alexander-N added a commit to Alexander-N/pyo3 that referenced this issue Jan 28, 2019
kwargs was broken by a check for the number of given arguments. Only
apply this check if no arbitary number of keyword arguments are allowed
by a "**" parameter of `#[args(...)`.

Closes PyO3#318
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants