Repository navigation
MAINT: Use PEP 8 names in the encryption module - #4436
zeyadrabie wants to merge 2 commits into
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #4436 +/- ##
=======================================
Coverage 99.21% 99.21%
=======================================
Files 59 59
Lines 12425 12427 +2
Branches 2368 2368
=======================================
+ Hits 12328 12330 +2
Misses 42 42
Partials 55 55 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
|
|
||
| @property | ||
| def PRINT_SCALING(self) -> NameObject: | ||
| def PRINT_SCALING(self) -> NameObject: # noqa: N802 |
There was a problem hiding this comment.
I have the feeling that we will forget to migrate this properly. We should probably deprecate this without a replacement for now, until we use named constants everywhere.
Do you think you can do this in this PR, or do we need another PR for this?
There was a problem hiding this comment.
I'll handle this in a separate deprecation PR once this merges and keep the noqa for now.
|
|
||
| @staticmethod | ||
| def compute_O_value_key(owner_password: bytes, rev: int, key_size: int) -> bytes: | ||
| def compute_o_value_key(owner_password: bytes, revision: int, key_size: int) -> bytes: |
There was a problem hiding this comment.
Why the short o? As far as I understand, this is the "owner password", thus we could name it compute_owner_password_value_key?
There was a problem hiding this comment.
Yep, renamed to compute_owner_password_value_key and updated the other short o/u names in the file to match.
|
|
||
| @staticmethod | ||
| def compute_O_value(rc4_key: bytes, user_password: bytes, rev: int) -> bytes: | ||
| def compute_o_value(rc4_key: bytes, user_password: bytes, revision: int) -> bytes: |
There was a problem hiding this comment.
Renamed to compute_owner_password_value.
|
|
||
| @staticmethod | ||
| def compute_U_value(key: bytes, rev: int, id1_entry: bytes) -> bytes: | ||
| def compute_u_value(key: bytes, revision: int, id1_entry: bytes) -> bytes: |
There was a problem hiding this comment.
Renamed to compute_user_password_value.
|
|
||
| @staticmethod | ||
| def compute_Perms_value(key: bytes, p: int, metadata_encrypted: bool) -> bytes: | ||
| def compute_perms_value(key: bytes, permissions: int, metadata_encrypted: bool) -> bytes: |
There was a problem hiding this comment.
If we already use permissions for the P value everywhere, shouldn't we align this name here as well?
| def compute_perms_value(key: bytes, permissions: int, metadata_encrypted: bool) -> bytes: | |
| def compute_permissions_value(key: bytes, permissions: int, metadata_encrypted: bool) -> bytes: |
| @@ -864,11 +864,11 @@ class EncryptAlgorithm(tuple, Enum): # type: ignore[type-arg] # noqa: SLOT001 | |||
|
|
|||
|
|
|||
| class EncryptionValues: | |||
There was a problem hiding this comment.
I would suggest two changes here:
- Make this a dataclass.
- Use full names instead of abbreviations again.
According to section 7.6.4.4.1 of ISO 32000-2:2020, the following verbose names can be used:
The computation of the values for the O (owner password) and the U (user password) entries for encryption revision 4 and earlier, and the O, U, OE (owner encryption), UE (user encryption) and Perms (permissions) values for encryption revision 6 require more explanation.
There was a problem hiding this comment.
Switched it to a dataclass and kept the _value suffix so the fields don't clash with the actual passwords and permissions in the same methods.
| when the document is opened with user access | ||
| entry: The encryption dictionary object. | ||
| EncryptMetadata: Whether to encrypt metadata in the document. | ||
| metadata_encrypted: Whether to encrypt metadata in the document. |
There was a problem hiding this comment.
To better indicate that this is a condition, I would keep encrypt_metadata as name.
There was a problem hiding this comment.
Switched back to encrypt_metadata everywhere.
|
|
||
| @property | ||
| def _ID(self) -> ArrayObject | None: | ||
| def _ID(self) -> ArrayObject | None: # noqa: N802 |
There was a problem hiding this comment.
Why aren't we renaming this, but ignore it? We will forget about this, unless we open a dedicated issue for it. (Similar to PRINT_SCALING, which is public, but used less often in our code.)
There was a problem hiding this comment.
Renamed to _file_identifiers on both PdfReader and PdfWriter.
Rename the uppercase functions, arguments and attributes in pypdf/_encryption.py and stop ignoring Ruff N802 and N803 for pypdf/. Part of py-pdf#3231.
Rename the remaining short names, make EncryptionValues a dataclass, keep encrypt_metadata and rename _ID to _file_identifiers.
68d0139 to
2db52d2
Compare
Part of #3231.
Renames the internal functions, arguments, locals, and attributes in
pypdf/_encryption.pyto full PEP 8 names (keeping the PDF keys in comments/docstrings), convertsEncryptionValuesto a dataclass, and fixes fourAlgV4docstrings that saidkey_sizewas in bytes instead of bits. Nothing public exposes these classes or attributes, so no deprecation is needed.Also renames
PdfReader._IDandPdfWriter._IDto_file_identifiersand removes the"pypdf/*" = ["N802", "N803"]ignore frompyproject.toml.ViewerPreferences.PRINT_SCALINGkeeps a# noqa: N802for now since it's public and needs a separate deprecation PR.AI assistance: Used Claude Code (Claude Opus 5.5) to script the renames, apply the edits, and run the checks; I reviewed the diff myself.