tux icon indicating copy to clipboard operation
tux copied to clipboard

refactor: move constants to assets/data/constants.toml

Open electron271 opened this issue 4 months ago • 4 comments

Description

moves the constants that used to be in constants.py to a toml file in assets/data/

Guidelines

  • My code follows the style guidelines of this project (formatted with Ruff)

  • I have performed a self-review of my own code

  • I have commented my code, particularly in hard-to-understand areas

  • I have made corresponding changes to the documentation if needed

  • My changes generate no new warnings

  • I have tested this change

  • Any dependent changes have been merged and published in downstream modules

  • I have added all appropriate labels to this PR

  • [x] I have followed all of these guidelines.

How Has This Been Tested? (if applicable)

ran the help command and used the tests i implemented

Screenshots (if applicable)

Please add screenshots to help explain your changes.

Additional Information

fixes #950

Summary by Sourcery

Move all static constants into a TOML configuration file and adapt the Constants class to load them at runtime, updating unit tests accordingly.

Enhancements:

  • Relocate static constants into assets/data/constants.toml and load them with tomllib
  • Refactor the Constants class to initialize its attributes from the TOML data

Tests:

  • Update unit tests to use the CONST instance and verify TOML-loaded constants across multiple categories
  • Add tests for loading embed colors, embed icons, and overall configuration from the TOML file

electron271 avatar Aug 03 '25 03:08 electron271

Reviewer's Guide

This PR refactors all hardcoded constants into a TOML configuration, implements a loader to dynamically assign them in the Constants class, and updates tests to reference the new CONST instance with added coverage for loaded values.

Class diagram for refactored Constants loading from TOML

classDiagram
    class Constants {
        +EMBED_COLORS: dict[str, int]
        +EMBED_ICONS: dict[str, str]
        +EMBED_MAX_NAME_LENGTH: int
        +EMBED_MAX_DESC_LENGTH: int
        +EMBED_MAX_FIELDS: int
        +EMBED_TOTAL_MAX: int
        +EMBED_FIELD_VALUE_LENGTH: int
        +NICKNAME_MAX_LENGTH: int
        +CONTEXT_MENU_NAME_LENGTH: int
        +SLASH_CMD_NAME_LENGTH: int
        +SLASH_CMD_MAX_DESC_LENGTH: int
        +SLASH_CMD_MAX_OPTIONS: int
        +SLASH_OPTION_NAME_LENGTH: int
        +ACTION_ROW_MAX_ITEMS: int
        +SELECTS_MAX_OPTIONS: int
        +SELECT_MAX_NAME_LENGTH: int
        +DEFAULT_REASON: str
        +DEFAULT_DELETE_AFTER: int
        +SNIPPET_MAX_NAME_LENGTH: int
        +SNIPPET_ALLOWED_CHARS_REGEX: str
        +SNIPPET_PAGINATION_LIMIT: int
        +AFK_PREFIX: str
        +AFK_TRUNCATION_SUFFIX: str
        +EIGHT_BALL_QUESTION_LENGTH_LIMIT: int
        +EIGHT_BALL_RESPONSE_WRAP_WIDTH: int
        +ADD_BOOKMARK: str
        +REMOVE_BOOKMARK: str
        +__init__()
    }
    class _load_constants {
        +_load_constants() dict[str, Any]
    }
    Constants --|> _load_constants : uses

File-Level Changes

Change Details Files
Extract constants into a TOML configuration and implement dynamic loading
  • Add assets/data/constants.toml with structured constant sections
  • Implement _load_constants using tomllib and workspace_root
  • Refactor Constants.init to assign attributes from loaded TOML data
  • Remove all static constant definitions from the class body
tux/utils/constants.py
assets/data/constants.toml
Update and extend unit tests to use the dynamic CONST instance
  • Change assertions from Constants.<ATTR> to CONST.<ATTR>
  • Add tests for embed colors, embed icons, and file-loading behavior
  • Introduce separate test methods for discord_limits, interaction_limits, default values, and bookmark constants
tests/unit/tux/utils/test_constants.py

Assessment against linked issues

Issue Objective Addressed Explanation
#950 Move the constants defined in tux/utils/constants.py to the assets/data/ directory.

Possibly linked issues

  • #950: The PR moves constants from constants.py to assets/data/constants.toml, directly addressing issue #950.

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an issue from a review comment by replying to it. You can also reply to a review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull request title to generate a title at any time. You can also comment @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in the pull request body to generate a PR summary at any time exactly where you want it. You can also comment @sourcery-ai summary on the pull request to (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the pull request to resolve all Sourcery comments. Useful if you've already addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull request to dismiss all existing Sourcery reviews. Especially useful if you want to start fresh with a new review - don't forget to comment @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

  • Contact our support team for questions or feedback.
  • Visit our documentation for detailed guides and information.
  • Keep in touch with the Sourcery team by following us on X/Twitter, LinkedIn or GitHub.

sourcery-ai[bot] avatar Aug 03 '25 03:08 sourcery-ai[bot]

Dependency Review

✅ No vulnerabilities or license issues or OpenSSF Scorecard issues found.

Scanned Files

None

github-actions[bot] avatar Aug 03 '25 03:08 github-actions[bot]

Deploying tux with  Cloudflare Pages  Cloudflare Pages

Latest commit: 46870ec
Status: ✅  Deploy successful!
Preview URL: https://e7a04753.tux-afh.pages.dev
Branch Preview URL: https://assets-data-constants.tux-afh.pages.dev

View logs

Codecov Report

:white_check_mark: All modified and coverable lines are covered by tests. :white_check_mark: Project coverage is 9.58%. Comparing base (b6a904b) to head (46870ec). :warning: Report is 4 commits behind head on main. :white_check_mark: All tests successful. No failed tests found.

Additional details and impacted files
@@           Coverage Diff            @@
##            main    #997      +/-   ##
========================================
+ Coverage   9.44%   9.58%   +0.13%     
========================================
  Files        123     123              
  Lines      10406   10422      +16     
  Branches    1277    1277              
========================================
+ Hits         983     999      +16     
  Misses      9330    9330              
  Partials      93      93              
Flag Coverage Δ *Carryforward flag
database 0.03% <ø> (-0.28%) :arrow_down: Carriedforward from b6a904b
integration 5.99% <100.00%> (+0.14%) :arrow_up:
unit 6.44% <100.00%> (+0.14%) :arrow_up:

*This pull request uses carry forward flags. Click here to find out more.

Components Coverage Δ
Core Bot Infrastructure 16.43% <ø> (ø)
Database Layer 0.00% <ø> (ø)
Bot Commands & Features 0.00% <ø> (ø)
Event & Error Handling ∅ <ø> (∅)
Utilities & Helpers ∅ <ø> (∅)
User Interface Components 0.00% <ø> (ø)
CLI Interface ∅ <ø> (∅)
External Service Wrappers ∅ <ø> (∅)

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

codecov[bot] avatar Aug 03 '25 03:08 codecov[bot]