Repository navigation
PYTHON-6274 Enable ruff rule PLR2004 #3225
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
Open
aclark4life
wants to merge
5
commits into
mongodb:main
Choose a base branch
from
aclark4life:PYTHON-6274
base: main
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.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
91b1d17
PYTHON-6274 Enable ruff rule PLR2004
aclark4life 8ee04dc
PYTHON-6274 Rename timezone offset constants by syntax
aclark4life 53e5c25
Merge branch 'main' into PYTHON-6274
aclark4life d206768
PYTHON-6274 Address review: consolidate constants, use them in messages
aclark4life b448836
Merge branch 'main' into PYTHON-6274
aclark4life 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
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
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
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
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 |
|---|---|---|
|
|
@@ -219,6 +219,13 @@ class UuidRepresentation: | |
| """BSON binary subtype for any user defined structure. | ||
| """ | ||
|
|
||
| # Binary subtypes are one byte, so the valid range is [0, 256). | ||
| _SUBTYPE_LIMIT = 256 | ||
| _UUID_LENGTH = 16 | ||
| _MAX_PADDING = 7 | ||
| _INT8_MIN = -128 | ||
| _INT8_MAX = 127 | ||
|
|
||
|
|
||
| class BinaryVectorDtype(Enum): | ||
| """Datatypes of vector subtype. | ||
|
|
@@ -328,7 +335,7 @@ def __new__( | |
| ) -> Self: | ||
| if not isinstance(subtype, int): | ||
| raise TypeError(f"subtype must be an instance of int, not {type(subtype)}") | ||
| if subtype >= 256 or subtype < 0: | ||
| if subtype >= _SUBTYPE_LIMIT or subtype < 0: | ||
| raise ValueError("subtype must be contained in [0, 256)") | ||
| # Support any type that implements the buffer protocol. | ||
| self = bytes.__new__(cls, memoryview(data).tobytes()) | ||
|
|
@@ -421,8 +428,10 @@ def as_uuid(self, uuid_representation: int = UuidRepresentation.STANDARD) -> UUI | |
| if uuid_representation == UuidRepresentation.UNSPECIFIED: | ||
| raise ValueError("uuid_representation cannot be UNSPECIFIED") | ||
|
|
||
| if len(self) != 16: | ||
| raise ValueError(f"cannot decode a uuid from a {len(self)}-byte payload, expected 16") | ||
| if len(self) != _UUID_LENGTH: | ||
| raise ValueError( | ||
| f"cannot decode a uuid from a {len(self)}-byte payload, expected {_UUID_LENGTH}" | ||
| ) | ||
|
|
||
| if uuid_representation == UuidRepresentation.PYTHON_LEGACY: | ||
| if self.subtype == OLD_UUID_SUBTYPE: | ||
|
|
@@ -514,7 +523,7 @@ def from_vector( | |
| raise ValueError(f"padding does not apply to {dtype=}") | ||
| elif dtype == BinaryVectorDtype.PACKED_BIT: # pack ints in [0, 255] as unsigned uint8 | ||
| format_str = "B" | ||
| if 0 <= padding > 7: | ||
| if 0 <= padding > _MAX_PADDING: | ||
| raise ValueError(f"{padding=}. It must be in [0,1, ..7].") | ||
| if padding and not vector: | ||
| raise ValueError("Empty vector with non-zero padding.") | ||
|
|
@@ -546,12 +555,12 @@ def from_vector( | |
| if dtype == BinaryVectorDtype.FLOAT32: | ||
| vector = vector.astype(np.dtype("float32"), copy=False) | ||
| elif dtype == BinaryVectorDtype.INT8: | ||
| if vector.min() >= -128 and vector.max() <= 127: | ||
| if vector.min() >= _INT8_MIN and vector.max() <= _INT8_MAX: | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a behavior change that should be done in a separate ticket. |
||
| vector = vector.astype(np.dtype("int8"), copy=False) | ||
| else: | ||
| raise ValueError("Values found outside INT8 range.") | ||
| elif dtype == BinaryVectorDtype.PACKED_BIT: | ||
| if vector.min() >= 0 and vector.max() <= 127: | ||
| if vector.min() >= 0 and vector.max() <= _INT8_MAX: | ||
| vector = vector.astype(np.dtype("uint8"), copy=False) | ||
| else: | ||
| raise ValueError("Values found outside UINT8 range.") | ||
|
|
@@ -611,8 +620,10 @@ def as_vector(self, return_numpy: bool = False) -> BinaryVector: | |
| # data packed as uint8 | ||
| if padding and not n_bytes: | ||
| raise ValueError("Corrupt data. Vector has a padding P, but no data.") | ||
| if padding > 7 or padding < 0: | ||
| raise ValueError(f"Corrupt data. Padding ({padding}) must be between 0 and 7.") | ||
| if padding > _MAX_PADDING or padding < 0: | ||
| raise ValueError( | ||
| f"Corrupt data. Padding ({padding}) must be between 0 and {_MAX_PADDING}." | ||
| ) | ||
| dtype_format = "B" | ||
| format_string = f"<{n_bytes}{dtype_format}" | ||
| unpacked_uint8s = list(struct.unpack_from(format_string, self, offset)) | ||
|
|
@@ -646,8 +657,10 @@ def as_vector(self, return_numpy: bool = False) -> BinaryVector: | |
| # data packed as uint8 | ||
| if padding and not n_bytes: | ||
| raise ValueError("Corrupt data. Vector has a padding P, but no data.") | ||
| if padding > 7 or padding < 0: | ||
| raise ValueError(f"Corrupt data. Padding ({padding}) must be between 0 and 7.") | ||
| if padding > _MAX_PADDING or padding < 0: | ||
| raise ValueError( | ||
| f"Corrupt data. Padding ({padding}) must be between 0 and {_MAX_PADDING}." | ||
| ) | ||
| data = np.frombuffer(self[offset:], dtype="uint8") | ||
| if padding and np.unpackbits(data[-1])[-padding:].sum() > 0: | ||
| warnings.warn( | ||
|
|
||
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
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.
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.
This differs from
json_util.py's_INT32_MAX = 2**31. There should be one definition shared across all usages.