Skip to content

Cannot implement an interface that extends another interface #1783

Closed

Description

@phxnsharp

Environment

  • Pythonnet version: 3.0.0a2
  • Python version: 3.8.10
  • Operating System: Ubuntu 20.04.4 LTS (docker run --rm -it ubuntu:20.04)
  • .NET Runtime: .NET Core 6.0.202

Details

  • Describe what you were trying to get done.

    Attempting to implement interfaces defined in .NET. Works fine for simple interfaces but I receive a weird error when the interface extends IDisposable

  • What commands did you run to trigger this issue? If you can provide a
    Minimal, Complete, and Verifiable example
    this will help us understand the issue.

from clr_loader import get_coreclr
rt = get_coreclr("conf.json")
from pythonnet import set_runtime
set_runtime(rt)
import clr

import System
import System.Buffers


class MyDisposable(System.IDisposable):
    __namespace__ = "MyNamespace"

    def Dispose(self) -> None:
        print("Dispose")

md = MyDisposable()
md.Dispose()


class MyBuffer(System.Buffers.IMemoryOwner):
    __namespace__ = "MyNamespace"


    def Dispose(self) -> None:
        print("Dispose")


mb = MyBuffer()
mb.Dispose()    

conf.json:

{
  "runtimeOptions": {
    "tfm": "net6.0",
    "framework": {
      "name": "Microsoft.NETCore.App",
      "version": "6.0.4"
    }
  }
}
  • If there was a crash, please include the traceback here.
(venv) root@ca20f043eef5:/t# python demo.py
Dispose
Traceback (most recent call last):
  File "demo.py", line 21, in <module>
    class MyBuffer(System.Buffers.IMemoryOwner):
TypeError: Method 'Dispose' in type 'MyNamespace.MyBuffer' from assembly 'Python.Runtime.Dynamic, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' does not have an implementation.

Activity

  1. lostmsu commented on May 5, 2022

    @lostmsu
    Member

    Can you try adding IDisposable explicitly to the list of MyBuffer bases? I wonder if it would work around the issue. (Still looks like a valid bug to me)

  2. phxnsharp commented on May 5, 2022

    @phxnsharp
    Author
    class MyBuffer(System.Buffers.IMemoryOwner, System.IDisposable):
    (venv) root@ca20f043eef5:/t# python demo.py
    Dispose
    Traceback (most recent call last):
      File "demo.py", line 21, in <module>
        class MyBuffer(System.Buffers.IMemoryOwner, System.IDisposable):
    TypeError: cannot use multiple inheritance with managed classes

    ** Edited, I posted the wrong error originally

    Thanks for the thought. Doesn't seem to be supported?

  3. phxnsharp commented on May 6, 2022

    @phxnsharp
    Author

    Tested this on Windows 10, Python 3.8.10, Pythonnet 3.0.0a2 using .NET Framework (removed the set_runtime() stuff, merely used import clr). Had to switch to System.ComponentModel.IComponent as System.Buffers.IMemoryOwner is not available there. I get the exact same error:

    >>> import clr
    >>> import System
    >>> import System.ComponentModel
    >>> class Bla(System.ComponentModel.IComponent):
    ...   __namespace__ = "MyNamespace"
    ...
    ...   def Dispose(self) -> None:
    ...     print("Dispose")
    ...
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: Method 'Dispose' in type 'MyNamespace.Bla' from assembly 'Python.Runtime.Dynamic, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' does not have an implementation.
  4. phxnsharp commented on May 6, 2022

    @phxnsharp
    Author

    The error has nothing specifically to do with IDisposable. Any interface that extends a second interface shows this problem:

    >>> import clr
    >>> import System.Collections
    >>> class Bla(System.Collections.IDictionary):
    ...   __namespace__ = "MyNamespace"
    ...
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: Method 'CopyTo' in type 'MyNamespace.Bla' from assembly 'Python.Runtime.Dynamic, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' does not have an implementation.
  5. changed the title [-]Cannot implement an interface that extends IDisposable[/-] [+]Cannot implement an interface that extends another interface[/+] on May 6, 2022
  6. self-assigned this
    on May 9, 2022
  7. added this to the 3.0.0 milestone on May 9, 2022
  8. lostmsu commented on May 12, 2022

    @lostmsu
    Member

    I do not believe this bug is a 3.0.0 blocker, as this probably never worked in 2.5 either. For 3.0.0 we should focus on regressions and breaking changes.

  9. removed this from the 3.0.0 milestone on May 20, 2022
  10. cordsmedia commented on Feb 2, 2023

    @cordsmedia

    I'm seeing the same error with this for example:

    from enum import Enum
    class SourceKey(str, Enum):
        A = "a"
        B = "b"
    
    
    cannot use multiple inheritance with managed classes
     During the algorithm initialization, the following exception has occurred: 
    Loader.TryCreatePythonAlgorithm(): Unable to import python module /somemodule/somepython.py. 
    AlgorithmPythonWrapper(): cannot use multiple inheritance with managed classes
      at <module>
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions