Giter Site home page Giter Site logo

do we need NoArgs? about pyo3 HOT 14 CLOSED

pyo3 avatar pyo3 commented on May 10, 2024
do we need NoArgs?

from pyo3.

Comments (14)

messense avatar messense commented on May 10, 2024

If we add a

impl<T: IntoPyTuple> IntoPyTuple for Option<T> {
    fn into_tuple(self, py: Python) -> Py<PyTuple> {
        match self {
            Some(v) => v.into_tuple(py),
            None => PyTuple::empty(py)
        }
    }
}

We can use None instead of NoArgs

from pyo3.

fafhrd91 avatar fafhrd91 commented on May 10, 2024

Super! Let's do that

from pyo3.

messense avatar messense commented on May 10, 2024

Unfortunately

error[E0282]: type annotations needed
   --> tests/test_class.rs:118:26
    |
118 |     assert!(typeobj.call(None, None).unwrap().cast_as::<EmptyClassWithNew>().is_ok());
    |                          ^^^^ cannot infer type for `T`

error: aborting due to previous error(s)

call(args, kwargs) can't use None as args because into_typle can't infer type for T (None is a variant of Option while NoArgs is a struct). So we stuck with () or NoArgs.

from pyo3.

fafhrd91 avatar fafhrd91 commented on May 10, 2024

What if you use NoArgs as T?

from pyo3.

fafhrd91 avatar fafhrd91 commented on May 10, 2024

Ah, nevermind

from pyo3.

fafhrd91 avatar fafhrd91 commented on May 10, 2024

is it possible to use default generic type parameter?

from pyo3.

messense avatar messense commented on May 10, 2024

I am not sure what you mean by default generic type parameter.

from pyo3.

fafhrd91 avatar fafhrd91 commented on May 10, 2024

https://github.com/rust-lang/rust/blob/master/src/test/run-pass/generic-default-type-params.rs

from pyo3.

fafhrd91 avatar fafhrd91 commented on May 10, 2024

rust-lang/rust#26870

from pyo3.

messense avatar messense commented on May 10, 2024

default generic type parameter seems only allowed in type declaration now.

error: defaults for type parameters are only allowed in `struct`, `enum`, `type`, or `trait` definitions.
   --> src/objectprotocol.rs:250:13
    |
250 |     fn call<A = ()>(&self, args: A, kwargs: Option<&PyDict>) -> PyResult<&PyObjectRef>
    |             ^
    |
    = note: #[deny(invalid_type_param_default)] on by default
    = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
    = note: for more information, see issue #36887 <https://github.com/rust-lang/rust/issues/36887>

error: aborting due to previous error(s)

error: Could not compile `pyo3`.

rust-lang/rust#36887

from pyo3.

messense avatar messense commented on May 10, 2024

If we change call signature to

fn call<A>(&self, args: A, kwargs: Option<&PyDict>) -> PyResult<&PyObjectRef>
    where A: Into<Option<Py<PyTuple>>>

Use should be able to pass None, Some(PyTuple::xxx) and PyTuple::xxx since Option has the following impl since 1.12.0:

impl<T> From<T> for Option<T>

https://doc.rust-lang.org/src/core/option.rs.html#854-858

But that would require user to provide a PyTuple object.

from pyo3.

fafhrd91 avatar fafhrd91 commented on May 10, 2024

I think it would reduce usability. i use IntoPyTuple much more often, plus NoArg is not big deal

from pyo3.

fafhrd91 avatar fafhrd91 commented on May 10, 2024

we can play with associated types, similar to class protocols. but I am not sure if it could be implemented

from pyo3.

fafhrd91 avatar fafhrd91 commented on May 10, 2024

lets close this issue, NoArgs is not priority at the moment.

from pyo3.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.