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

Skip to content

"TypeError: No method matches given arguments" error for a virtual method with a template parameter #1522

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
rayrapetyan opened this issue Aug 19, 2021 · 2 comments · Fixed by #1657
Assignees
Labels
Milestone

Comments

@rayrapetyan
Copy link

rayrapetyan commented Aug 19, 2021

Environment

Details

Trying to re-implement example from here using Python.

Everything works fine except this line of code:

    slideMasterPart1.AddPart(slideLayoutPart1, "rId1");

There are two definitions of AddPart:

    public virtual T AddPart<T>(T part)  where T : OpenXmlPart
    public virtual T AddPart<T>(T part, string id)  where T : OpenXmlPart

slideMasterPart1.AddPart.Overloads output:

T AddPart[T](T)
T AddPart[T](T, System.String)

What works:

    slideMasterPart1.AddPart(slideLayoutPart1)
    slideMasterPart1.AddPart[SlideLayoutPart](slideLayoutPart1)

What doesn't work:

    slideMasterPart1.AddPart(slideLayoutPart1, "rId1")
    slideMasterPart1.AddPart(slideLayoutPart1, System.String("rId1"))
    slideMasterPart1.AddPart[SlideLayoutPart](slideLayoutPart1, "rId1")

Error:

Python.Runtime.PythonException: DocumentFormat.OpenXml.Packaging.SlideLayoutPart value cannot be converted to T

The above exception was the direct cause of the following exception:

System.ArgumentException: DocumentFormat.OpenXml.Packaging.SlideLayoutPart value cannot be converted to T in method T AddPart[T](T, System.String) ---> Python.Runtime.PythonException: DocumentFormat.OpenXml.Packaging.SlideLayoutPart value cannot be converted to T
   --- End of inner exception stack trace ---

The above exception was the direct cause of the following exception:

System.AggregateException: One or more errors occurred. (DocumentFormat.OpenXml.Packaging.SlideLayoutPart value cannot be converted to T in method T AddPart[T](T, System.String)) ---> System.ArgumentException: DocumentFormat.OpenXml.Packaging.SlideLayoutPart value cannot be converted to T in method T AddPart[T](T, System.String) ---> Python.Runtime.PythonException: DocumentFormat.OpenXml.Packaging.SlideLayoutPart value cannot be converted to T
   --- End of inner exception stack trace ---
   --- End of inner exception stack trace ---
---> (Inner Exception #0) System.ArgumentException: DocumentFormat.OpenXml.Packaging.SlideLayoutPart value cannot be converted to T in method T AddPart[T](T, System.String) ---> Python.Runtime.PythonException: DocumentFormat.OpenXml.Packaging.SlideLayoutPart value cannot be converted to T
   --- End of inner exception stack trace ---<---

The above exception was the direct cause of the following exception:
...
slideMasterPart1.AddPart(slideLayoutPart1, "rId1");
TypeError: No method matches given arguments for AddPart: (<class 'DocumentFormat.OpenXml.Packaging.SlideLayoutPart'>, <class 'str'>)

mcve:

import clr

clr.AddReference('DocumentFormat.OpenXml')

from DocumentFormat.OpenXml.Packaging import (
    SlideLayoutPart,
    SlideMasterPart,
)

slideLayoutPart1: SlideLayoutPart = SlideLayoutPart()
slideMasterPart1: SlideMasterPart = SlideMasterPart()
slideMasterPart1.AddPart(slideLayoutPart1)  # this works OK
slideMasterPart1.AddPart(slideLayoutPart1, "rId1")  # this raises a runtime exception

So it's only when the second (string) parameter is provided, the first can't be converted. Looks like a pythonnet issue.

@filmor
Copy link
Member

filmor commented Sep 24, 2021

Thank you for the detailed report. I have looked into this, and I think the culprit is mixing of generic and non-generic parameters.

@filmor filmor added the bug label Sep 28, 2021
@filmor filmor added this to the 3.0.0 milestone Sep 28, 2021
@filmor filmor self-assigned this Sep 28, 2021
@LeeDongGeon1996
Copy link

Same issue with me. Is there any progress on this?

lostmsu added a commit to losttech/pythonnet that referenced this issue Jan 4, 2022
Prior to this change if method had multiple generic overloads, only 1 of them could be matched (whichever one reflection would return first)

Now MethodBinder.MatchParameters returns all matching generic overloads, not just the first one.

fixes pythonnet#1522
lostmsu added a commit that referenced this issue Jan 5, 2022
Prior to this change if method had multiple generic overloads, only 1 of them could be matched (whichever one reflection would return first)

Now MethodBinder.MatchParameters returns all matching generic overloads, not just the first one.

fixes #1522
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.

3 participants