Open
Conversation
This commit refactors the Snowflake ID generation classes based on your feedback to improve code structure and type safety.
Key changes:
1. **Unified `SnowflakeGenerator` Class:**
* I merged the previous `SnowflakeIDGenerator` (async) and `SyncSnowflakeIDGenerator` into a single class named `SnowflakeGenerator`.
* This class now handles both asynchronous ID generation via the `generate()` method (using `asyncio.Lock`) and synchronous ID generation via the `generate_sync()` method (using `threading.Lock`).
* Internal state like `last_timestamp` and `sequence` is shared within an instance, protected by the respective locks during the critical section of ID generation.
2. **`SnowflakeInfo` Dataclass for Extraction Results:**
* I introduced a new `SnowflakeInfo` dataclass (`@dataclass(frozen=True)`).
* The `extract_snowflake_info` method in `SnowflakeGenerator` now returns an instance of `SnowflakeInfo` instead of a dictionary. This provides better type safety and a more explicit API contract for the extracted ID components (timestamp_ms, readable_timestamp, node_id, sequence).
3. **Test Suite Updates:**
* I updated all test files (`test_32_bits.py`, `test_48_bits.py`, `test_64_bits.py`, `test_96_bits.py`) to:
* Use the unified `SnowflakeGenerator` class.
* Call `generator.generate_sync()` for synchronous test cases.
* Expect and assert against the `SnowflakeInfo` dataclass attributes when testing `extract_snowflake_info`.
4. **Documentation and Cleanup:**
* I updated docstrings and comments throughout `snowflakeid/generator.py` and the test suite to reflect these structural changes.
* I performed general code cleanup to ensure consistency and remove outdated elements.
These changes address your feedback requesting a more consolidated class structure and a more type-safe return type for ID component extraction, leading to a cleaner and more maintainable codebase.
…ew class and constants
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixed failing unit tests.
Verified all tests now pass locally using pytest / unittest (or relevant test runner).