Skip to content

Commit

Permalink
Refactor BlockLengthSampler for Enhanced Type Safety, Validation, a…
Browse files Browse the repository at this point in the history
…nd Extensibility (#175)

## 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

- [x] Bug fix (non-breaking change which fixes an issue)
- [x] 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:

- [x] My code follows the style guidelines of this project
- [x] I have performed a self-review of my own code
- [x] I have commented my code, particularly in hard-to-understand areas
- [x] I have made corresponding changes to the documentation
- [x] My changes generate no new warnings
- [x] 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.):

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

---------

Co-authored-by: Sankalp Gilda <[email protected]>
  • Loading branch information
astrogilda and Sankalp Gilda authored Oct 12, 2024
1 parent 81ec78e commit c229715
Show file tree
Hide file tree
Showing 3 changed files with 448 additions and 122 deletions.
Loading

0 comments on commit c229715

Please sign in to comment.