Skip to content

Simplify get_database_url default handling with or #645

Description

@nanotaboada

Problem

databases/player_database.py::get_database_url:

database_url = os.getenv("DATABASE_URL")
if not database_url:
    storage_path = os.getenv("STORAGE_PATH", "./players-sqlite3.db")
    database_url = f"sqlite+aiosqlite:///{storage_path}"
return database_url

The "declare, then reassign inside an if" shape is a common C#-ism. Python
expresses "use this, otherwise the fallback" with or.

Proposed Solution

def get_database_url() -> str:
    """Return the async database URL from environment variables."""
    storage_path = os.getenv("STORAGE_PATH", "./players-sqlite3.db")
    return os.getenv("DATABASE_URL") or f"sqlite+aiosqlite:///{storage_path}"

Semantic nuance worth understanding (and maybe a comment): or treats an
empty-string DATABASE_URL as unset — same as the current if not database_url. If "set but empty" should be distinct from "unset", the test is
os.getenv("DATABASE_URL") is None instead.

Suggested Approach

  1. Collapse the body to the or expression above.
  2. Keep the module-level DATABASE_URL / _connect_args logic downstream
    unchanged.
  3. Run uv run pytest.

Acceptance Criteria

  • get_database_url returns a single or expression
  • Empty / unset DATABASE_URL still falls back to SQLite
  • STORAGE_PATH override still honoured
  • All tests pass
  • CHANGELOG.md updated

References

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or requestpriority:lowNice-to-have improvement. Can be deferred without blocking other work.pythonPull requests that update Python codepython:idiomsRefactors toward idiomatic Python (PEP conventions, stdlib idioms)

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions