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.
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
Add type annotations to crypto_utils and config #5532
Add type annotations to crypto_utils and config #5532
Changes from all commits
53d6809
File filter
Filter by extension
Conversations
Jump to
There are no files selected for viewing
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.
I think this is really hurts the effectiveness of type annotations and code analysis tools in general (especially for critical code like CryptoUtil).
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.
Do you have a solution in mind? A subclass of
Flask
?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.
A subclass wouldn't work because the pattern I've seen in the code base is:
There is no way (I think?) to change the class of
flask.current_app
.One approach I thought of is to have a global variable with the "current" CryptoUtil or Storage instance (based on the
config
) so it would be like this:I can make that change but it's a big refactor and should probably be done in a separate PR?
I have a poc branch where I did this with only the diceware / words lists logic (as a first step), that I took out of CryptoUtil: nabla-c0d3/securedrop@typing-annotations-i18n-store-and-misc...nabla-c0d3:refactor-diceware-generator
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.
What about this?
A quick run through
tests/test_journalist.py
passes, andtype(current_app._get_current_object())
returnsjournalist_app.JournalistApp
. You have to use the old comment annotations before 3.6, but could that solve the annotation/static analysis problems you saw?At any rate, like you said, that change would merit its own PR. Certainly doesn't have to be done here.
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.
I don't think it would work for the pattern I described; you can see it there for example: https://github.com/freedomofpress/securedrop/blob/develop/securedrop/journalist_app/main.py#L108
flask.current_app
is typed as a_FlaskLocalProxy
and I don't see a way to change that.I think fixing this will require changing how the code retrieves the CryptoUtil instance for the current app.
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.
Hmm. With a
Flask
subclass in place, if I add these two lines there:The dev server logs this:
I was going by the Werkzeug docs. I'm wondering how you're seeing a different proxy class, but it is supposed to be some sort of proxy for an instance of
Flask
or a derivation of it.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.
The type does get returned at runtime (as the actual object gets returned, after all), but I don't think this type information is available to mypy when its doing its (static-only) analysis. This is how the method is typed:
There is no way to change it to a subclass of
Flask
. Additionally:_get_current_object()
(and people will forget to call it I think).Overall I think fixing this requires a new approach. I'm also not sure if the CryptoUtil or Storage instances need to be attached to the app - to me it just adds another layer of indirection/complexity compared to doing just
CryptoUtil.get_current()
. I might be missing something tho.