question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

From https://github.com/pythonnet/pythonnet/pull/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.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
lostmsucommented, 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).

0reactions
LeeDongGeon1996commented, 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?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Pythonnet: Why are .NET enums type casted to int?
NET enums should not be automatically converted to Python int and back. The only workaround would be to add a C# helper dll...
Read more >
Enum HOWTO — Python 3.11.4 documentation
An Enum is a set of symbolic names bound to unique values. They are similar to global variables, but they offer a more...
Read more >
Making Enums (as always, arguably) more Pythonic
Though it's less dangerous for enum with string values than with int values (IntEnum is a bad idea IMHO), I prefer having 2...
Read more >
How to use Enums when using Entity Framework Core with ...
A value conversion, unsurprisingly, automatically converts a value stored in the database into something else that you can use in code. So, in ......
Read more >
Convert int to enum in C#
Here you will learn how to convert Int to Enum in C#. ... You can explicitly type cast an int to a particular...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found