Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Can not call method in parent class, when using nested classes with inheritance #1420

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

Closed
koliyo opened this issue Mar 18, 2021 · 1 comment · Fixed by #1652
Closed

Can not call method in parent class, when using nested classes with inheritance #1420

koliyo opened this issue Mar 18, 2021 · 1 comment · Fixed by #1652
Labels
Milestone

Comments

@koliyo
Copy link

koliyo commented Mar 18, 2021

Environment

Pythonnet version: 2.5.1
Python version: Python 3.9.2
Operating System: MacOS
.NET Runtime: 5.0.200

Details

Related to the problem reported in #1414

I.e. classes-in-classes are not handled properly in Pythonnet.

Even when applying the workaround mentioned in the #1414 issue there are still problems related to the inheritance.

Namely, I can not call a method declared on the baseclass, on an instance of the nested child class.

The following code demonstrates the issue

namespace pythonnet_testing {

  public class Bar {

    public string MethodInBaseClass() {
      return "hello";
    }

    public class Hej : Bar {
    }
  }

  public class Foo {
    public Bar Bar;

    public static Foo Create() {
      Foo f = new();
      f.Bar = new Bar.Hej();
      return f;
    }
  }

  class Program {
    static void Main(string[] args) {

      var pyCode = @"
import pythonnet_testing
f = pythonnet_testing.Foo.Create()
print('Trying to access member of nested subclass')
_ = pythonnet_testing.Bar # NOTE: The python code crashes unless this explicit reference to the Bar class is used
print(f'Bar: {f.Bar}')
print(f'Method call: {f.Bar.MethodInBaseClass()}')

      ";

      PythonEngine.Initialize();

      using (Py.GIL()) {
        PythonEngine.Exec(pyCode);
      }
    }
  }

Running this gives the following error message

Trying to access member of nested subclass
Bar: pythonnet_testing.Bar+Hej
Unhandled exception. Python.Runtime.PythonException: AttributeError : 'Hej' object has no attribute 'MethodInBaseClass'
['  File "<string>", line 7, in <module>\n']   at Python.Runtime.PythonException.ThrowIfIsNull(BorrowedReference reference)
   at Python.Runtime.PythonEngine.RunString(String code, Nullable`1 globals, Nullable`1 locals, RunFlagType flag)

In order to call the MethodInBaseClass method on the child class, I must explicitly re-define it like this:

    public class Hej : Bar {
      public new string MethodInBaseClass() => base.MethodInBaseClass();
    }
@lostmsu lostmsu added this to the 3.0.0 milestone Sep 23, 2021
@lostmsu
Copy link
Member

lostmsu commented Sep 23, 2021

I believe #1487 should have fixed the issue for 3.0, but we need a test.

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

Successfully merging a pull request may close this issue.

2 participants