-
Notifications
You must be signed in to change notification settings - Fork 4
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
Add CheckDataMixin #568
Draft
fzimmermann89
wants to merge
20
commits into
gh/fzimmermann89/33/head
Choose a base branch
from
gh/fzimmermann89/35/head
base: gh/fzimmermann89/33/head
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Add CheckDataMixin #568
Conversation
This file contains 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
This was referenced Nov 29, 2024
fzimmermann89
added a commit
that referenced
this pull request
Nov 29, 2024
ghstack-source-id: f63c6910796c61db777cdf0246e1d5edaec4e352 ghstack-comment-id: 2506886010 Pull Request resolved: #568
📚 Documentation |
fzimmermann89
added a commit
that referenced
this pull request
Nov 29, 2024
ghstack-source-id: f5a1fcae72994dc5ded3fda918068364da7a2617 ghstack-comment-id: 2506886010 Pull Request resolved: #568
fzimmermann89
added a commit
that referenced
this pull request
Nov 29, 2024
ghstack-source-id: bf978f509e37434b2a6e5680aa5238a8f2fdc12f ghstack-comment-id: 2506886010 Pull Request resolved: #568
fzimmermann89
added a commit
that referenced
this pull request
Dec 2, 2024
ghstack-source-id: d3ba8f70111d645e2e8e51108770c33ee6265861 ghstack-comment-id: 2506886010 Pull Request resolved: #568
fzimmermann89
added a commit
that referenced
this pull request
Dec 2, 2024
ghstack-source-id: 61cd0d71ca65c1df4665dfb1de46d4113f5620e4 ghstack-comment-id: 2506886010 Pull Request resolved: #568
fzimmermann89
added a commit
that referenced
this pull request
Dec 2, 2024
ghstack-source-id: 139c4abb3716fcedc462c2f593e1f073e6fb56d5 ghstack-comment-id: 2506886010 Pull Request resolved: #568
fzimmermann89
added a commit
that referenced
this pull request
Dec 2, 2024
ghstack-source-id: cfe2e7e770ef021524e16a1f1531ec451aab3c17 ghstack-comment-id: 2506886010 Pull Request resolved: #568
fzimmermann89
added a commit
that referenced
this pull request
Dec 2, 2024
ghstack-source-id: 0a9bcb0d6d68dd93ef6016d994f7dca796777305 ghstack-comment-id: 2506886010 Pull Request resolved: #568
Draft
Draft
Closed
Draft
fzimmermann89
commented
Jan 3, 2025
class CheckedDataClass(CheckDataMixin): | ||
"""A test dataclass.""" | ||
|
||
float_tensor: Annotated[ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See here for an example of the Annotations
fzimmermann89
commented
Jan 3, 2025
) | ||
|
||
|
||
def test_suspend_check_success() -> None: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
see here (and next text) for the suspend checks context manager
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.
This adds a new Mixin for Dataclasses that checks the types of the fields.
If the fields are Annotated with a (newly added)
Annotation
, specifying either its data type or shape, this will also be checked.After some discussions following #495, this is now optional. None of the core reshaping or indexing functions will depend on these checks, but the will be performed afterwards. If this ever becomes an issue, removing the mixin from the dataclass will only remove the checks.
This is IMHO the best way to check that certain invariants are true after reshaping or indexing in a readable and maintable way.
An alternative I considered was using data class descriptors instead of Annotated and perform the check in the set of the descriptors. But this would make the checking code fundamental to the data classes, instead of just a checking addon that can be removed.
One can specify the expected shape of a field using the syntax of jaxtyping.
There is also a context manager to disable checks temporarily.
By default, when exiting the context, the suspended checks will be performed. So one can first create a wrongly shaped data class, reshape it, and on exit it is ensured that the fields follow the rules. This behavior can also be disabled to disable checks in a part of the code completely.
The string parsing etc happens on import time, not at creation time.
Parts of the shape checks are cached, so running the same invariants check multiple times without substantial changes to the shapes will be fast.
I also removed some
__future__.annotation
imports, as they stringify all type annotations. Instead, we should either fix the dependencies, use stringified annotations (these cannot be runtime checked) or wait for PEP 649/749