Skip to content

Return snake-cased member name from str() on enum values#135

Merged
jhonabreul merged 4 commits into
QuantConnect:masterfrom
jhonabreul:bug-enum-str-snake-case
Jul 10, 2026
Merged

Return snake-cased member name from str() on enum values#135
jhonabreul merged 4 commits into
QuantConnect:masterfrom
jhonabreul:bug-enum-str-snake-case

Conversation

@jhonabreul

Copy link
Copy Markdown
Collaborator

Description

C# enum members are exposed to Python with upper-cased snake case names (e.g. FileAccess.READ_WRITE), but converting an enum value to a string returned the C# member name from Enum.ToString() (e.g. "ReadWrite"), so the string representation did not round-trip with how the member is accessed from Python.

EnumObject now overrides the tp_str slot so that str() (and f-string formatting) return the same Python-facing snake-cased name used to access the member:

str(DayOfWeek.SATURDAY)                  # "SATURDAY" (was "Saturday")
str(FileShare.READ_WRITE)                # "READ_WRITE" (was "ReadWrite")
str(FileShare.READ | FileShare.DELETE)   # "READ, DELETE" (was "Read, Delete")
str(Enum.ToObject(DayOfWeek, 10))        # "10" (unchanged: no defined member)

String equality comparison also accepts the snake-cased name now, consistently with str(), while still matching the C# member name:

FileShare.READ == "Read"   # True (unchanged)
FileShare.READ == "READ"   # True (was False)

Note this is a behavior change for code that logs, serializes or compares str(enum_value) expecting the C# member name.

Includes the version bump to 2.0.61.

Enum members are exposed to Python with upper-cased snake case names
(e.g. FileAccess.READ_WRITE), but str() on an enum value returned the
C# member name from Enum.ToString() (e.g. ReadWrite). str() and
f-string formatting now return the same Python-facing snake-cased name
used to access the member. Flags combinations keep the comma-separated
format with each name snake-cased, and values without a defined member
keep the raw numeric representation.

String equality comparison now accepts the snake-cased name as well,
consistently with str(), while still matching the C# member name.
@jhonabreul jhonabreul marked this pull request as ready for review July 10, 2026 16:51
@jhonabreul jhonabreul merged commit 687791d into QuantConnect:master Jul 10, 2026
8 checks passed
@jhonabreul jhonabreul deleted the bug-enum-str-snake-case branch July 10, 2026 22:05
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

Successfully merging this pull request may close these issues.

2 participants