PyO3 / PyO3/pyo3

using subclass, args on new, and overriding __init__ fails

Open
#1,644 5 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

1.0-candidate confusing-api documentation needs-design
Dominant language
Rust
Stars
16.2k
Forks
1k
Avg merge
2d 6h
Merged PRs (30d)
66

Description

This issue is about sub classing with overridden init ; This bug prevents it. Details below.

🐛 Bug Reports

When reporting a bug, please provide the following information. If this is not a bug report you can just discard this template.

🌍 Environment
  • Your operating system and version: linux, ubuntu focal (20.04)
  • Your python version: 3.8
  • How did you install python (e.g. apt or pyenv)? Did you use a virtualenv? ubuntu package (apt-get). using a virtualenv for testing.
  • Your Rust version (rustc --version): rustc 1.54.0-nightly (b663c0f4f 2021-05-29)
  • Your PyO3 version: master 8bf3adee3a0da3ce1cf19ed5e20bd4759edfa3ae (v0.8.0-1694-g8bf3adee3) (Merge pull request #1641 from 1tgr/for-each)
  • Have you tried using latest PyO3 main (replace version = "0.x.y" with git = "https://github.com/PyO3/pyo3")?yes.
💥 Reproducing

I have a branch with the augmented test (examples/pyo3-pytests/src/subclassing.rs & examples/pyo3-pytests/tests/test_subclassing.py), at https://github.com/alonblade/pyo3/tree/add-test-for-inheritance-with-init ; Here is the change:

#[pyclass(subclass)]
pub struct SubclassableWithParameter {}

#[pymethods]
impl SubclassableWithParameter {
    #[new]
    fn new(foo: bool) -> Self {
        SubclassableWithParameter {}
    }
}
class SubclassWithExtraInitArguments(SubclassableWithParameter):
    def __init__(self, bar):
        print("before super init")
        super().__init__(foo=bar * 2)


def test_subclass_with_init():
    s = SubclassWithExtraInitArguments(10)

And the output

tests/test_subclassing.py .F                                                                                                                                                                                  [100%]

===================================================================================================== FAILURES ======================================================================================================
______________________________________________________________________________________________ test_subclass_with_init ______________________________________________________________________________________________

    def test_subclass_with_init():
>       s = SubclassWithExtraInitArguments(10)
E       TypeError: argument 'foo': 'int' object cannot be converted to 'PyBool'

tests/test_subclassing.py:27: TypeError

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the reproducer in examples/pyo3-pytests/src/subclassing.rs and examples/pyo3-pytests/tests/test_subclassing.py, then run test_subclass_with_init to confirm the TypeError. Trace how the subclass calls super().init(foo=bar * 2); done means this test passes without the reported conversion failure.

Written by the indexing model from the issue text.

Assessment

Tech stack
python, rust
Domain
api
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.