use enum name in repr by koubaa · Pull Request #2239 · pythonnet/pythonnet
Repository navigation
use enum name in repr - #2239
Merged
lostmsu merged 8 commits into pythonnet:master from koubaa:pythonnet-enum-repr Sep 22, 2023
Merged
use enum name in repr #2239 lostmsu merged 8 commits into pythonnet:master from koubaa:pythonnet-enum-repr
Conversation
koubaa commented Sep 18, 2023 · edited Loading
Copy link Copy Markdown
Contributor
What does this implement/fix? Explain your changes.
Proposal to fix #2238
Before the changes:
Python 3.11.4 (tags/v3.11.4:d2340ef, Jun 7 2023, 05:45:37) [MSC v.1934 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import clr; import System
>>> System.DayOfWeek.Monday
<System.DayOfWeek object at 0x0000024D5BAC7300>
IronPython (as a reference):
IronPython 2.7.4 (2.7.0.40) on .NET 4.0.30319.42000 (64-bit)
Type "help", "copyright", "credits" or "license" for more information.
>>> import System
>>> System.DayOfWeek.Monday
System.DayOfWeek.Monday
With these changes:
Python 3.11.4 (tags/v3.11.4:d2340ef, Jun 7 2023, 05:45:37) [MSC v.1934 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import clr; import System
>>> System.DayOfWeek.Monday
DayOfWeek.Monday
Checklist
- Make sure to include one or more tests for your change
- If an enhancement PR, please create docs and at best an example
- Ensure you have signed the .NET Foundation CLA
- Add yourself to
AUTHORS - Updated the
CHANGELOG
use enum name in repr
d9c326c
update test
3acd1ca
changelog
be2ae67
This was referenced Sep 18, 2023
Enum repr
ansys/ansys-pythonnet#8
Merged
[Embedding] Enum values are integers, they should be string-based for usability
ansys/pymechanical#240
Closed
lostmsu commented Sep 18, 2023 · edited Loading
Copy link Copy Markdown
Member
|
Can you please make the format similar to Python's >>> from enum import Enum >>> class Color(Enum): ... RED = 1 ... GREEN = 2 ... >>> repr(Color.GREEN) '<Color.GREEN: 2>' I suggest to use decimal values when enum is a regular enum, and either binary or hexadecimal when it has P.S. Disregard the Mac and ARM failures, they are caused by our test infra. |
Use python convention
dea66fb
fix test
a3be481
koubaa commented Sep 19, 2023
Copy link Copy Markdown
Contributor
Author
|
@lostmsu Done - but I'm not really that happy with the Flags case. |
lostmsu commented Sep 19, 2023
Copy link Copy Markdown
Member
|
@koubaa both of course. |
View reviewed changes
Mohamed Koubaa added 2 commits September 21, 2023 10:40
respect base enum type
5133cef
fix other assert
6e70b0e
koubaa commented Sep 21, 2023
Copy link Copy Markdown
Contributor
Author
|
@lostmsu done. The switch case isn't so pretty, but I couldn't think of a way to improve it. here's a minimal compiler explorer link with that logic if anyone wants to try and see if they can improve it: https://godbolt.org/z/dYG6sdnKo |
lostmsu commented Sep 21, 2023 · edited Loading
Copy link Copy Markdown
Member
|
@koubaa try using Convert.ChangeType string ConvertFlags(Enum value) {
Type primitiveType = value.GetType().GetEnumUnderlyingType();
string format = "X" + (Marshal.SizeOf(primitiveType) * 2).ToString(CultureInfo.InvariantCulture);
var primitive = (IFormattable)Convert.ChangeType(value, primitiveType);
return primitive.ToString(format, null);
}
string ConvertValue(Enum value) {
Type primitiveType = value.GetType().GetEnumUnderlyingType();
return Convert.ChangeType(value, primitiveType).ToString()!;
}
|
View reviewed changes
refactor
2274595
lostmsu
merged commit 22d07dd
into
pythonnet : master
Sep 22, 2023
koubaa
added a commit
to ansys/ansys-pythonnet
that referenced
this pull request
Sep 25, 2023
Enum repr (#8)
5030f75
mirror of upstream PR: pythonnet/pythonnet#2239Sign up for free to join this conversation on GitHub . Already have an account? Sign in to comment