2

How can I get generics to work in Python.NET with CPython. I get an error when using the subscript syntax from Python.NET Using Generics

TypeError: unsubscriptable object

With Python 2.7.11 + pythonnet==2.1.0.dev1

>python.exe
Python 2.7.11 (v2.7.11:6d1b6a68f775, Dec  5 2015, 20:32:19) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import clr
>>> from System import EventHandler
>>> from System import EventArgs
>>> EventHandler[EventArgs]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unsubscriptable object

I've also tried pythonnet==2.0.0 and building form github ca15fe8 with ReleaseWin x86 and got the same error.

With IronPython-2.7.5:

>ipy.exe
IronPython 2.7.5 (2.7.5.0) on .NET 4.0.30319.0 (32-bit)
Type "help", "copyright", "credits" or "license" for more information.
>>> import clr
>>> from System import EventHandler
>>> from System import EventArgs
>>> EventHandler[EventArgs]
<type 'EventHandler[EventArgs]'>
2
  • Your ironpython example is not complete, this question seems outdated from the one you asked on github Commented Feb 19, 2016 at 17:53
  • 1
    @denfromufa I've updated my examples. With IronPython the subscript syntax instantiates the EventHandler as expected but with CPython+Python.NET it returns an error. Commented Feb 20, 2016 at 23:23

2 Answers 2

4
+100

You can get the generic class object explicitly:

EventHandler = getattr(System, 'EventHandler`1')

The number indicates the number of generic arguments.

Sign up to request clarification or add additional context in comments.

1 Comment

in pythonnet 2.1 you fixed this issue! please edit your answer.
1

That doesn't work because there are both generic and non-generic versions of the EventHandler class that exist in the System namespace. The name is overloaded. You need to indicate that you want the generic version.

I'm not sure how exactly Python.NET handles overloaded classes/functions but it seems like it has an Overloads property. Try something like this and see how that goes:

EventHandler_EventArgs = EventHandler.Overloads[EventArgs]

(this doesn't work)

It doesn't seem like Python.NET has ways to resolve the overloaded class name, each overload is treated as separate distinct types (using their clr names). It doesn't do the same thing as IronPython and lump them into a single containing type.

In this particular case, the EventArgs name corresponds to the non-generic EventArgs type. You'll need to get the appropriate type directly from the System module as filmor illustrates.

2 Comments

I get a "TypeError: No match found for constructor signature" when I try that.
Hmm, I guess the Overloads property only applies to methods (in this case, constructors). It would be a shame if Python.net didn't offer a way to do this natively.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.