fix: correct FeatureUsageFlag to use IntFlag and return str from __str__#1036
fix: correct FeatureUsageFlag to use IntFlag and return str from __str__#1036proazr wants to merge 3 commits into
Conversation
The FeatureUsageFlag enum has two issues: 1. The __str__ method returns self.value (an int) instead of a string, violating Python's data model requirement that __str__ must return str. This causes TypeError on all Python versions when calling str() on enum members. 2. The class inherits from (int, Enum) instead of IntFlag, even though the values are clearly bit flags (powers of 2). This prevents proper bitwise operations - combining flags with | returns plain int instead of FeatureUsageFlag. Changes Made: - Changed FeatureUsageFlag base class from (int, Enum) to IntFlag - Fixed __str__ to return str(self.value) instead of self.value - Added test suite for all enum classes
|
|
|
There was a problem hiding this comment.
Pull request overview
This PR fixes msgraph_core._enums.FeatureUsageFlag so it behaves like a proper bit-flag enum and no longer raises a runtime TypeError when converted to a string, aligning it with Python’s data model expectations and enabling correct bitwise semantics across the core middleware feature-usage tracking.
Changes:
- Switch
FeatureUsageFlagfrom(int, Enum)toIntFlagto preserve enum semantics during bitwise operations. - Fix
FeatureUsageFlag.__str__to return astr(stringified numeric value) instead of anint. - Add a dedicated
tests/test_enums.pysuite validating__str__behavior andIntFlagbitwise operations.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
src/msgraph_core/_enums.py |
Updates FeatureUsageFlag to inherit from IntFlag and ensures __str__ returns a string. |
tests/test_enums.py |
Adds unit tests covering __str__ return type/value and bitwise behavior for FeatureUsageFlag, plus sanity checks for other enums. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.



Overview
The
FeatureUsageFlagenum has two issues that cause runtime errors and prevent proper bitwise operations:__str__returns int instead of str: The__str__method returnsself.value(an int), violating Python's data model requirement that__str__must return a string. This causesTypeError: __str__ returned non-string (type int)on all Python versions when callingstr()on enum members.Incorrect base class for bit flags: The class inherits from
(int, Enum)instead ofIntFlag, even though the values are clearly bit flags (powers of 2: 0, 1, 2, 4, 8, 16). This prevents proper bitwise operations - combining flags with|returns plainintinstead ofFeatureUsageFlag.Demo
Before (broken):
After (fixed):
Notes
IntFlagis a subclass of bothintandEnum, so existingisinstance()checks continue to workFeatureUsageFlagwas affected;APIVersionandNationalCloudsalready work correctly as they inherit fromstrTesting Instructions
pytest tests/test_enums.py -v__str__returns string type for all enum classesFeatureUsageFlagis anIntFlagsubclassFeatureUsageFlag__str__