Skip to content

Comments

Start cleaning things up#28

Merged
theodore-s-beers merged 7 commits intomainfrom
tsb
May 7, 2025
Merged

Start cleaning things up#28
theodore-s-beers merged 7 commits intomainfrom
tsb

Conversation

@theodore-s-beers
Copy link
Contributor

@theodore-s-beers theodore-s-beers commented May 6, 2025

Summary by Sourcery

Clean up and improve type hints, error handling, and code quality across the DataFed TorchFlow library

Bug Fixes:

  • Fixed potential None-related errors in various methods
  • Added missing initializations
  • Corrected some type-related edge cases

Enhancements:

  • Added type hints to method signatures
  • Improved error handling with explicit None checks
  • Refactored type checking using isinstance()
  • Simplified some conditional logic

Chores:

  • Updated dependencies in setup.cfg
  • Added development tools like pyright and ruff

@sourcery-ai
Copy link

sourcery-ai bot commented May 6, 2025

Reviewer's Guide

This pull request implements enhancements by adding type hints across modules, introducing validation checks for crucial parameters, and refining code style. Project dependencies have also been updated.

Updated Class Diagram for PyTorchModelLogger

classDiagram
    class PyTorchModelLogger {
        +__init__(self, ..., input_data_shape: Optional_tuple_int_Ellipsis, ...)
        +getMetadata(self, local_vars: Optional_list_tuple_str_Any, model_hyperparameters: Optional_dict_str_Any, **kwargs)
        +save_notebook(self)
    }
    note for PyTorchModelLogger "Type hints updated for parameters in __init__ and getMetadata. Added input validation for some parameters."
    note for PyTorchModelLogger.getMetadata "Raises ValueError if local_vars or model_hyperparameters is None."
    note for PyTorchModelLogger.save_notebook "Raises ValueError if notebook metadata cannot be fetched."
Loading

Updated Class Diagram for DataFed

classDiagram
    class API {
        %% Base class, details not shown as not modified in this PR
    }
    class DataFed {
        +__init__(self, ..., datafed_path: str, ..., dataset_id_or_path: Optional_str, ...)
        +user_id(self) : str
        +check_string_for_dot_or_slash(self, s)
        +data_record_create(self, ..., record_title: Optional_str, ...)
        +data_record_update(self, ..., record_title: Optional_str, ...)
        +required_keys(self, dict_list, required_keys)
        +getData(self, dataset_id: Optional_str)
    }
    DataFed --|> API
    note for DataFed "Type hints added/updated for several methods. Methods 'check_string_for_dot_or_slash' and 'required_keys' changed from static to instance methods. Input validation added."
    note for DataFed.user_id "Property; raises RuntimeError if authenticated user info cannot be fetched."
    note for DataFed.data_record_create "Raises ValueError if record_title is None."
    note for DataFed.data_record_update "Raises ValueError if record_title is None."
    note for DataFed.getData "Raises ValueError if dataset_id_or_path is not set and dataset_id is None."
Loading

Updated Class Diagram for UniversalEncoder

classDiagram
    class JSONEncoder {
        %% Base class from json module, details not shown
    }
    class UniversalEncoder {
        +default(self, o)
    }
    UniversalEncoder --|> JSONEncoder
    note for UniversalEncoder "Parameter 'obj' in 'default' method renamed to 'o'."
Loading

File-Level Changes

Change Details Files
Added type annotations and input validation checks.
  • Applied type hints to function signatures, return types, and attributes.
  • Implemented runtime checks for None values in critical parameters, raising ValueError or RuntimeError.
src/datafed_torchflow/pytorch.py
src/datafed_torchflow/datafed.py
Improved code style, Pythonic idioms, and addressed minor logical issues.
  • Replaced type() in [] constructs with isinstance() for type checking.
  • Modified dictionary update operations for broader Python version compatibility.
  • Standardized f-string formatting for floating-point numbers in example notebooks.
  • Ensured variables are initialized before use to prevent potential errors.
  • Refactored a static method to an instance method where appropriate.
  • Minor cosmetic changes such as parameter renaming and spacing adjustments.
src/datafed_torchflow/pytorch.py
src/datafed_torchflow/datafed.py
src/datafed_torchflow/JSON.py
examples/PyTorchModelLogger.ipynb
examples/PyTorchModelLogger.old.ipynb
src/datafed_torchflow/computer.py
Updated project dependencies and development tooling.
  • Added pyright, ruff, and sphinx to the list of dependencies.
  • Reordered items within the install_requires section.
setup.cfg

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

Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey @theodore-s-beers - I've reviewed your changes - here's some feedback:

  • The 'Bandaid fix' comment regarding self.data_path in DataFed.getData should be addressed with a more permanent solution.
  • Consider providing a more descriptive PR title and a brief summary in the description to outline the main changes, as 'Start cleaning things up' is quite general.
  • The required_keys method in DataFed no longer references self after the @staticmethod decorator was removed; evaluate if it should be a static method or a standalone utility function.
Here's what I looked at during the review
  • 🟡 General issues: 3 issues found
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@theodore-s-beers
Copy link
Contributor Author

Going to merge this. There may be details to revert later, given some of the guards I've added, but I think it's better to start by imposing constraints.

@theodore-s-beers theodore-s-beers merged commit db95463 into main May 7, 2025
4 checks passed
@theodore-s-beers theodore-s-beers deleted the tsb branch May 7, 2025 20:20
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.

1 participant