Skip to content

Linting improvements #54

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Jul 3, 2024
Merged

Linting improvements #54

merged 10 commits into from
Jul 3, 2024

Conversation

mvanwyk
Copy link
Contributor

@mvanwyk mvanwyk commented Jul 3, 2024

PR Type

Enhancement, Tests, Documentation


Description

  • Added module and class docstrings across multiple modules for better documentation.
  • Improved exception messages for clarity.
  • Refactored code for readability and maintainability.
  • Updated type hints for better type checking.
  • Enhanced test coverage and added docstrings to test files.
  • Added linting requirements and configuration for Ruff in pyproject.toml.

Changes walkthrough 📝

Relevant files
Enhancement
7 files
cross_shop.py
Add docstrings and refactor CrossShop class                           

pyretailscience/cross_shop.py

  • Added module and class docstrings.
  • Improved exception messages.
  • Refactored code for readability.
  • Updated type hints.
  • +22/-19 
    simulation.py
    Add docstrings and refactor Simulation module                       

    pyretailscience/data/simulation.py

  • Added module and class docstrings.
  • Improved exception messages.
  • Refactored code for readability.
  • Updated type hints.
  • +69/-66 
    range_planning.py
    Add docstrings and refactor RangePlanning module                 

    pyretailscience/range_planning.py

  • Added module and class docstrings.
  • Improved exception messages.
  • Refactored code for readability.
  • Updated type hints.
  • +44/-48 
    segmentation.py
    Add docstrings and refactor Segmentation module                   

    pyretailscience/segmentation.py

  • Added module and class docstrings.
  • Improved exception messages.
  • Refactored code for readability.
  • Updated type hints.
  • +39/-34 
    standard_graphs.py
    Add docstring and refactor Standard Graphs module               

    pyretailscience/standard_graphs.py

  • Added module docstring.
  • Improved exception messages.
  • Refactored code for readability.
  • Updated type hints.
  • +24/-26 
    graph_utils.py
    Add docstring and refactor Graph Utils module                       

    pyretailscience/style/graph_utils.py

  • Added module docstring.
  • Improved exception messages.
  • Refactored code for readability.
  • Updated type hints.
  • +21/-9   
    tailwind.py
    Add docstring and refactor Tailwind module                             

    pyretailscience/style/tailwind.py

  • Added module docstring.
  • Improved exception messages.
  • Refactored code for readability.
  • Updated type hints.
  • +47/-12 
    Documentation
    1 files
    __init__.py
    Add docstring to style module                                                       

    pyretailscience/style/init.py

    • Added module docstring.
    +1/-0     
    Tests
    3 files
    test_contracts.py
    Add docstrings and improve test coverage for contracts     

    tests/data/test_contracts.py

    • Added module and function docstrings.
    • Improved test coverage.
    +19/-8   
    test_range_planning.py
    Add docstrings and improve test coverage for range planning

    tests/test_range_planning.py

    • Added module and function docstrings.
    • Improved test coverage.
    +17/-4   
    test_standard_graphs.py
    Add docstrings and improve test coverage for standard graphs

    tests/test_standard_graphs.py

    • Added module and function docstrings.
    • Improved test coverage.
    +14/-7   
    Configuration changes
    1 files
    pyproject.toml
    Add linting configuration to pyproject.toml                           

    pyproject.toml

    • Added linting requirements and configuration for Ruff.
    +93/-0   

    💡 PR-Agent usage:
    Comment /help on the PR to get a list of all available PR-Agent tools and their descriptions

    Summary by CodeRabbit

    • Documentation

      • Added docstrings and improved documentation across multiple modules, including CrossShop, CustomerDecisionHierarchy, TransactionGenerator, Customer, Simulation, and various test files.
    • Refactor

      • Updated and refined several classes and methods across the project for better clarity, functionality, and readability.
      • Simplified return statements and adjusted method signatures in several classes.
      • Improved error messages and added type annotations.
    • Style

      • Enhanced code styling, including font adjustments in plotting methods and improved parameter handling in visualization functions.
    • Tests

      • Added descriptive docstrings and test descriptions to test functions for better understanding and readability.
      • Refactored test functions to handle dataset modifications correctly.
    • Chores

      • Adjusted various linting configurations, including setting per-file ignores, and configuring rules for tidy imports and quotes.

    Copy link

    coderabbitai bot commented Jul 3, 2024

    Walkthrough

    The recent changes across various pyretailscience modules and related unit tests introduce substantial updates to linting configurations, docstrings, error messages, methods, and overall code simplification. The updates enhance code readability, maintainability, and functionality through improved documentation, added type hints, new methods, and refined class logic. The configuration file pyproject.toml also received extensive modifications to incorporate updated linting and formatting standards.

    Changes

    File(s) Change Summary
    pyproject.toml Added py310 as target-version, configured various linting settings including per-file ignores.
    pyretailscience/cross_shop.py Added docstring, modified error message, simplified return statement, and updated font styling.
    pyretailscience/data/simulation.py Enhanced docstrings, refined methods, handled YAML errors, and adjusted transaction handling.
    pyretailscience/range_planning.py Added new method for range planning, updated existing methods and documentation.
    pyretailscience/segmentation.py Refined segmentation logic, handled special cases, improved plot method.
    pyretailscience/standard_graphs.py Updated import sources, added type hints, and improved function documentation.
    pyretailscience/style/graph_utils.py Added type hints, additional parameters, and updated function signatures and documentation.
    pyretailscience/style/tailwind.py Added Tailwind CSS palettes, type annotations, improved error messages, and code clarity.
    tests/data/test_contracts.py, tests/test_standard_graphs.py Refactored test functions with descriptive docstrings, handled dataset modifications.
    tests/test_range_planning.py Added docstrings to test methods, clarified test scenarios.
    pyretailscience/style/__init__.py Added a module-level docstring.

    Sequence Diagram(s)

    (No sequence diagrams are generated as the changes are too varied and do not align with a single functional workflow visualization.)

    Poem

    In lines of code, new tales unfold,
    With linting checks, so bright and bold.
    Docstrings added, errors refined,
    Improved logic, now well-defined.
    Celebrate this coding spree,
    As rabbits hop with glee! 🐇✨


    Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

    Share
    Tips

    Chat

    There are 3 ways to chat with CodeRabbit:

    • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
      • I pushed a fix in commit <commit_id>.
      • Generate unit testing code for this file.
      • Open a follow-up GitHub issue for this discussion.
    • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
      • @coderabbitai generate unit testing code for this file.
      • @coderabbitai modularize this function.
    • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
      • @coderabbitai generate interesting stats about this repository and render them as a table.
      • @coderabbitai show all the console.log statements in this repository.
      • @coderabbitai read src/utils.ts and generate unit testing code.
      • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
      • @coderabbitai help me debug CodeRabbit configuration file.

    Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

    CodeRabbit Commands (invoked as PR comments)

    • @coderabbitai pause to pause the reviews on a PR.
    • @coderabbitai resume to resume the paused reviews.
    • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
    • @coderabbitai full review to do a full review from scratch and review all the files again.
    • @coderabbitai summary to regenerate the summary of the PR.
    • @coderabbitai resolve resolve all the CodeRabbit review comments.
    • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
    • @coderabbitai help to get help.

    Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

    CodeRabbit Configration File (.coderabbit.yaml)

    • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
    • Please see the configuration documentation for more information.
    • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

    Documentation and Community

    • Visit our Documentation for detailed information on how to use CodeRabbit.
    • Join our Discord Community to get help, request features, and share feedback.
    • Follow us on X/Twitter for updates and announcements.

    @qodo-merge-pro qodo-merge-pro bot added documentation Improvements or additions to documentation enhancement New feature or request Tests Review effort [1-5]: 3 labels Jul 3, 2024
    Copy link
    Contributor

    qodo-merge-pro bot commented Jul 3, 2024

    PR Reviewer Guide 🔍

    ⏱️ Estimated effort to review: 3 🔵🔵🔵⚪⚪
    🧪 PR contains tests
    🔒 No security concerns identified
    ⚡ Key issues to review

    Possible Bug:
    The use of a magic number '3' for three_circles in the plot method of the CrossShop class could be replaced with a class-level constant to improve readability and maintainability.

    Refactoring Suggestion:
    The plot method of the CrossShop class and other similar methods across modules are quite lengthy and complex. Consider breaking these methods into smaller, more manageable functions.

    Code Style:
    Consistent use of single quotes for strings is recommended unless the string contains a single quote character, in which case double quotes are acceptable.

    Copy link
    Contributor

    qodo-merge-pro bot commented Jul 3, 2024

    PR Code Suggestions ✨

    CategorySuggestion                                                                                                                                    Score
    Possible bug
    Add a check to prevent division by zero in Yule's Q coefficient calculation

    To avoid potential division by zero errors in the calculation of Yule's Q coefficient, add
    a check to ensure the denominator is not zero before performing the division.

    pyretailscience/range_planning.py [151]

    -q = (a * d - b * c) / (a * d + b * c)
    +denominator = (a * d + b * c)
    +q = (a * d - b * c) / denominator if denominator != 0 else 0
     
    • Apply this suggestion
    Suggestion importance[1-10]: 9

    Why: This suggestion addresses a potential bug that could cause runtime errors, making it highly important.

    9
    Add error handling for missing keys in dictionary access

    Add error handling for KeyError to enhance robustness when accessing dictionary keys.

    pyretailscience/segmentation.py [244-245]

    -text = diagram.subset_label_artists[subset_id]
    +text = diagram.subset_label_artists.get(subset_id, None)
    +if text is None:
    +    raise KeyError(f"Subset ID {subset_id} not found in diagram subset label artists.")
     
    • Apply this suggestion
    Suggestion importance[1-10]: 9

    Why: Adding error handling for KeyError enhances the robustness of the code, preventing potential runtime errors when accessing dictionary keys. This is an important improvement for code reliability.

    9
    Add error handling for zero input to avoid infinite loops and division errors

    Add error handling for the case where num is zero to avoid infinite loops and potential
    division by zero errors in the human_format function.

    pyretailscience/style/graph_utils.py [52-54]

    +if num == 0:
    +    return f"{prefix}0"
     while abs(num) >= minimum_magnitude_difference:
         magnitude += 1
         num /= minimum_magnitude_difference
     
    • Apply this suggestion
    Suggestion importance[1-10]: 9

    Why: Adding error handling for zero input is crucial to prevent potential infinite loops and division by zero errors, which are significant issues.

    9
    Ensure boolean check is robust against non-boolean returns

    Ensure the 'validate' method returns a boolean to prevent potential runtime errors if it
    returns non-boolean values.

    pyretailscience/segmentation.py [65-67]

    -if contract.validate() is False:
    +if not contract.validate():
         msg = f"The dataframe requires the columns {required_cols} and they must be non-null and unique."
         raise ValueError(msg)
     
    • Apply this suggestion
    Suggestion importance[1-10]: 7

    Why: This suggestion ensures that the boolean check is more robust, preventing potential runtime errors if the validate method returns non-boolean values. This is a minor but useful improvement.

    7
    Enhancement
    Improve string formatting for consistency and readability

    Replace the string concatenation with f-string for consistency and improved readability.

    pyretailscience/segmentation.py [66]

    -msg = "The dataframe requires the columns " + required_cols + " and they must be non-null and unique."
    +msg = f"The dataframe requires the columns {required_cols} and they must be non-null and unique."
     
    • Apply this suggestion
    Suggestion importance[1-10]: 8

    Why: Using f-strings improves readability and consistency in the code. This is a minor enhancement but beneficial for maintaining code quality.

    8
    Simplify type conversion to improve code readability and performance

    Replace the manual check with a direct use of pandas' built-in functionality for better
    performance and readability.

    pyretailscience/segmentation.py [103-105]

    -if isinstance(group_1_idx, list):
    -    group_1_idx = pd.Series(group_1_idx)
    -if isinstance(group_2_idx, list):
    -    group_2_idx = pd.Series(group_2_idx)
    +group_1_idx = pd.Series(group_1_idx) if isinstance(group_1_idx, list) else group_1_idx
    +group_2_idx = pd.Series(group_2_idx) if isinstance(group_2_idx, list) else group_2_idx
     
    • Apply this suggestion
    Suggestion importance[1-10]: 6

    Why: The suggested change simplifies the type conversion logic, making the code more readable and potentially improving performance. This is a minor enhancement.

    6
    Use a dictionary for suffix mapping to enhance readability and maintainability

    Consider using a dictionary to map magnitudes to their suffixes instead of a list for
    better readability and maintainability in the human_format function.

    pyretailscience/style/graph_utils.py [57]

    -return f"{prefix}%.{decimals}f%s" % (num, ["", "K", "M", "B", "T", "P"][magnitude])
    +suffixes = {0: "", 1: "K", 2: "M", 3: "B", 4: "T", 5: "P"}
    +return f"{prefix}%.{decimals}f{suffixes[magnitude]}"
     
    • Apply this suggestion
    Suggestion importance[1-10]: 6

    Why: Using a dictionary for suffix mapping can enhance readability and maintainability, though the improvement is relatively minor compared to the existing list implementation.

    6
    Possible issue
    Add a check for non-empty input arrays to prevent errors in calculations

    Consider adding a check for non-empty arrays before performing calculations in
    _calculate_yules_q. This will prevent potential errors or unexpected behavior when working
    with empty arrays.

    tests/test_range_planning.py [19-43]

    -assert rp.CustomerDecisionHierarchy._calculate_yules_q(bought_product_1, bought_product_2) == expected_q
    +if bought_product_1.size > 0 and bought_product_2.size > 0:
    +    assert rp.CustomerDecisionHierarchy._calculate_yules_q(bought_product_1, bought_product_2) == expected_q
    +else:
    +    raise ValueError("Input arrays should not be empty.")
     
    • Apply this suggestion
    Suggestion importance[1-10]: 8

    Why: This suggestion addresses a potential issue where calculations might be performed on empty arrays, which could lead to unexpected behavior or errors. Adding this check improves the robustness of the tests.

    8
    Add a check to ensure the DataFrame is not empty before processing

    Ensure that the DataFrame df is not empty before proceeding with the get_indexes function
    to avoid potential errors when the DataFrame is empty.

    tests/test_standard_graphs.py [18-65]

    -output = get_indexes(
    +if not df.empty:
    +    output = get_indexes(
    +else:
    +    raise ValueError("Input DataFrame should not be empty.")
     
    • Apply this suggestion
    Suggestion importance[1-10]: 8

    Why: This suggestion helps prevent potential errors when the DataFrame is empty, ensuring that the function get_indexes only processes non-empty DataFrames. This adds a layer of validation and robustness to the tests.

    8
    Add a check to ensure the dataset is not empty before testing

    It's recommended to handle the case where the DataFrame dataset might be empty to prevent
    errors during testing.

    tests/data/test_contracts.py [56-58]

    -test_contract = contracts.ContractBase(dataset)
    +if not dataset.empty:
    +    test_contract = contracts.ContractBase(dataset)
    +else:
    +    raise ValueError("Dataset should not be empty.")
     
    • Apply this suggestion
    Suggestion importance[1-10]: 8

    Why: This suggestion addresses a potential issue where an empty dataset could cause errors during testing. Adding this check ensures that the tests are only run with valid, non-empty datasets, improving the reliability of the tests.

    8
    Best practice
    Add type annotations to improve clarity and consistency

    Add type annotations for the not_none function parameters to improve code clarity and
    consistency.

    pyretailscience/style/graph_utils.py [108]

    -def not_none(value1: any, value2: any) -> any:
    +def not_none(value1: Optional[Any], value2: Optional[Any]) -> Optional[Any]:
     
    • Apply this suggestion
    Suggestion importance[1-10]: 8

    Why: Adding type annotations improves code clarity and consistency, making it easier for developers to understand and maintain the code.

    8
    Maintainability
    Use a method to determine default labels based on orientation to enhance readability

    Replace the manual checks for orientation with a method that returns default labels based
    on the orientation. This will improve code readability and maintainability.

    pyretailscience/range_planning.py [256-258]

    -default_x_label, default_y_label = (
    -    ("Products", "Distance") if orientation in ["top", "bottom"] else ("Distance", "Products")
    -)
    +default_x_label, default_y_label = self.get_default_labels(orientation)
     
    • Apply this suggestion
    Suggestion importance[1-10]: 7

    Why: This change improves code readability and maintainability by reducing manual checks, but it is not critical.

    7
    Replace a hard-coded value with a constant for better maintainability

    Replace the hard-coded value 1000.0 in the minimum_magnitude_difference variable with a
    constant defined at the module level to improve maintainability and readability.

    pyretailscience/style/graph_utils.py [49]

    -minimum_magnitude_difference = 1000.0
    +MIN_MAGNITUDE_DIFF = 1000.0
    +minimum_magnitude_difference = MIN_MAGNITUDE_DIFF
     
    • Apply this suggestion
    Suggestion importance[1-10]: 7

    Why: This suggestion improves maintainability and readability by replacing a hard-coded value with a constant, making the code easier to update and understand.

    7
    Encapsulate graph style settings into a method for consistent usage across different plots

    To ensure that the GraphStyles class is used consistently, replace the direct use of
    GraphStyles properties with a method that encapsulates style settings. This method can be
    used to apply consistent styling across different plotting functions.

    pyretailscience/range_planning.py [262-264]

    -fontproperties=GraphStyles.POPPINS_SEMI_BOLD,
    -fontsize=GraphStyles.DEFAULT_TITLE_FONT_SIZE,
    -pad=GraphStyles.DEFAULT_TITLE_PAD + 5,
    +**self.apply_graph_style('title')
     
    • Apply this suggestion
    Suggestion importance[1-10]: 6

    Why: This suggestion improves maintainability by centralizing style settings, but it is not crucial for functionality.

    6
    Centralize setting of tick properties into a method to improve code reuse and consistency

    Instead of using a manual approach to set the tick label properties, define a method that
    applies these properties based on the orientation. This method can be reused in multiple
    places and ensures consistency.

    pyretailscience/range_planning.py [324-326]

    -for tick in ax.get_xticklabels():
    -    tick.set_fontproperties(GraphStyles.POPPINS_REG)
    -for tick in ax.get_yticklabels():
    -    tick.set_fontproperties(GraphStyles.POPPINS_REG)
    +self.set_tick_properties(ax, orientation)
     
    • Apply this suggestion
    Suggestion importance[1-10]: 6

    Why: This suggestion enhances maintainability by centralizing repetitive code, but it is not essential for functionality.

    6
    Performance
    Use list comprehension to optimize the creation of the colors list

    Optimize the creation of the colors list by using list comprehension directly instead of
    nested loops.

    pyretailscience/style/tailwind.py [370-372]

    -colors = []
    -for color_number in color_numbers:
    -    for color in color_order:
    -        colors.append(COLORS[color][color_number])
    +colors = [COLORS[color][color_number] for color_number in color_numbers for color in color_order]
     
    • Apply this suggestion
    Suggestion importance[1-10]: 7

    Why: This suggestion improves the performance and readability of the code by replacing nested loops with a list comprehension. While the improvement is minor, it makes the code cleaner and potentially faster.

    7

    Copy link
    Contributor

    qodo-merge-pro bot commented Jul 3, 2024

    CI Failure Feedback 🧐

    Action: Pre-Commit

    Failed stage: Run Pre-commit [❌]

    Failure summary:

    The action failed because the ruff linter found several issues in the code:

  • tests/test_gain_loss.py had multiple issues including missing docstrings, unnecessary assignments,
    and usage of magic values.
  • tests/test_cross_shop.py had issues such as missing trailing commas, incorrect pytest fixture style,
    and unsorted imports.
  • A total of 118 errors were found, out of which 61 were fixed and 57 remained unresolved.

  • Relevant error logs:
    1:  ##[group]Operating System
    2:  Ubuntu
    ...
    
    505:  [INFO] Once installed this environment will be reused.
    506:  [INFO] This may take a few minutes...
    507:  [INFO] Installing environment for https://github.com/pre-commit/pre-commit-hooks.
    508:  [INFO] Once installed this environment will be reused.
    509:  [INFO] This may take a few minutes...
    510:  [INFO] Installing environment for https://github.com/kynan/nbstripout.
    511:  [INFO] Once installed this environment will be reused.
    512:  [INFO] This may take a few minutes...
    513:  ruff.....................................................................Failed
    ...
    
    566:  tests/test_gain_loss.py:8:5: D103 Missing docstring in public function
    567:  tests/test_gain_loss.py:33:12: RET504 Unnecessary assignment to `df` before `return` statement
    568:  tests/test_gain_loss.py:36:5: D103 Missing docstring in public function
    569:  tests/test_gain_loss.py:40:48: PLR2004 Magic value used in comparison, consider replacing `2` with a constant variable
    570:  tests/test_gain_loss.py:70:5: D103 Missing docstring in public function
    571:  tests/test_gain_loss.py:74:48: PLR2004 Magic value used in comparison, consider replacing `2` with a constant variable
    572:  tests/test_gain_loss.py:108:5: D103 Missing docstring in public function
    573:  tests/test_gain_loss.py:123:39: PLR2004 Magic value used in comparison, consider replacing `2` with a constant variable
    574:  Fixed 61 errors:
    ...
    
    586:  - tests/test_cross_shop.py:
    587:  5 × COM812 (missing-trailing-comma)
    588:  1 × PT001 (pytest-fixture-incorrect-parentheses-style)
    589:  1 × I001 (unsorted-imports)
    590:  - tests/test_gain_loss.py:
    591:  26 × Q000 (bad-quotes-inline-string)
    592:  9 × COM812 (missing-trailing-comma)
    593:  1 × PT001 (pytest-fixture-incorrect-parentheses-style)
    594:  Found 118 errors (61 fixed, 57 remaining).
    ...
    
    596:  ruff-format..............................................................Passed
    597:  trim trailing whitespace.................................................Passed
    598:  fix end of files.........................................................Passed
    599:  fix python encoding pragma...............................................Passed
    600:  check yaml...............................................................Passed
    601:  debug statements (python)................................................Passed
    602:  pytest...................................................................Passed
    603:  nbstripout...............................................................Passed
    604:  ##[error]Process completed with exit code 1.
    

    ✨ CI feedback usage guide:

    The CI feedback tool (/checks) automatically triggers when a PR has a failed check.
    The tool analyzes the failed checks and provides several feedbacks:

    • Failed stage
    • Failed test name
    • Failure summary
    • Relevant error logs

    In addition to being automatically triggered, the tool can also be invoked manually by commenting on a PR:

    /checks "https://github.com/{repo_name}/actions/runs/{run_number}/job/{job_number}"
    

    where {repo_name} is the name of the repository, {run_number} is the run number of the failed check, and {job_number} is the job number of the failed check.

    Configuration options

    • enable_auto_checks_feedback - if set to true, the tool will automatically provide feedback when a check is failed. Default is true.
    • excluded_checks_list - a list of checks to exclude from the feedback, for example: ["check1", "check2"]. Default is an empty list.
    • enable_help_text - if set to true, the tool will provide a help message with the feedback. Default is true.
    • persistent_comment - if set to true, the tool will overwrite a previous checks comment with the new feedback. Default is true.
    • final_update_message - if persistent_comment is true and updating a previous checks message, the tool will also create a new message: "Persistent checks updated to latest commit". Default is true.

    See more information about the checks tool in the docs.

    Copy link

    @coderabbitai coderabbitai bot left a comment

    Choose a reason for hiding this comment

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

    Actionable comments posted: 1

    Outside diff range and nitpick comments (20)
    pyproject.toml (1)

    65-65: Consider removing show-fixes in production.

    The show-fixes option is useful during development but might be unnecessary in production. Evaluate if it should be kept.

    pyretailscience/style/graph_utils.py (4)

    1-1: Consider expanding the module docstring.

    The module docstring is brief. Consider expanding it to provide more context about the helper functions and their usage.


    48-49: Typo in comment.

    There is a typo in the comment: "recieve" should be "receive".

    - # The minimum difference between two numbers to recieve a different suffix
    + # The minimum difference between two numbers to receive a different suffix

    108-109: Typo in docstring.

    There is a typo in the docstring: "funciont" should be "function".

    - """Helper funciont that returns the first value that is not None.
    + """Helper function that returns the first value that is not None.

    124-124: Clarify the docstring for get_decimals.

    The docstring mentions the human_format function but does not explain the relationship clearly. Consider rephrasing for clarity.

    - """Helper function for the `human_format` function that determines the number of decimals to use for the y-axis.
    + """Determine the number of decimals to use for the y-axis based on the `human_format` function.
    tests/data/test_contracts.py (6)

    1-1: Consider expanding the module docstring.

    The module docstring is brief. Consider expanding it to provide more context about the tests and their purpose.


    23-23: Improve test description.

    The docstring for the test function can be more descriptive. Consider explaining what the function is testing in more detail.

    - """Test the expect_product_and_quantity_sign_to_be_unique_in_a_transaction function."""
    + """Test the function that ensures product and quantity sign are unique within a transaction."""

    55-55: Improve test description.

    The docstring for the test function can be more descriptive. Consider explaining what the function is testing in more detail.

    - """Test the validate function of the ContractBase class."""
    + """Test the validation function of the ContractBase class with basic and extended expectation sets."""

    77-77: Improve test description.

    The docstring for the test function can be more descriptive. Consider explaining what the function is testing in more detail.

    - """Test the build_expected_columns function."""
    + """Test the function that builds expected columns for a contract."""

    109-109: Improve test description.

    The docstring for the test function can be more descriptive. Consider explaining what the function is testing in more detail.

    - """Test the build_expected_unique_columns function."""
    + """Test the function that builds expected unique columns for a contract."""

    141-141: Improve test description.

    The docstring for the test function can be more descriptive. Consider explaining what the function is testing in more detail.

    - """Test the build_non_null_columns function."""
    + """Test the function that builds non-null columns for a contract."""
    pyretailscience/style/tailwind.py (5)

    1-1: Typo in docstring.

    There is a typo in the docstring: "Palettes" should be "palettes".

    - """Tailwind CSS color Palettes and helper functions.
    + """Tailwind CSS color palettes and helper functions.

    303-311: Typo in docstring and function name.

    There is a typo in the docstring and function name: "pallete" should be "palette".

    - """Returns a list of colors from the Tailwind color pallete of the given name.
    + """Returns a list of colors from the Tailwind color palette of the given name.
    - def get_color_list(name: str) -> list[str]:
    + def get_color_palette(name: str) -> list[str]:

    319-326: Typo in docstring and function name.

    There is a typo in the docstring and function name: "pallete" should be "palette".

    - """Returns a ListedColormap from the Tailwind color pallete of the given name.
    + """Returns a ListedColormap from the Tailwind color palette of the given name.
    - def get_listed_cmap(name: str) -> ListedColormap:
    + def get_listed_palette(name: str) -> ListedColormap:

    331-338: Typo in docstring and function name.

    There is a typo in the docstring and function name: "pallete" should be "palette".

    - """Returns a LinearSegmentedColormap from the Tailwind color pallete of the given name.
    + """Returns a LinearSegmentedColormap from the Tailwind color palette of the given name.
    - def get_linear_cmap(name: str) -> LinearSegmentedColormap:
    + def get_linear_palette(name: str) -> LinearSegmentedColormap:

    343-347: Typo in docstring.

    There is a typo in the docstring: "pallete" should be "palette".

    - """Returns a ListedColormap with all the Tailwind colors.
    + """Returns a ListedColormap with all the Tailwind colors.
    
    </blockquote></details>
    <details>
    <summary>pyretailscience/cross_shop.py (4)</summary><blockquote>
    
    `1-1`: **Consider expanding the module docstring.**
    
    The module docstring is brief. Consider expanding it to provide more context about the `CrossShop` class and its usage.
    
    ---
    
    `14-14`: **Consider improving the class docstring.**
    
    The class docstring is brief. Consider expanding it to provide more context about the `CrossShop` class and its purpose.
    
    ---
    
    `54-55`: **Improve error message clarity.**
    
    The error message can be more descriptive. Consider including the actual missing columns in the message.
    
    ```diff
    - msg = f"The dataframe requires the columns {required_cols} and they must be non-null"
    + missing_cols = [col for col in required_cols if col not in df.columns or df[col].isnull().any()]
    + msg = f"The dataframe requires the columns {required_cols}. Missing or null columns: {missing_cols}"
    

    182-182: Remove redundant type hint.

    The type hint dict[str, any] is redundant in **kwargs. Consider removing it.

    - **kwargs: dict[str, any],
    + **kwargs,
    Review details

    Configuration used: CodeRabbit UI
    Review profile: CHILL

    Commits

    Files that changed from the base of the PR and between e8456cf and 16af1ac.

    Files selected for processing (12)
    • pyproject.toml (1 hunks)
    • pyretailscience/cross_shop.py (8 hunks)
    • pyretailscience/data/simulation.py (14 hunks)
    • pyretailscience/range_planning.py (11 hunks)
    • pyretailscience/segmentation.py (10 hunks)
    • pyretailscience/standard_graphs.py (10 hunks)
    • pyretailscience/style/init.py (1 hunks)
    • pyretailscience/style/graph_utils.py (6 hunks)
    • pyretailscience/style/tailwind.py (3 hunks)
    • tests/data/test_contracts.py (5 hunks)
    • tests/test_range_planning.py (3 hunks)
    • tests/test_standard_graphs.py (5 hunks)
    Files skipped from review due to trivial changes (3)
    • pyretailscience/style/init.py
    • tests/test_range_planning.py
    • tests/test_standard_graphs.py
    Additional comments not posted (31)
    pyproject.toml (3)

    63-63: Specify the Python version for consistency.

    The target-version is set to py310. Ensure that the rest of the project and its dependencies are compatible with Python 3.10.


    67-75: Review the linting ignore rules.

    The ignore rules disable several important checks. Ensure that these rules are justified and won't lead to potential issues.


    145-145: Avoid banning all relative imports.

    The ban-relative-imports setting is set to "all". While this can improve clarity, it might be too restrictive. Consider if some relative imports are acceptable.

    pyretailscience/style/graph_utils.py (1)

    31-36: Ensure compatibility with older Python versions.

    The type hint int | None requires Python 3.10+. Ensure that the project is consistently using Python 3.10 or higher.

    tests/data/test_contracts.py (1)

    6-6: Verify the necessity of the new import.

    The ExpectationConfiguration import is added. Ensure that this is used correctly in the tests and is necessary.

    pyretailscience/cross_shop.py (1)

    131-131: Simplify the merge logic.

    The merge logic can be simplified by using the on parameter directly.

    - return cs_df.merge(kpi_df, left_index=True, right_index=True)
    + return cs_df.merge(kpi_df, on="customer_id")

    Likely invalid or redundant comment.

    pyretailscience/segmentation.py (5)

    Line range hint 22-44: LGTM! Good implementation of add_segment method.

    The method correctly adds a segment to the dataframe and includes necessary error handling.


    Line range hint 46-71: LGTM! Good implementation of __init__ method in ExistingSegmentation.

    The method correctly initializes the class and validates the dataframe using CustomContract.


    Line range hint 73-131: LGTM! Good implementation of __init__ method in HMLSegmentation.

    The method correctly initializes the class, validates the dataframe, and segments customers based on total spend.


    Line range hint 134-185: LGTM! Good implementation of __init__ method in SegTransactionStats.

    The method correctly initializes the class and calculates transaction statistics by segment.


    Line range hint 187-311: LGTM! Good implementation of plot method in SegTransactionStats.

    The method correctly plots the value_col by segment with appropriate error handling and customization options.

    pyretailscience/standard_graphs.py (3)

    Line range hint 1-114: LGTM! Good implementation of time_plot function.

    The function correctly plots the value_col over time with various customization options and proper error handling.


    Line range hint 128-161: LGTM! Good implementation of get_indexes function.

    The function correctly calculates the index of the value_col for a subset of a dataframe with proper validation and aggregation.


    Line range hint 164-309: LGTM! Good implementation of index_plot function.

    The function correctly plots the value_col over time with various customization options and proper error handling.

    pyretailscience/range_planning.py (6)

    Line range hint 1-82: LGTM! Good implementation of __init__ and _get_pairs methods in CustomerDecisionHierarchy.

    The methods correctly initialize the class, validate the dataframe, and process the dataframe to get pairs of products.


    Line range hint 85-118: LGTM! Good implementation of _get_truncated_svd_distances method in CustomerDecisionHierarchy.

    The method correctly calculates the truncated SVD distances with proper validation and normalization.


    Line range hint 121-153: LGTM! Good implementation of _calculate_yules_q method in CustomerDecisionHierarchy.

    The method correctly calculates the Yule's Q coefficient with proper validation and error handling.


    Line range hint 156-195: LGTM! Good implementation of _get_yules_q_distances method in CustomerDecisionHierarchy.

    The method correctly calculates the Yule's Q distances with proper validation and normalization.


    Line range hint 198-233: LGTM! Good implementation of _calculate_distances method in CustomerDecisionHierarchy.

    The method correctly calculates distances using the specified method with proper validation and error handling.


    Line range hint 235-326: LGTM! Good implementation of plot method in CustomerDecisionHierarchy.

    The method correctly plots the dendrogram with various customization options and proper error handling.

    pyretailscience/data/simulation.py (11)

    Line range hint 79-92: LGTM! Good implementation of _random_time function.

    The function correctly generates a random time between start_hour and end_hour with proper validation.


    Line range hint 95-122: LGTM! Good implementation of Product dataclass.

    The dataclass correctly defines product attributes with type hints for better readability.


    Line range hint 127-153: LGTM! Good implementation of __init__ method in TransactionGenerator.

    The method correctly initializes the class with various parameters.


    Line range hint 155-224: LGTM! Good implementation of generate_transaction method in TransactionGenerator.

    The method correctly generates a random transaction for a customer with proper validation and error handling.


    Line range hint 248-272: LGTM! Good implementation of __init__ method in Customer.

    The method correctly initializes the class with various parameters.


    Line range hint 274-298: LGTM! Good implementation of step method in Customer.

    The method correctly simulates a step (day) for the customer with proper validation and error handling.


    Line range hint 302-322: LGTM! Good implementation of __init__ method in Simulation.

    The method correctly initializes the class with various parameters.


    Line range hint 324-344: LGTM! Good implementation of from_config_file method in Simulation.

    The method correctly creates a Simulation from a config file with proper validation and error handling.


    Line range hint 346-367: LGTM! Good implementation of step method in Simulation.

    The method correctly simulates a step (day) for the simulation with proper validation and error handling.


    Line range hint 369-391: LGTM! Good implementation of run method in Simulation.

    The method correctly runs the simulation with proper validation and error handling.


    Line range hint 393-442: LGTM! Good implementation of _create_customer and _load_products methods in Simulation.

    The methods correctly create a customer and load products from the config file with proper validation and error handling.

    pyproject.toml Outdated
    "NPY", # Numpy
    "PERF", # Unnecessary performance costs
    "PGH", # Pygrep hooks
    "PIE", # Unnecessary code
    Copy link

    Choose a reason for hiding this comment

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

    Consider reviewing the max-args setting.

    The max-args setting is quite high at 15. Review if this can be reduced to improve function readability and maintainability.

    @mvanwyk mvanwyk merged commit 9a3a0b9 into main Jul 3, 2024
    1 check passed
    @mvanwyk mvanwyk deleted the linting_improvements branch July 3, 2024 17:28
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    documentation Improvements or additions to documentation enhancement New feature or request Review effort [1-5]: 3 Tests
    Projects
    None yet
    Development

    Successfully merging this pull request may close these issues.

    1 participant