Skip to content

Conversation

@proazr
Copy link

@proazr proazr commented Dec 30, 2025

Overview

The FeatureUsageFlag enum has two issues that cause runtime errors and prevent proper bitwise operations:

  1. __str__ returns int instead of str: The __str__ method returns self.value (an int), violating Python's data model requirement that __str__ must return a string. This causes TypeError: __str__ returned non-string (type int) on all Python versions when calling str() on enum members.

  2. Incorrect base class for bit flags: The class inherits from (int, Enum) instead of IntFlag, 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 plain int instead of FeatureUsageFlag.

Demo

Before (broken):

>>> from msgraph_core._enums import FeatureUsageFlag
>>> str(FeatureUsageFlag.NONE)
TypeError: __str__ returned non-string (type int)

>>> type(FeatureUsageFlag.REDIRECT_HANDLER_ENABLED | FeatureUsageFlag.RETRY_HANDLER_ENABLED)
<class 'int'>  # Lost enum semantics

After (fixed):

>>> from msgraph_core._enums import FeatureUsageFlag
>>> str(FeatureUsageFlag.NONE)
'0'

>>> combined = FeatureUsageFlag.REDIRECT_HANDLER_ENABLED | FeatureUsageFlag.RETRY_HANDLER_ENABLED
>>> type(combined)
<enum 'FeatureUsageFlag'>  # Proper IntFlag behavior
>>> combined.value
3

Notes

  • This bug affects all supported Python versions (3.9, 3.10, 3.11, 3.12, 3.13)
  • The fix is backward-compatible: IntFlag is a subclass of both int and Enum, so existing isinstance() checks continue to work
  • Only FeatureUsageFlag was affected; APIVersion and NationalClouds already work correctly as they inherit from str

Testing Instructions

  • Run the new enum tests: pytest tests/test_enums.py -v
  • All 15 tests should pass, covering:
    • __str__ returns string type for all enum classes
    • FeatureUsageFlag is an IntFlag subclass
    • Bitwise operations (OR, AND, XOR, invert) return FeatureUsageFlag
    • Combined flags also return string from __str__
    • Flag values are correct powers of 2

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
@proazr proazr requested a review from a team as a code owner December 30, 2025 09:38
@sonarqubecloud
Copy link

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.

1 participant