Skip to content

Pythonnet 2.1 not identifying System.Object type #203

Closed

Description

@vmuriart

I'm not sure how the tests wouldn't have caught this bug.
I have a .net class that has the function AddQuery with the following method signatures.

AddQuery
    Void AddQuery(api.QueryType)...
    Void AddQuery(System.Object)...

where QueryType is another custom object type.

On 2.0 and 2.1dev1 the function works as expected.

  • I can pass a QueryType object to AddQuery and it would call the first method.
  • Calling AddQuery with a string or int would use the second method and process them.

On 2.1.0 and the earliest archived copy in appveyor this behavior changed.

  • Passing QueryType object to AddQuery still works.
  • Passing string type to AddQuery errors with No method matches given arguments

I've tested on Python 2.7, Python 3.5 both x64.
Looks like Pythonnet is no longer identifying string int and other types to be of type System.Object as well.

Activity

  1. vmuriart commented on Apr 17, 2016

    @vmuriart
    ContributorAuthor

    Possibly related to #187
    Tested on windows 7 and 8.1

  2. vmuriart commented on Apr 17, 2016

    @vmuriart
    ContributorAuthor

    Tried build before and after #176, both still have this bug present.

  3. den-run-ai commented on Apr 17, 2016

    @den-run-ai
    Contributor

    @vmuriart
    try passing System.Object("your_python_string") or System.Object(123) for string or int types respectively.

    this breaking change is because implicit casting can be dangerous:

    #131

  4. vmuriart commented on Apr 17, 2016

    @vmuriart
    ContributorAuthor

    @denfromufa just tried your suggestion and it semi-works. It passes through pythonnet and passes the Object-type string to the library I'm using, and breaks there. The underlying function is expecting a String-type string and fails to identify the Object-type string as such.

  5. den-run-ai commented on Apr 17, 2016

    @den-run-ai
    Contributor

    did you try System.String() and System.Int32()? or maybe even System.Object(System.String())...

  6. vmuriart commented on Apr 17, 2016

    @vmuriart
    ContributorAuthor

    I did. those to work, but have to explicitly cast each type before sending it.
    I'm taking a look at the src; I think there might be a better solution for the original bug #131 that would both fix the bug and not break legacy code. Might even try to fix #90 finally 😄

  7. den-run-ai commented on Apr 17, 2016

    @den-run-ai
    Contributor

    @vmuriart I'm thinking of context manager that would allow you to specify "unsafe" casting operations like before for overloaded methods, see discussion here (my 4th comment):

    #174

  8. den-run-ai commented on Apr 17, 2016

    @den-run-ai
    Contributor

    @vmuriart I think for your case, the overloading resolution in pythonnet could be improved by checking isinstance("string",System.Object) which corresponds to is operator in c#, but this is still dangerous - what if both types are present in 2 different overloaded methods?

  9. vmuriart commented on Apr 17, 2016

    @vmuriart
    ContributorAuthor

    I see your point. I'm not familiar enough with the source code in relation to how pythonnet decides which method to call.
    The way I was thinking of it is that each Python type has an underlying c-type associated to it which should give an indication how to properly cast it into its equivalent .net counterpart. That may or may not work as a solution here though

  10. den-run-ai commented on Apr 17, 2016

    @den-run-ai
    Contributor

    @vmuriart python and .net types cannot be directly mapped to c-types due to much more complicated type system. all conversions are manually taken care for basic types in pythonnet:

    https://github.com/pythonnet/pythonnet/blob/master/src/runtime/converter.cs

  11. vmuriart commented on Apr 18, 2016

    @vmuriart
    ContributorAuthor

    The problem that I am having with it at the moment is that quite a few of the libraries have broken with 2.1 release. Not sure if in this case the benefit outweighs the loss of backward compatibility.
    @tonyroberts it seems pr #151 has introduced a few incompatibilities in the attempt to solve #131. Can #151 be reverted until a better fix is in place?

  12. den-run-ai commented on Apr 18, 2016

    @den-run-ai
    Contributor

    Can we just look at all types of incompatibilities that you experience with
    new method overloading?

    Then we should really think whether the wrong results from
    method overloading are less important than possible exceptions
    with incompatible types? It would be very hard to spot the former and very
    easy to fix or workaround the latter.

    On Sunday, April 17, 2016, Vik [email protected] wrote:

    The problem that I am having with it at the moment is that quite a few of
    the libraries have broken with 2.1 release. Not sure if in this case the
    benefit outweighs the loss of backward compatibility.
    @tonyroberts https://github.com/tonyroberts it seems pr #151
    #151 has introduced a few
    incompatibilities in the attempt to solve #131
    #131. Can #151
    #151 be reverted until a b
    etter fix is in place?

    —
    You are receiving this because you were mentioned.
    Reply to this email directly or view it on GitHub
    #203 (comment)

  13. tonyroberts commented on Apr 18, 2016

    @tonyroberts
    Contributor

    @vmuriart @denfromufa my preference would be to attempt to fix 2.1 to fix the backward compatibilities, rather than just roll back. If it's just adding a case so that System.Object matches everything then hopefully that wouldn't be a big change, would it? Or are there more problems than that?

  14. tonyroberts commented on Apr 18, 2016

    @tonyroberts
    Contributor

    @vmuriart would it be easy for you to add a few unit tests to check what's currently breaking? I think it's better to show the build currently as broken, if the only reason it's not showing as broken now is just lack of test coverage! :)

  15. den-run-ai commented on Apr 18, 2016

    @den-run-ai
    Contributor

    @tonyroberts so match even python object on System.Object?

  16. 16 remaining items

  17. matthid commented on Dec 4, 2016

    @matthid
    Contributor

    Just an idea: Maybe Roslyn can help with issues like these. If we can re-use their (or parts of their) overload resolution logic we don't need to reimplement the spec.

    On the other hand: Going a different route is valid as well. In F# you actually need to mark implicit casts with an operator (while this case would still work as everything is an object)

  18. den-run-ai commented on Dec 4, 2016

    @den-run-ai
    Contributor

    @matthid i was thinking about re-using roslyn as well, but then we have a lot of python-specific logic.

    what do you mean by "On the other hand: Going a different route is valid as well"?

  19. matthid commented on Dec 4, 2016

    @matthid
    Contributor

    What I meant was that pythonnet doesn't need to mirror C# overload resolution logic. Instead it is completely fine to define its own (possibly simpler) logic. For this issue I'd say that most people expect this to work. But IMHO it could be closed as "wontfix" as well (and say that "by-design" types need to match exactly).

    It depends on what spec/design guideline this project follows. As you are trying to mirror ironpython(?) this is most likely a bug :)

  20. added a commit that references this issue on Jan 6, 2017
  21. added a commit that references this issue on Feb 14, 2017
  22. den-run-ai commented on Feb 15, 2017

    @den-run-ai
    Contributor

    it appears that precedence of types is simplified version of Jython, found by @vmuriart 👍

    https://github.com/pythonnet/pythonnet/blob/master/src/runtime/methodbinder.cs#L170

    I'm not sure how this compares to IronPython, but likely very similar due to the same author:
    https://en.wikipedia.org/wiki/Jim_Hugunin

  23. vmuriart commented on Feb 15, 2017

    @vmuriart
    ContributorAuthor

    Fun fact on the history of Jim and IronPython.

    IronPython was created in 2005 by Jim Hugunin to prove that the .NET Framework was a poor platform for dynamic languages. He failed to do so, and IronPython was born.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions