Skip to content

fix: code quality improvements#7

Open
TheTrustedAdvisor wants to merge 1 commit intoLars147:mainfrom
TheTrustedAdvisor:fix/code-quality
Open

fix: code quality improvements#7
TheTrustedAdvisor wants to merge 1 commit intoLars147:mainfrom
TheTrustedAdvisor:fix/code-quality

Conversation

@TheTrustedAdvisor
Copy link
Copy Markdown

Summary

  • Add format_qty() helper to safely format quantity values, preventing TypeError when qty is None, a string, or other non-numeric type — replaces 7+ raw qty == int(qty) checks spread across the file
  • Remove import os inside function in cmd_cache_clear; replace os.remove(path) with path.unlink() using the already-imported pathlib.Path
  • Remove unnecessary import json as json_module in cmd_shopping_exportjson is not shadowed (the format variable is fmt), so the top-level import can be used directly
  • Fix misleading docs in references/commands.md that claimed credentials are stored in secrets/cookidoo.env — the code never reads from that file; corrected to reflect actual behavior (interactive prompt or environment variables)
  • Lazy-load categories via get_categories() instead of running file I/O at module import time; adds _categories_cache and replaces all CATEGORIES / CATEGORY_NAMES global references

Test plan

  • Run python3 -c "import tmx_cli" — should complete without file I/O side effects (categories not loaded until first use)
  • Run tmx search -c hauptgerichtget_categories() called, returns correct category ID
  • Run tmx recipe show <id> — servings and ingredient quantities display correctly for integer and decimal values, and for recipes where quantity is None or ""
  • Run tmx cache clear — cached files deleted without error (no import os needed)
  • Run tmx shopping export --format json — JSON output produced correctly using top-level json module
  • Run tmx categories sync — categories reloaded into _categories_cache and subsequent get_categories() calls return updated data
  • Verify references/commands.md no longer references secrets/cookidoo.env

🤖 Generated with Claude Code

- Add format_qty() helper to prevent TypeError on None/string quantities
- Replace import os with pathlib.Path.unlink() in cache clear
- Remove unnecessary json module re-import in shopping export
- Fix misleading secrets/cookidoo.env reference in docs
- Lazy-load categories to avoid I/O at module import time
Copilot AI review requested due to automatic review settings April 18, 2026 07:18
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR improves the robustness and startup behavior of tmx_cli by centralizing quantity formatting, reducing redundant imports, updating documentation, and introducing a cached/lazy category loader to avoid doing category file I/O at module import time.

Changes:

  • Added format_qty() helper and replaced multiple ad-hoc qty == int(qty) formatting blocks.
  • Replaced module-level category globals with a cached get_categories() accessor and refreshed cache after categories sync.
  • Simplified file deletion (Path.unlink()), removed an unnecessary json re-import, and corrected login credential documentation.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.

File Description
tmx_cli.py Adds format_qty(), introduces cached get_categories(), and simplifies cache clear / shopping export logic.
references/commands.md Updates login credential documentation to match actual CLI behavior.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread tmx_cli.py
Comment on lines 722 to 724
if category:
cat_id = CATEGORIES.get(category.lower())
cat_id = get_categories().get(category.lower())
if cat_id:
Comment thread tmx_cli.py
search_parser.add_argument("-d", "--difficulty", choices=["easy", "medium", "advanced"], help="Schwierigkeitsgrad")
search_parser.add_argument("--tm", choices=["TM5", "TM6", "TM7"], help="Thermomix-Version")
search_parser.add_argument("-c", "--category", choices=list(CATEGORIES.keys()), help="Kategorie")
search_parser.add_argument("-c", "--category", choices=list(get_categories().keys()), help="Kategorie")
Comment thread tmx_cli.py
Comment on lines +232 to +235
_categories_cache = None


def get_categories() -> dict:
Comment thread tmx_cli.py
Comment on lines +235 to +239
def get_categories() -> dict:
"""Get categories, loading from cache/fallback on first access."""
global _categories_cache
if _categories_cache is None:
_categories_cache, _ = load_categories()
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