Skip to content

.NET enums should not be automatically converted to Python int and back #1220

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
lostmsu opened this issue Sep 13, 2020 · 7 comments
Closed
Milestone

Comments

@lostmsu
Copy link
Member

lostmsu commented Sep 13, 2020

From #1179

In Python, it should be possible to refer to enum members: MyEnum.Member, and to create arbitrary enum values from int explicitly like this: MyEnum(42). But by default a number and a value of some enum should not be freely convertible to each other. E.g. this should fail:

void Method(MyEnum value) => ...;

Python:

Method(42); # <- this should be a TypeError

We can add a generic enum codec, that could be enabled on demand to restore old behavior.

@tminka
Copy link
Contributor

tminka commented Jan 3, 2021

This is related to #1099

@wollmich
Copy link

I support that. This would solve a lot of problems, e.g.: https://stackoverflow.com/q/65805093/7556646.

@lostmsu lostmsu added this to the 3.0.0 milestone Jan 20, 2021
@TheCollegedude
Copy link

Is there a workaround? I tried using IPyObjectDecoder but it is marked as "obsolete". The wiki, where I found it, seems to be
outdated.

lostmsu added a commit to losttech/pythonnet that referenced this issue Feb 20, 2021
- enums are no longer converted to and from PyLong automatically pythonnet#1220
- one can construct an instance of MyEnum from Python using MyEnum(numeric_val), e.g. MyEnum(10)
- in the above, if MyEnum does not have [Flags] and does not have value 10 defined, to create MyEnum with value 10 one must call MyEnum(10, True). Here True is an unnamed parameter, that allows unchecked conversion
- legacy behavior has been moved to a codec (EnumPyLongCodec); enums can now be encoded by codecs
lostmsu added a commit to losttech/pythonnet that referenced this issue Feb 20, 2021
- enums are no longer converted to and from PyLong automatically pythonnet#1220
- one can construct an instance of MyEnum from Python using MyEnum(numeric_val), e.g. MyEnum(10)
- in the above, if MyEnum does not have [Flags] and does not have value 10 defined, to create MyEnum with value 10 one must call MyEnum(10, True). Here True is an unnamed parameter, that allows unchecked conversion
- legacy behavior has been moved to a codec (EnumPyLongCodec); enums can now be encoded by codecs
@lostmsu lostmsu mentioned this issue Feb 20, 2021
2 tasks
lostmsu added a commit to losttech/pythonnet that referenced this issue Feb 20, 2021
- enums are no longer converted to and from PyLong automatically pythonnet#1220
- one can construct an instance of MyEnum from Python using MyEnum(numeric_val), e.g. MyEnum(10)
- in the above, if MyEnum does not have [Flags] and does not have value 10 defined, to create MyEnum with value 10 one must call MyEnum(10, True). Here True is an unnamed parameter, that allows unchecked conversion
- legacy behavior has been moved to a codec (EnumPyLongCodec); enums can now be encoded by codecs
@lostmsu
Copy link
Member Author

lostmsu commented Feb 20, 2021

@TheCollegedude I've just submitted a new PR for master branch to disable default conversion.

If you are planning to stay on 2.5, you can use IPyObjectDecoder. It is marked obsolete, as it might change in 3.0 (so far it has not).

lostmsu added a commit to losttech/pythonnet that referenced this issue Feb 21, 2021
- enums are no longer converted to and from PyLong automatically pythonnet#1220
- one can construct an instance of MyEnum from Python using MyEnum(numeric_val), e.g. MyEnum(10)
- in the above, if MyEnum does not have [Flags] and does not have value 10 defined, to create MyEnum with value 10 one must call MyEnum(10, True). Here True is an unnamed parameter, that allows unchecked conversion
- legacy behavior has been moved to a codec (EnumPyLongCodec); enums can now be encoded by codecs
lostmsu added a commit to losttech/pythonnet that referenced this issue Feb 21, 2021
- enums are no longer converted to and from PyLong automatically pythonnet#1220
- one can construct an instance of MyEnum from Python using MyEnum(numeric_val), e.g. MyEnum(10)
- in the above, if MyEnum does not have [Flags] and does not have value 10 defined, to create MyEnum with value 10 one must call MyEnum(10, True). Here True is an unnamed parameter, that allows unchecked conversion
- legacy behavior has been moved to a codec (EnumPyLongCodec); enums can now be encoded by codecs
- flags enums support bitwise ops via EnumOps class
lostmsu added a commit to losttech/pythonnet that referenced this issue Feb 21, 2021
- enums are no longer converted to and from PyLong automatically pythonnet#1220
- one can construct an instance of MyEnum from Python using MyEnum(numeric_val), e.g. MyEnum(10)
- in the above, if MyEnum does not have [Flags] and does not have value 10 defined, to create MyEnum with value 10 one must call MyEnum(10, True). Here True is an unnamed parameter, that allows unchecked conversion
- legacy behavior has been moved to a codec (EnumPyLongCodec); enums can now be encoded by codecs
- flags enums support bitwise ops via EnumOps class
lostmsu added a commit to losttech/pythonnet that referenced this issue Mar 30, 2021
- enums are no longer converted to and from PyLong automatically pythonnet#1220
- one can construct an instance of MyEnum from Python using MyEnum(numeric_val), e.g. MyEnum(10)
- in the above, if MyEnum does not have [Flags] and does not have value 10 defined, to create MyEnum with value 10 one must call MyEnum(10, True). Here True is an unnamed parameter, that allows unchecked conversion
- legacy behavior has been moved to a codec (EnumPyLongCodec); enums can now be encoded by codecs
- flags enums support bitwise ops via EnumOps class
lostmsu added a commit that referenced this issue Mar 30, 2021
- enums are no longer converted to and from PyLong automatically #1220
- one can construct an instance of MyEnum from Python using MyEnum(numeric_val), e.g. MyEnum(10)
- in the above, if MyEnum does not have [Flags] and does not have value 10 defined, to create MyEnum with value 10 one must call MyEnum(10, True). Here True is an unnamed parameter, that allows unchecked conversion
- legacy behavior has been moved to a codec (EnumPyLongCodec); enums can now be encoded by codecs
- flags enums support bitwise ops via EnumOps class
@LeeDongGeon1996
Copy link

LeeDongGeon1996 commented Sep 18, 2021

I think this should be a hot fix.

If there is C# codes like the below:

namespace myname.mynamespace
{
    public static partial class MyClass
    {
        public static IEnumerable<MyResult> MyFunc<TSometing>(
            this IEnumerable<TSomething> something,
            int num)
            where TSomething : ISomething
        {
             // do something.
        }

        public static IEnumerable<MyResult> MyFunc<TSometing>(
            this IEnumerable<TSomething> something,
            int num,
            MyEnum myenum = MyEnum.Elem)
            where TSomething : ISomething
        {
              // do something.
        }
    }
}

The second overloaded method can never be called:

from myname.mynamespace import MyClass
from myname.mynamespace import MyEnum

#Something is sub-class of ISomething .
MyClass.MyFunc[Something](something_list, 10, MyEnum.Elem2)

>>>
TypeError: No method matches given arguments for MyFunc: (<class 'System.Collections.Generic.0, Culture=neutral, PublicKeyToken=null]]'>, <class 'int'>, <class 'int'>)

I also tried to call the second one manually by using Overload(__overload__), fetching Type from System.Reflection, it doesn't work

@lostmsu
Copy link
Member Author

lostmsu commented Sep 18, 2021

@LeeDongGeon1996 this has already been fixed in pythonnet 3.0 previews. We do not plan to backport it.

@lostmsu lostmsu closed this as completed Sep 18, 2021
@LeeDongGeon1996
Copy link

LeeDongGeon1996 commented Sep 22, 2021

@lostmsu Thanks. But, I have a problem with using 3.0 preview.
I'm using it on Anaconda env with Mono on Mac. Is there any way to install preview with conda install -c conda-forge command?

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

No branches or pull requests

5 participants