Skip to content

Replace magic strings with StrEnum types for command flags#86

Merged
vm-mishchenko merged 2 commits into
mainfrom
260514-spec-flag-enums
May 15, 2026
Merged

Replace magic strings with StrEnum types for command flags#86
vm-mishchenko merged 2 commits into
mainfrom
260514-spec-flag-enums

Conversation

@vm-mishchenko
Copy link
Copy Markdown
Owner

Summary

This PR introduces type-safe enums for three command flags: branch_source, pr_mode, and worktree_mode. A new src/draft/types.py module defines BranchSource, PrMode, and WorktreeMode as StrEnum types. All command files are updated to use enum members instead of hardcoded string literals.

The change preserves backward compatibility—enum values match current strings exactly, so existing persisted state.json files continue to work without migration.

Test plan

  • Verify JSON serialization: python -c "import json; from draft.types import BranchSource; print(json.dumps({'x': BranchSource.NEW}))" outputs {"x": "new"}
  • Verify enum equality: BranchSource.NEW == "new" returns True; "existing" in (BranchSource.NEW, BranchSource.EXISTING) returns True
  • Load existing state.json from before this change and verify RunContext.load followed by comparisons against enum members work correctly
  • Run draft create on a new branch and verify state.json persists as "new", "open", "worktree" (raw strings)
  • Run draft create --branch <existing> and verify correct string values in state.json
  • Run draft babysit, draft fix-pr, draft continue on existing runs and verify same expected steps as before
  • Verify no bare-string magic values remain in code (except protected literals: gh pr list --state open argument and git worktree subcommands)

Define BranchSource, PrMode, and WorktreeMode as StrEnum in a new
types module. Replace hardcoded string literals throughout command
files with enum members. StrEnum values match the current strings,
preserving JSON serialization and backward compatibility with
existing persisted state.
Add clarifying comments to each flag enum value to explain their
purpose and behavior. This makes the command flag types
self-documenting and easier to understand without needing external
references.
@vm-mishchenko vm-mishchenko marked this pull request as ready for review May 15, 2026 00:34
@vm-mishchenko vm-mishchenko merged commit 59c4e06 into main May 15, 2026
1 check passed
@vm-mishchenko vm-mishchenko deleted the 260514-spec-flag-enums branch May 15, 2026 01:06
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