Skip to content
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

Pylint alerts corrections as part of an intervention experiment 398 #399

Closed
wants to merge 3 commits into from

Conversation

evidencebp
Copy link

Makes the interventions describe in intervention issue.
The experiment is described here.

Each intervention was done in a dedicated commit with a message explaining it.
However, note that the original code and fix are very similar.

Catching Exception might hide unexpected exceptions, like those that might be raised due to future modification.
Therefore, it is recommended to narrow the exceptions.

The method verify_signature of class Signer catches exception in line 233.The try section just calls base64_decode from encoding.py, which in line 37 
 except (TypeError, ValueError) as e:       
    raise BadData("Invalid base64-encoded data") from e

raises BadData instead of TypeError and ValueError.Note that binascii.Error is not translated and might be raised.    https://docs.python.org/3/library/base64.html#base64.b64decode

I therefore replaced catching Exception in catching  (BadData, base64.binascii.Error)
Catching Exception might hide unexpected exceptions, like those that might be raised due to future modification.
Therefore, it is recommended to narrow the exceptions.

The method unsign of class TimestampSigner catches exception in line 116.The try section just calls bytes_to_int(base64_decode(ts_bytes)) from encoding.py.base64_decode  in line 37   
except (TypeError, ValueError) as e:       
    raise BadData("Invalid base64-encoded data") from e

raises BadData instead of TypeError and ValueError.
Note that binascii.Error is not translated and might be raised.  https://docs.python.org/3/library/base64.html#base64.b64decode

I therefore replaced catching Exception in catching  (BadData, base64.binascii.Error) 
https://stackoverflow.com/questions/43560936/python-how-to-catch-this-error-cant-source-error-name-binascii-error/43561287

bytes_to_int, also from encoding.py does not seem to raise exceptions.
The error is not found and fails the CI/CD
I thought that the used library is
https://docs.python.org/3/library/base64.html#base64.b64decode

Removing to make the CI/CD pass again.

typing: commands[0]> mypy
src/itsdangerous/signer.py:233: error: Module has no attribute "binascii"
[attr-defined]
            except (BadData, base64.binascii.Error):
                             ^~~~~~~~~~~~~~~
src/itsdangerous/timed.py:116: error: Module has no attribute "binascii"
[attr-defined]
            except (BadData, base64.binascii.Error):
                             ^~~~~~~~~~~~~~~
Found 2 errors in 2 files (checked 8 source files)
@davidism davidism closed this Nov 25, 2024
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Dec 14, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants