Skip to content
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

Refactor BlockLengthSampler for Enhanced Type Safety, Validation, and Extensibility #175

Merged
merged 5 commits into from
Oct 12, 2024

Conversation

astrogilda
Copy link
Owner

Description

This pull request addresses and resolves multiple issues within the BlockLengthSampler class to enhance its reliability, maintainability, and usability. The key changes include:

  1. Enhanced Type Safety and Validation:

    • Transitioned from manual property setters and getters to Pydantic's BaseModel for automatic data validation.
    • Defined block_length_distribution to accept both str and DistributionTypes enums, ensuring type consistency through validators.
  2. Centralized Distribution Management:

    • Introduced the DistributionTypes enum to strictly define supported distribution types.
    • Implemented a DistributionRegistry class to manage and retrieve distribution sampling functions, promoting scalability and modularity.
  3. Replacement of Lambdas with Named Functions:

    • Defined distribution sampling functions as named, standalone functions instead of lambdas to improve readability and maintainability.
  4. Comprehensive Logging and Error Handling:

    • Integrated Python's logging module to provide detailed logs for debugging and monitoring.
    • Utilized warnings.warn to notify users about non-critical issues, such as adjusting avg_block_length.
  5. Simplified Class Structure:

    • Removed unnecessary mixin classes, encapsulating all functionality within the BlockLengthSampler class for clarity and simplicity.
  6. Improved Extensibility:

    • Facilitated easy addition of new distributions by registering them with the DistributionRegistry without altering the core sampler class.
  7. Resolved Type Errors:

    • Defined RngTypes within the same module using TypeAlias to ensure recognition by type checkers like Pylance.
    • Adjusted the typing and validators for block_length_distribution to eliminate type mismatch errors.

These changes collectively improve the BlockLengthSampler class's robustness, type safety, and developer experience.

Type of change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)

How Has This Been Tested?

  • Type Checking:

    • Verified that VSCode's Pylance no longer flags RngTypes as unknown.
    • Confirmed that passing both str and DistributionTypes to block_length_distribution no longer causes type mismatch errors.
  • Functionality Testing:

    • Ensured that sample_block_length works correctly with various distributions and returns valid block lengths.
    • Tested with different Python versions to confirm compatibility.
  • Logging Verification:

    • Checked that relevant log messages are produced during distribution registration and sampling processes.

Checklist:

  • My code follows the style guidelines of this project
  • 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
  • My changes generate no new warnings
  • Any dependent changes have been merged and published in downstream modules

Additional Information (if applicable)

No additional information.

Add All Contributors Command

Remember to acknowledge your contributions, replace contribution_type with your contribution (code, doc, etc.):

@all-contributors please add @<your-username> for code

@astrogilda astrogilda added enhancement New feature or request good first issue Good for newcomers maintenance labels Oct 12, 2024
@astrogilda astrogilda self-assigned this Oct 12, 2024
@astrogilda astrogilda merged commit c229715 into main Oct 12, 2024
28 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers maintenance
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Refactor BlockLengthSampler for Enhanced Type Safety, Validation, and Extensibility
1 participant