Skip to content

Enhancement: Convert TypeStatus and TypeExecution to Enum #206

@FernandoCelmer

Description

@FernandoCelmer

Summary

TypeStatus and TypeExecution are plain classes with string class attributes. They lack type safety, validation, iteration, and membership testing that Python's `Enum` provides.

Current Code

# dotflow/core/types/status.py
class TypeStatus:
    NOT_STARTED = "Not started"
    IN_PROGRESS = "In progress"
    COMPLETED = "Completed"
    PAUSED = "Paused"
    RETRY = "Retry"
    FAILED = "Failed"

# dotflow/core/types/execution.py
class TypeExecution:
    SEQUENTIAL = "sequential"
    BACKGROUND = "background"
    PARALLEL = "parallel"

Problems

1. No validation — any string is accepted

task.status = "banana"       # no error, silently accepted
task.status = "completed"    # wrong case, silently accepted (should be "Completed")
task.status = ""             # empty string, silently accepted

2. No membership testing

# Can't do this:
"Completed" in TypeStatus      # TypeError: argument of type 'type' is not iterable

# Have to manually check:
if mode not in {"sequential", "sequential_group", "background", "parallel"}:
    raise ExecutionModeNotExist()

This manual check exists in Manager.__init__ and would be unnecessary with an Enum.

3. No iteration

# Can't list all statuses:
list(TypeStatus)  # TypeError

# Would need to manually maintain lists like in get_symbol():
status = {
    TypeStatus.NOT_STARTED: "⚪",
    TypeStatus.IN_PROGRESS: "🔵",
    # ... must keep in sync manually
}

4. Comparison is by string value, not identity

TypeStatus.COMPLETED == "Completed"  # True — but also:
"Completed" == TypeStatus.COMPLETED  # True

# This means typos go unnoticed:
if task.status == "completed":  # always False, no error raised
    print("done")

Proposed Fix

TypeStatus

# dotflow/core/types/status.py
from enum import StrEnum

class TypeStatus(StrEnum):
    NOT_STARTED = "Not started"
    IN_PROGRESS = "In progress"
    COMPLETED = "Completed"
    PAUSED = "Paused"
    RETRY = "Retry"
    FAILED = "Failed"

    @property
    def symbol(self) -> str:
        symbols = {
            TypeStatus.NOT_STARTED: "⚪",
            TypeStatus.IN_PROGRESS: "🔵",
            TypeStatus.COMPLETED: "✅",
            TypeStatus.PAUSED: "◼️",
            TypeStatus.RETRY: "❗",
            TypeStatus.FAILED: "❌",
        }
        return symbols[self]

TypeExecution

# dotflow/core/types/execution.py
from enum import StrEnum

class TypeExecution(StrEnum):
    SEQUENTIAL = "sequential"
    SEQUENTIAL_GROUP = "sequential_group"
    BACKGROUND = "background"
    PARALLEL = "parallel"

Why StrEnum

StrEnum (Python 3.11+) inherits from str, so TypeStatus.COMPLETED == "Completed" still works. This makes the migration backward-compatible — existing comparisons against string literals won't break.

For Python 3.10 support, use the backport from enum:

# Python 3.10 compatible:
class TypeStatus(str, Enum):
    ...

Benefits after migration

# Validation:
TypeStatus("banana")         # ValueError: 'banana' is not a valid TypeStatus

# Membership:
"Completed" in TypeStatus    # True (with StrEnum)

# Iteration:
for s in TypeStatus:
    print(s.name, s.value, s.symbol)

# Manager.__init__ simplification:
# Before:
VALID_MODES = {"sequential", "sequential_group", "background", "parallel"}
if mode not in VALID_MODES:
    raise ExecutionModeNotExist()

# After:
try:
    mode = TypeExecution(mode)
except ValueError:
    raise ExecutionModeNotExist()

Notes

  • StrEnum ensures backward compatibility with existing == "string" comparisons
  • get_symbol() classmethod can become a symbol property on each enum member
  • SEQUENTIAL_GROUP is missing from TypeExecution today — it's handled as a string "sequential_group" in Manager but not declared as a constant
  • If the project needs to support Python < 3.11, use class TypeStatus(str, Enum) instead of StrEnum

Checklist

  • Convert TypeStatus to StrEnum (or str, Enum for 3.10)
  • Convert TypeExecution to StrEnum
  • Add SEQUENTIAL_GROUP to TypeExecution
  • Replace get_symbol classmethod with a symbol property
  • Simplify Manager.__init__ mode validation using the enum
  • Update Task.status setter to accept only TypeStatus values
  • Update tests for enum behavior

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions