Skip to content

WIP: Custom frozen dataclasses #586

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

Draft
wants to merge 9 commits into
base: master
Choose a base branch
from
Draft

Conversation

tony
Copy link
Member

@tony tony commented Feb 28, 2025

Resolves #588

Summary by Sourcery

Tests:

  • Adds comprehensive tests for the frozen_dataclass decorator, covering initialization, immutability, inheritance, edge cases, nested mutability, bidirectional references, and mutation methods.

Copy link

sourcery-ai bot commented Feb 28, 2025

Reviewer's Guide by Sourcery

This pull request introduces a frozen_dataclass decorator that allows creating immutable dataclasses that inherit from mutable ones. It includes comprehensive tests and configurations for mypy and ruff.

Class diagram for frozen_dataclass decorator

classDiagram
    class frozen_dataclass {
        +__init__(cls: type[_T]) type[_T]
    }
    class dataclasses.dataclass {
        +__init__(frozen: bool)
    }
    class cls {
        +__setattr__(name: str, value: t.Any) None
        +__delattr__(name: str) None
        -_frozen: bool
    }
    frozen_dataclass --|> dataclasses.dataclass: Uses
    frozen_dataclass --|> cls: Decorates
Loading

File-Level Changes

Change Details Files
Introduces a frozen_dataclass decorator to create immutable dataclasses inheriting from mutable ones.
  • Adds a frozen_dataclass decorator.
  • Applies @dataclasses.dataclass(frozen=False) to preserve dataclass generation.
  • Overrides __setattr__ and __delattr__ to prevent changes after initialization.
  • Adds a _frozen attribute to control immutability.
  • Includes documentation and examples for the decorator.
src/libtmux/_internal/frozen_dataclass.py
Adds tests for the frozen_dataclass decorator, covering initialization, immutability, inheritance, and edge cases.
  • Creates base and snapshot dataclasses for testing.
  • Tests immutability by attempting to modify fields and verifying AttributeError exceptions.
  • Tests inheritance from mutable base classes.
  • Tests internal attribute modification.
  • Tests nested mutability.
  • Tests bidirectional references.
  • Adds parametrized tests for dimensions and frozen flag behavior.
  • Adds tests for mutation methods and inheritance behavior.
tests/_internal/test_frozen_dataclass.py
Configures mypy to ignore method-assign errors for the frozen_dataclass module.
  • Adds a mypy override to disable method-assign errors for the libtmux._internal.frozen_dataclass module.
pyproject.toml
Configures ruff to ignore B010 (set-attr-with-constant) errors for the frozen_dataclass module.
  • Adds a ruff ignore rule to disable B010 errors for the src/libtmux/_internal/frozen_dataclass.py module.
pyproject.toml

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!
  • Generate a plan of action for an issue: Comment @sourcery-ai plan on
    an issue to generate a plan of action for it.

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

codecov bot commented Feb 28, 2025

Codecov Report

Attention: Patch coverage is 84.23645% with 32 lines in your changes missing coverage. Please review.

Project coverage is 80.25%. Comparing base (5803841) to head (0975cfd).

Files with missing lines Patch % Lines
src/libtmux/_internal/frozen_dataclass_sealable.py 79.25% 20 Missing and 8 partials ⚠️
src/libtmux/_internal/frozen_dataclass.py 92.85% 1 Missing and 1 partial ⚠️
.../_internal/frozen_dataclass_sealable/test_basic.py 95.00% 1 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master     #586      +/-   ##
==========================================
+ Coverage   79.83%   80.25%   +0.42%     
==========================================
  Files          22       25       +3     
  Lines        1914     2117     +203     
  Branches      294      338      +44     
==========================================
+ Hits         1528     1699     +171     
- Misses        266      288      +22     
- Partials      120      130      +10     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@tony tony force-pushed the frozen-dataclasses-custom branch 9 times, most recently from d4dc6e7 to a490526 Compare February 28, 2025 13:17
@tony
Copy link
Member Author

tony commented Feb 28, 2025

@sourcery-ai review

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 @tony - I've reviewed your changes - here's some feedback:

Overall Comments:

  • Consider adding a note about the performance implications of using __setattr__ and __delattr__.
  • It would be helpful to include a warning in the documentation about the potential for users to bypass the immutability by modifying the _frozen attribute.
Here's what I looked at during the review
  • 🟢 General issues: all looks good
  • 🟢 Security: all looks good
  • 🟡 Testing: 1 issue found
  • 🟢 Complexity: 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.

width=80,
height=24,
expected_error=False,
),
Copy link

Choose a reason for hiding this comment

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

issue (testing): Edge case: Unfreezing via _frozen attribute

Although tested, the ability to unfreeze by modifying _frozen is a security risk. Consider making _frozen a property with a private setter to prevent this or document this behavior clearly as a known limitation.

@tony tony force-pushed the frozen-dataclasses-custom branch 4 times, most recently from d2c13aa to 70e415c Compare February 28, 2025 18:57
@tony tony mentioned this pull request Feb 28, 2025
@tony tony force-pushed the frozen-dataclasses-custom branch 7 times, most recently from 4353421 to e18198b Compare March 1, 2025 00:03
@tony tony force-pushed the frozen-dataclasses-custom branch 4 times, most recently from e77bc7d to e881b35 Compare March 2, 2025 13:31
@tony tony force-pushed the frozen-dataclasses-custom branch from e881b35 to ba0a3b9 Compare March 2, 2025 14:57
@tony tony force-pushed the master branch 3 times, most recently from ab84634 to 5803841 Compare April 6, 2025 12:44
tony added 9 commits April 6, 2025 07:48
why: Fix type checking errors in the custom frozen_dataclass implementation

what:
- Added targeted mypy configuration override to disable method-assign errors
- Only scoped to libtmux._internal.frozen_dataclass module
- Preserves strict type checking across the rest of the codebase

refs: Enables inheritance from mutable to immutable dataclasses
…thod-assign`

why: Fix type checking errors in the custom frozen_dataclass implementation

what:
- Added targeted mypy configuration override to disable method-assign errors
- Only scoped to libtmux._internal.frozen_dataclass module
- Preserves strict type checking across the rest of the codebase

refs: Enables inheritance from mutable to immutable dataclasses
…iles from type checking

why: The frozen_dataclass_sealable decorator adds attributes and methods dynamically at runtime,
which mypy cannot properly analyze in test contexts, resulting in false positive errors.

what:
- Added mypy override to ignore type errors in tests._internal.test_frozen_dataclass_sealable
- Added mypy override to ignore type errors in tests.examples._internal.frozen_dataclass_sealable.test_basic
- Preserves strict typing for the implementation code while allowing tests to use dynamic features

refs: This addresses the mypy test failures while maintaining type safety for the implementation
…tests from strict checking

why:
The frozen_dataclass_sealable decorator adds attributes and methods dynamically
at runtime which causes false positive errors with static analysis tools.
Testing this functionality requires patterns that deliberately violate some
rules.

what:
- Added mypy override to ignore type errors in
  tests._internal.test_frozen_dataclass_sealable
- Added mypy override to ignore type errors in
  tests.examples._internal.frozen_dataclass_sealable.test_basic
- Added per-file ignore for RUF009 (function call in default argument) in
  test_frozen_dataclass_sealable.py
- Preserves strict typing and linting for implementation code while allowing
  tests to use dynamic features

refs: This maintains code quality while acknowledging the inherent
limitations of static analysis tools when dealing with Python's dynamic runtime
features
@tony tony force-pushed the frozen-dataclasses-custom branch from ba0a3b9 to 0975cfd Compare April 6, 2025 12:48
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.

Frozen dataclasses
1 participant