libtmux icon indicating copy to clipboard operation
libtmux copied to clipboard

snapshot(refactor[typing]): Improve type overrides with generics

Open tony opened this issue 8 months ago • 2 comments

#587

why: Remove the need for type: ignore comments on property overrides

what:

  • Use Generic base classes with covariant type parameters
  • Add properly typed overrides for inherited properties
  • Define a clear SnapshotType union type for shared operations
  • Improve type safety in filter_snapshot with better type checks

Summary by Sourcery

Improves type safety and reduces the need for type ignores by using generic base classes with covariant type parameters for snapshots. It also defines a union type for shared snapshot operations and improves type checking in the filter_snapshot function.

Enhancements:

  • Improves type safety by using generic base classes with covariant type parameters.
  • Defines a clear SnapshotType union type for shared operations.
  • Improves type safety in filter_snapshot with better type checks by using isinstance.
  • Adds properly typed overrides for inherited properties.

tony avatar Mar 02 '25 12:03 tony

Reviewer's Guide by Sourcery

This pull request refactors the snapshot module to improve type safety by using generic base classes with covariant type parameters, defining a SnapshotType union, and adding properly typed overrides for inherited properties. This removes the need for type: ignore comments and enhances the overall type checking within the module, especially in functions like filter_snapshot.

No diagrams generated as the changes look simple and do not need a visual representation.

File-Level Changes

Change Details Files
Introduces generic base classes for _SealableWindowBase, _SealableSessionBase, and _SealableServerBase to improve type safety and remove the need for type: ignore comments on property overrides.
  • Define type variables for generic typing.
  • Make base classes implement Sealable and use Generics.
  • Add generic type parameters to _SealableWindowBase, _SealableSessionBase, and _SealableServerBase.
  • Use t.cast to properly type the overridden properties like panes, active_pane, and windows in the generic base classes.
  • Update WindowSnapshot, SessionSnapshot, and ServerSnapshot to inherit from the new generic base classes.
src/libtmux/snapshot.py
Defines a SnapshotType union type for shared operations across different snapshot classes.
  • Define a SnapshotType union type that includes ServerSnapshot, SessionSnapshot, WindowSnapshot, and PaneSnapshot.
src/libtmux/snapshot.py
Improves type safety in the filter_snapshot function by using the SnapshotType union and more specific type checks.
  • Update the snapshot parameter in filter_snapshot to use the SnapshotType union.
  • Update the filter_func parameter in filter_snapshot to use the SnapshotType union.
  • Add explicit type annotations for filtered_sessions and filtered_windows lists.
  • Add type checking using isinstance when appending to filtered_sessions and filtered_windows.
  • Use Optional type for return type annotation.
src/libtmux/snapshot.py
Updates the snapshot_to_dict function to use the SnapshotType union.
  • Update the snapshot parameter in snapshot_to_dict to use a union of SnapshotType and t.Any.
src/libtmux/snapshot.py
Updates the is_active inner function within snapshot_active_only to use the SnapshotType union.
  • Update the obj parameter in is_active to use the SnapshotType union.
src/libtmux/snapshot.py

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

  • Contact our support team for questions or feedback.
  • Visit our documentation for detailed guides and information.
  • Keep in touch with the Sourcery team by following us on X/Twitter, LinkedIn or GitHub.

sourcery-ai[bot] avatar Mar 02 '25 12:03 sourcery-ai[bot]

Codecov Report

Attention: Patch coverage is 77.31481% with 147 lines in your changes missing coverage. Please review.

Project coverage is 79.23%. Comparing base (5803841) to head (ac85da4). Report is 26 commits behind head on snapshots.

Files with missing lines Patch % Lines
src/libtmux/snapshot/models/pane.py 56.25% 27 Missing and 8 partials :warning:
src/libtmux/_internal/frozen_dataclass_sealable.py 79.10% 20 Missing and 8 partials :warning:
src/libtmux/snapshot/utils.py 71.26% 19 Missing and 6 partials :warning:
src/libtmux/snapshot/models/window.py 72.30% 11 Missing and 7 partials :warning:
src/libtmux/snapshot/models/session.py 73.43% 11 Missing and 6 partials :warning:
src/libtmux/snapshot/base.py 80.76% 10 Missing :warning:
src/libtmux/snapshot/models/server.py 84.37% 9 Missing and 1 partial :warning:
src/libtmux/_internal/frozen_dataclass.py 92.85% 1 Missing and 1 partial :warning:
.../_internal/frozen_dataclass_sealable/test_basic.py 95.00% 1 Missing and 1 partial :warning:
Additional details and impacted files
@@              Coverage Diff              @@
##           snapshots     #590      +/-   ##
=============================================
- Coverage      79.83%   79.23%   -0.60%     
=============================================
  Files             22       33      +11     
  Lines           1914     2562     +648     
  Branches         294      416     +122     
=============================================
+ Hits            1528     2030     +502     
- Misses           266      375     +109     
- Partials         120      157      +37     

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

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

codecov[bot] avatar Mar 02 '25 12:03 codecov[bot]