Skip to content

Add utility functions for enum value handling and platform normalization#847

Open
tenkus47 wants to merge 1 commit into
developfrom
update-series_update
Open

Add utility functions for enum value handling and platform normalization#847
tenkus47 wants to merge 1 commit into
developfrom
update-series_update

Conversation

@tenkus47

@tenkus47 tenkus47 commented Jul 3, 2026

Copy link
Copy Markdown
Member
  • Introduced _enum_value to convert enum and string representations to their corresponding string values.
  • Added _normalize_platform to standardize platform values to lowercase.
  • Updated get_users_with_matching_timeblocks to utilize these new functions for improved data consistency.
  • Created unit tests for both utility functions to ensure correct behavior across various input types.

- Introduced `_enum_value` to convert enum and string representations to their corresponding string values.
- Added `_normalize_platform` to standardize platform values to lowercase.
- Updated `get_users_with_matching_timeblocks` to utilize these new functions for improved data consistency.
- Created unit tests for both utility functions to ensure correct behavior across various input types.
@tenkus47 tenkus47 requested a review from Tech-lo July 3, 2026 13:51
@greptile-apps

greptile-apps Bot commented Jul 3, 2026

Copy link
Copy Markdown

Confidence Score: 4/5

Safe to merge for the current enum values in use; the helper functions behave correctly for SessionType and PushPlatform members.

The refactoring works correctly for all known inputs. The dot-splitting fallback is the sharpest edge — any future string platform value that contains a dot would be silently truncated — but this is speculative given the current enum definitions. The None"" behavior change and the untested None branch are worth noting but do not affect current runtime paths.

The _enum_value dot-splitting logic in routine_notification_repository.py deserves a second look if the platform or session-type value space ever expands beyond simple uppercase identifiers.

Reviews (1): Last reviewed commit: "Add utility functions for enum value han..." | Re-trigger Greptile

Comment on lines +24 to +25
if hasattr(value, "value"):
return str(value.value)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Overly broad hasattr check may match non-enum objects

hasattr(value, "value") will return True for any object that has a value attribute — not just enum.Enum instances. If a SQLAlchemy row proxy, a Pydantic model, or a named tuple with a value field is accidentally passed in, its .value will be silently consumed without a type error. Using isinstance(value, enum.Enum) makes the intent explicit and avoids false positives.

Comment on lines +26 to +28
raw = str(value)
if "." in raw:
return raw.rsplit(".", 1)[-1]

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Dot-splitting applies to any string, not just enum repr strings

The "." in raw branch is designed to handle SQLAlchemy returning enum members as their repr-like string (e.g. "SessionType.PLAN"), but it applies to any string that happens to contain a dot. A future platform value like "fcm.android" or a URL-like string would be silently truncated to just the last segment. Consider narrowing the check (e.g. only strip when the prefix matches a known class name) or document the assumed input range clearly.

Comment on lines +1 to +34
from pecha_api.push_devices.push_device_enums import PushPlatform
from pecha_api.routines.routine_notifications.routine_notification_repository import (
_enum_value,
_normalize_platform,
)
from pecha_api.routines.routines_enums import SessionType


def test_enum_value_from_session_type_enum():
assert _enum_value(SessionType.SERIES) == "SERIES"
assert _enum_value(SessionType.PLAN) == "PLAN"


def test_enum_value_from_stringified_enum_name():
assert _enum_value("SessionType.SERIES") == "SERIES"
assert _enum_value("SessionType.PLAN") == "PLAN"


def test_enum_value_from_plain_string():
assert _enum_value("SERIES") == "SERIES"


def test_normalize_platform_from_push_platform_enum():
assert _normalize_platform(PushPlatform.ANDROID) == "android"
assert _normalize_platform(PushPlatform.IOS) == "ios"


def test_normalize_platform_from_stringified_enum_name():
assert _normalize_platform("PushPlatform.ANDROID") == "android"
assert _normalize_platform("pushplatform.android") == "android"


def test_normalize_platform_from_plain_string():
assert _normalize_platform("ANDROID") == "android"

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 None input — an explicitly handled case — has no test coverage

_enum_value has an explicit early-return for None (returning ""), and _normalize_platform(None) therefore also returns "". This is a deliberate behavior change from the previous str(None).lower()"none" path, but no test asserts on it. Without a test, the None branch could be silently altered in a future refactor without any signal.

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

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