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

Add type hints and improve or add missing docstrings #80

Merged
merged 10 commits into from
Aug 11, 2023

Conversation

barthisrael
Copy link
Contributor

@barthisrael barthisrael commented Aug 9, 2023

This PR applies the following changes:

  • Add type hints to the code;
  • Improve existing docstrings of methods and functions;
  • Add missing docstrings to modules, methods, functions and classes.

References: BAR-89.

@barthisrael
Copy link
Contributor Author

Besides what I already submitted in this PR, I thought of applying a couple other changes too:

  • Rename write_jobs_files as create_job_file
  • Make create_output_file create the output dir instead of write_jobs_files doing that.

Thoughts?

Copy link
Collaborator

@martinmarques martinmarques left a 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

@@ -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) \
Copy link
Collaborator

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.

Copy link
Contributor Author

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.

Copy link
Contributor

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.

Copy link
Contributor

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.

Copy link
Contributor Author

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".
Copy link
Collaborator

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?

Copy link
Contributor Author

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

@barthisrael barthisrael marked this pull request as draft August 10, 2023 11:11
@barthisrael
Copy link
Contributor Author

I converted this PR into a draft. I'll work on splitting this into different PRs, each one covering a different type of change.

@barthisrael
Copy link
Contributor Author

Reverted the changes on README.md

@barthisrael barthisrael changed the title Overall improvements -- type hints, docstrings, README.md changes, f-strings, fixing undefined variables WIP - Add type hints and improve or add missing docstrings Aug 10, 2023
@barthisrael barthisrael marked this pull request as ready for review August 10, 2023 13:41
@barthisrael
Copy link
Contributor Author

This PR now contains only changes related to type hints and docstrings

@barthisrael barthisrael changed the title WIP - Add type hints and improve or add missing docstrings Add type hints and improve or add missing docstrings Aug 10, 2023
Copy link
Contributor

@mikewallace1979 mikewallace1979 left a 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.

"""
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
Copy link
Contributor

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.

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.
Copy link
Contributor

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
Copy link
Contributor

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``.
Copy link
Contributor

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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a JSON

@@ -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.
Copy link
Contributor

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
Copy link
Contributor

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

Copy link
Contributor Author

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
Copy link
Contributor

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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing quoting here too.


:param server: server from which to get a backup.
:param backup_id: ID of the backup to be retrieved. It accepts a few
wildcards:
Copy link
Contributor

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
Copy link
Contributor

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?

Copy link
Contributor Author

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.

@barthisrael barthisrael marked this pull request as draft August 11, 2023 10:51
```
pg_backup_api/__main__.py:87:1: W391 blank line at end of file
```

Signed-off-by: Israel Barth Rubio <[email protected]>
@barthisrael barthisrael marked this pull request as ready for review August 11, 2023 12:05
Copy link
Contributor

@mikewallace1979 mikewallace1979 left a 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.

@barthisrael barthisrael merged commit b4a7d1f into main Aug 11, 2023
2 checks passed
@barthisrael barthisrael deleted the studying/israel branch August 11, 2023 13:13
@gonzalemario
Copy link
Contributor

I really wished we could have squashed down before merging.

@barthisrael
Copy link
Contributor Author

barthisrael commented Aug 11, 2023

squash

@gonzalemario I guess you are mixing the PRs. This one was actually squashed into b4a7d1f

The one that is showing more commits on main is #82, which you mentioned about using "rebase and merge" to avoid having a merge commit -- maybe we should have used "squash and merge" for that one too :)

@gonzalemario
Copy link
Contributor

Yeah. I wasn't explicit enough about the rebase, sorry.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants