-
Notifications
You must be signed in to change notification settings - Fork 3
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 hints and improve or add missing docstrings #80
Conversation
Besides what I already submitted in this PR, I thought of applying a couple other changes too:
Thoughts? |
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.
Please move the documentation improvements to a separate PR for better review handling (put me as a reviewer on that docs PR).
There are two changes requested. One is related to breaking a line which I personally don't feel it's that long that would require a line break. I saw similar line breaks in other parts of the code, which also didn't feel appropriate given the original length.
I left the actual code review work for Mike and Mario
pg_backup_api/pg_backup_api/run.py
Outdated
@@ -94,32 +158,55 @@ def recovery_operation(args): | |||
end_time = server_ops.time_event_now() | |||
content["success"] = success | |||
content["end_time"] = end_time | |||
content["output"] = output.decode() if isinstance(output, bytes) else output | |||
content["output"] = output.decode() if isinstance(output, bytes) \ |
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 see any advantages to splitting the line here. It's not like the original line is terribly long that would make sense to split 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.
I only did that to silence flake8
.
I'm not sure if we intend to add a linter to the project later to check the code on pushed commits?
I can revert that change and silence the flake8 warning if you prefer.
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.
We could add black/flake8 to our workflow if you like. I usually try to prioritise readability than form. The above splitting I think proves my point.
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.
We can convert that into 2 lines or write a method to sanitise content{"output"] so we can silence flake8 and will be easy to read IMO.
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.
Sounds good. I'm reverting the change for now. Maybe we can even set flake8 to allow max-line length as 120 for example, like we have in Patroni.
if not self.operation_id: | ||
raise Exception("operation_id is required here") | ||
|
||
file_name = "{}.json".format(self.operation_id) | ||
fpath = join(file_type, file_name) | ||
file_name = f"{self.operation_id}.json". |
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.
Are you missing something after the trailing dot?
Said differently, is that dot a leftover from the original code?
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 guess I added that by mistake after running flake8
, otherwise it would have showed something like this when I ran the linter:
pg_backup_api/server_operation.py:228:50: E999 SyntaxError: invalid syntax
I converted this PR into a draft. I'll work on splitting this into different PRs, each one covering a different type of change. |
Reverted the changes on |
README.md
changes, f-strings, fixing undefined variables
This PR now contains only changes related to type hints and docstrings |
8161979
to
b0a77ca
Compare
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.
Thanks for adding the docstrings - looks great overall.
I suggested a handful of minor/superficial changes to the wording and also found an issue with the conditional import causing errors when TYPE_CHECKING
is False
.
pg_backup_api/pg_backup_api/app.py
Outdated
""" | ||
Used when running pg-backup-api REST API server as an WSGI application. | ||
|
||
Load Barman configuration, set up logging for WSGI, and set up an JSON console |
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.
Should be a JSON
not an JSON
.
pg_backup_api/pg_backup_api/app.py
Outdated
Used when running pg-backup-api REST API server as an WSGI application. | ||
|
||
Load Barman configuration, set up logging for WSGI, and set up an JSON console | ||
output writter. |
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.
writer
instead of writter
@@ -31,9 +33,19 @@ | |||
from pg_backup_api.run import app | |||
from pg_backup_api.server_operation import ServerOperation, ServerOperationConfigError | |||
|
|||
if TYPE_CHECKING: # pramga: no cover |
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.
pramga
-> pragma
def diagnose(): | ||
def diagnose() -> Response: | ||
""" | ||
``Handle GET`` request to ``/diagnose``. |
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.
Looks like Handle
shouldn't be in the double backticks here.
operation. | ||
:param operation_id: ID of the recovery operation previously created | ||
through pg-backup-api. | ||
:return: if *server_name* and *operation_id* are valid, return an JSON |
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 JSON
pg_backup_api/pg_backup_api/utils.py
Outdated
@@ -16,39 +16,59 @@ | |||
# You should have received a copy of the GNU General Public License | |||
# along with Postgres Backup API. If not, see <http://www.gnu.org/licenses/>. | |||
|
|||
""" | |||
Utilitary functions and constants used through pg-backup-api code. |
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.
Utilitary
-> Utility
|
||
from flask import Flask | ||
|
||
import barman | ||
from barman import config | ||
from barman.infofile import BackupInfo | ||
|
||
if TYPE_CHECKING: # pragma: no cover |
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.
On python 3.6 and 3.7 at least (I haven't tested other versions yet) the imports on the following lines are still required, even if TYPE_CHECKING
is False
. Currently it fails as follows:
$ ~/.local/bin/pg-backup-api
Traceback (most recent call last):
File "/var/lib/barman/.local/bin/pg-backup-api", line 11, in <module>
load_entry_point('pg-backup-api', 'console_scripts', 'pg-backup-api')()
File "/usr/lib/python3.6/site-packages/pkg_resources/__init__.py", line 476, in load_entry_point
return get_distribution(dist).load_entry_point(group, name)
File "/usr/lib/python3.6/site-packages/pkg_resources/__init__.py", line 2700, in load_entry_point
return ep.load()
File "/usr/lib/python3.6/site-packages/pkg_resources/__init__.py", line 2318, in load
return self.resolve()
File "/usr/lib/python3.6/site-packages/pkg_resources/__init__.py", line 2324, in resolve
module = __import__(self.module_name, fromlist=['__name__'], level=0)
File "/pg-backup-api/pg_backup_api/pg_backup_api/__init__.py", line 20, in <module>
import pg_backup_api.logic.utility_controller
File "/pg-backup-api/pg_backup_api/pg_backup_api/logic/utility_controller.py", line 31, in <module>
from pg_backup_api.utils import load_barman_config, get_server_by_name, parse_backup_id
File "/pg-backup-api/pg_backup_api/pg_backup_api/utils.py", line 46, in <module>
def create_app() -> flask.app.Flask:
NameError: name 'flask' is not defined
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.
Oh, I missed quoting these.
@@ -31,9 +33,19 @@ | |||
from pg_backup_api.run import app | |||
from pg_backup_api.server_operation import ServerOperation, ServerOperationConfigError | |||
|
|||
if TYPE_CHECKING: # pramga: no cover |
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.
Conditionally importing Request
and Response
causes the following failure when TYPE_CHECKING
is False
:
$ ~/.local/bin/pg-backup-api serve
Traceback (most recent call last):
File "/var/lib/barman/.local/bin/pg-backup-api", line 11, in <module>
load_entry_point('pg-backup-api', 'console_scripts', 'pg-backup-api')()
File "/usr/lib/python3.6/site-packages/pkg_resources/__init__.py", line 476, in load_entry_point
return get_distribution(dist).load_entry_point(group, name)
File "/usr/lib/python3.6/site-packages/pkg_resources/__init__.py", line 2700, in load_entry_point
return ep.load()
File "/usr/lib/python3.6/site-packages/pkg_resources/__init__.py", line 2318, in load
return self.resolve()
File "/usr/lib/python3.6/site-packages/pkg_resources/__init__.py", line 2324, in resolve
module = __import__(self.module_name, fromlist=['__name__'], level=0)
File "/pg-backup-api/pg_backup_api/pg_backup_api/__init__.py", line 20, in <module>
import pg_backup_api.logic.utility_controller
File "/pg-backup-api/pg_backup_api/pg_backup_api/logic/utility_controller.py", line 41, in <module>
def diagnose() -> Response:
NameError: name 'Response' is not defined
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.
Missing quoting here too.
pg_backup_api/pg_backup_api/utils.py
Outdated
|
||
:param server: server from which to get a backup. | ||
:param backup_id: ID of the backup to be retrieved. It accepts a few | ||
wildcards: |
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.
Could we say aliases
instead of wildcards
here?
README.md
Outdated
in the dependency tree. Besides that, you also need the `libpq` C library in | ||
your system (`libpq-dev` package on Debian based, or `libpq-devel` package on | ||
RedHat based). | ||
tbd |
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.
Did you mean to revert the README.md
changes 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.
No, something went wrong with the rebase, sorry.
I'm sure I accept head although it created problems with README.md
🤔
I'll turn this into a draft and check what is going on.
Signed-off-by: Israel Barth Rubio <[email protected]>
…ler.py` Signed-off-by: Israel Barth Rubio <[email protected]>
Signed-off-by: Israel Barth Rubio <[email protected]>
Signed-off-by: Israel Barth Rubio <[email protected]>
Signed-off-by: Israel Barth Rubio <[email protected]>
Signed-off-by: Israel Barth Rubio <[email protected]>
Signed-off-by: Israel Barth Rubio <[email protected]>
Signed-off-by: Israel Barth Rubio <[email protected]>
Signed-off-by: Israel Barth Rubio <[email protected]>
b0a77ca
to
09190be
Compare
``` pg_backup_api/__main__.py:87:1: W391 blank line at end of file ``` Signed-off-by: Israel Barth Rubio <[email protected]>
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.
Looks good - I think these commits can all squash down into a single commit which adds the type hints and docstrings.
I really wished we could have squashed down before merging. |
@gonzalemario I guess you are mixing the PRs. This one was actually squashed into b4a7d1f The one that is showing more commits on |
Yeah. I wasn't explicit enough about the rebase, sorry. |
This PR applies the following changes:
References: BAR-89.