-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Make MemoryView
Generic, make cast
accurate
#12247
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
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
c1e1fe8
Make MemoryView generic
max-muoto 2ab244f
Remove fallback
max-muoto 324958d
Explicit
max-muoto 0c1a4c3
Fix
max-muoto f0f890e
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] a12b494
Tweak?
max-muoto 2f53e1a
Add tests
max-muoto f4cb985
Merge branch 'main' into memoryview-as-generic
max-muoto 4a5d917
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 1960c19
Fix float formats
max-muoto f01fc40
Add test case
max-muoto ba70c57
Grammar
max-muoto File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
from __future__ import annotations | ||
|
||
import array | ||
from typing_extensions import assert_type | ||
|
||
# Casting to bytes. | ||
buf = b"abcdefg" | ||
view = memoryview(buf).cast("c") | ||
elm = view[0] | ||
assert_type(elm, bytes) | ||
assert_type(view[0:2], memoryview[bytes]) | ||
|
||
# Casting to a bool. | ||
a = array.array("B", [0, 1, 2, 3]) | ||
mv = memoryview(a) | ||
bool_mv = mv.cast("?") | ||
assert_type(bool_mv[0], bool) | ||
assert_type(bool_mv[0:2], memoryview[bool]) | ||
|
||
|
||
# Casting to a signed char. | ||
a = array.array("B", [0, 1, 2, 3]) | ||
mv = memoryview(a) | ||
signed_mv = mv.cast("b") | ||
assert_type(signed_mv[0], int) | ||
assert_type(signed_mv[0:2], memoryview[int]) | ||
|
||
# Casting to a signed short. | ||
a = array.array("B", [0, 1, 2, 3]) | ||
mv = memoryview(a) | ||
signed_mv = mv.cast("h") | ||
assert_type(signed_mv[0], int) | ||
assert_type(signed_mv[0:2], memoryview[int]) | ||
|
||
# Casting to a signed int. | ||
a = array.array("B", [0, 1, 2, 3]) | ||
mv = memoryview(a) | ||
signed_mv = mv.cast("i") | ||
assert_type(signed_mv[0], int) | ||
assert_type(signed_mv[0:2], memoryview[int]) | ||
|
||
# Casting to a signed long. | ||
a = array.array("B", [0, 1, 2, 3]) | ||
mv = memoryview(a) | ||
signed_mv = mv.cast("l") | ||
assert_type(signed_mv[0], int) | ||
assert_type(signed_mv[0:2], memoryview[int]) | ||
|
||
# Casting to a float. | ||
a = array.array("B", [0, 1, 2, 3]) | ||
mv = memoryview(a) | ||
float_mv = mv.cast("f") | ||
assert_type(float_mv[0], float) | ||
assert_type(float_mv[0:2], memoryview[float]) | ||
|
||
# An invalid literal should raise an error. | ||
mv = memoryview(b"abc") | ||
mv.cast("abc") # type: ignore |
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
Oops, something went wrong.
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.
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.
Let me know if we'd prefer these to be inline for the sake of IDEs, seemed harder to maintain so just decided to place them outside.