Skip to content

refactor: Unify generators and return dataclass for extracted info#2

Open
Iamsdt wants to merge 1 commit intomainfrom
refactor-unified-generator
Open

refactor: Unify generators and return dataclass for extracted info#2
Iamsdt wants to merge 1 commit intomainfrom
refactor-unified-generator

Conversation

@Iamsdt
Copy link
Contributor

@Iamsdt Iamsdt commented May 23, 2025

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.

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.
@Iamsdt Iamsdt requested a review from Copilot May 23, 2025 09:25
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

A refactor to unify asynchronous and synchronous Snowflake ID generators under SnowflakeGenerator, introduce a SnowflakeInfo dataclass for extraction results, and update the test suite accordingly.

  • Combined async and sync generators into one class with respective locks.
  • Replaced dictionary returns with a frozen SnowflakeInfo dataclass.
  • Updated all tests to use SnowflakeGenerator, generate_sync(), and assert on SnowflakeInfo attributes.

Reviewed Changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 3 comments.

File Description
tests/test_32_bits.py Updated to use SnowflakeGenerator, generate_sync(), SnowflakeInfo, and consistent type hints.
tests/test_48_bits.py Added comprehensive 48-bit tests and updated imports, helpers, and assertions.
tests/test_64_bits.py Refactored 64-bit tests for unified class, added Base62 checks, and removed redundant collision tests.
tests/test_96_bits.py Introduced new 96-bit async/sync tests, info extraction, and config validation.
Comments suppressed due to low confidence (1)

tests/test_48_bits.py:235

  • Non-Python planning text was accidentally appended to the test file and will break the test suite. Remove or comment out this block.
Okay, `tests/test_48_bits.py` has been created with a comprehensive set of tests, including standard async/sync tests, tests for edge node/worker IDs, and tests for configurations with minimal and maximal sequence bits. This provides good coverage for the 48-bit scenario.


# Keep one comprehensive collision test if desired, or rely on uniqueness tests.
# For this refactoring, focusing on the requested tests.
# The existing test_snowflake_id_collision_32bit2 (renamed to _64bit) can serve this.
Copy link

Copilot AI May 23, 2025

Choose a reason for hiding this comment

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

The async test 'test_async_uniqueness_large_batch_64bit' is missing the @pytest.mark.asyncio decorator, so pytest will not run it in an event loop.

Suggested change
# The existing test_snowflake_id_collision_32bit2 (renamed to _64bit) can serve this.
# The existing test_snowflake_id_collision_32bit2 (renamed to _64bit) can serve this.

Copilot uses AI. Check for mistakes.
"""Test ID generation with two different 64-bit generator configurations."""
generator1 = SnowflakeGenerator(config=TEST_CONFIG_64BIT) # Updated class name
generator2 = SnowflakeGenerator(config=TEST_CONFIG_64BIT2) # Updated class name
id1 = await generator1.generate()
Copy link

Copilot AI May 23, 2025

Choose a reason for hiding this comment

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

There is uncommented test code at the module level—this 'await' call is outside any test function and will cause syntax errors or unintended execution. Remove or re-indent it under the intended test.

Copilot uses AI. Check for mistakes.
# - test_snowflake_id_collision_32bit2
# - one of the test_snowflake_id_collision_32bit3 (the one that was purely collision)
# test_async_uniqueness_32bit now covers the core async uniqueness logic.

Copy link

Copilot AI May 23, 2025

Choose a reason for hiding this comment

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

The async test 'test_base62_encoding_decoding_32bit' is missing the @pytest.mark.asyncio decorator, so pytest will not schedule it under an event loop.

Suggested change

Copilot uses AI. Check for mistakes.
Copy link

@MutharasuArchunan13 MutharasuArchunan13 left a comment

Choose a reason for hiding this comment

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

added:

  1. sync way to create the ID ,
  2. added the testcased along with sync and async ways and bit wise also added

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.

3 participants