Open pubpub-zz opened 1 month ago
The API is ambiguous here, which makes a solution elusive. In a simple construction like PdfWriter(f)
the semantics depend on if f
is an empty file or not. Empty means write to f
and non-empty means clone from f
. This violates the principle of least astonishment. I would like it better if there were separate keyword arguments for "write to" and "clone from".
The other main problem is that the context manager __exit__()
is going to try to write to (and close) the file object, even if it was of the "clone from" kind and not writable.
The double-init suggests that the logic and API can be tightened up.
I have already requested changes on the corresponding PR, which is currently flawed and just introduced a magic positional argument which would guess its correct destination - while this would work if the flaws are removed, this just complicates the pypdf code unnecessarily as the user should know best what is desired in each case.
My proposal was to make the API more explicit by enforcing keyword-only arguments in the future (after a deprecation period). This way, we should at least avoid the ambiguity and force the user to set the correct value to avoid the current side effects.
Originally posted by @MasterOdin in https://github.com/py-pdf/pypdf/issues/2905#issuecomment-2421498743