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 some type hints and docstrings for app.py #677

Open
wants to merge 4 commits into
base: devel
Choose a base branch
from

Conversation

yuvipanda
Copy link
Contributor

@yuvipanda yuvipanda commented Jan 2, 2023

I loved reading
https://github.com/asfadmin/thin-egress-app/blob/master/docs/vision.md! Hopefully this PR is useful and not just noise.

  • I added type hints for returns and function parameters wherever I could determine them.
    I couldn't figure out how exactly to type a return for the S3 botocore client, but otherwise
    I think this is accurate.
  • I also added docstrings for the majority of functions after reading through the code to understand
    what it's doing.

@yuvipanda yuvipanda changed the base branch from master to devel January 3, 2023 01:15
@yuvipanda yuvipanda changed the base branch from devel to master January 3, 2023 01:16
@yuvipanda
Copy link
Contributor Author

Not sure if this should target master or devel?

@reweeden
Copy link
Contributor

reweeden commented Jan 3, 2023

Hey @yuvipanda, thanks for the PR! The way we have the repo set up right now, we make PR's into devel first, so if you could change the base branch that would be great.

I loved reading
https://github.com/asfadmin/thin-egress-app/blob/master/docs/vision.md!
Hopefully this PR is useful and not just noise.

I couldn't figure out how exactly to type a return for the S3 botocore
client, but otherwise I think this is accurate.
@yuvipanda yuvipanda changed the base branch from master to devel January 4, 2023 18:34
@yuvipanda
Copy link
Contributor Author

Great! git macigck'd and base changed :)

mckadesorensen
mckadesorensen previously approved these changes Jan 4, 2023
@yuvipanda yuvipanda changed the title Add more return type hints for app.py Add some type hints and docstrings for app.py Jan 7, 2023
@yuvipanda
Copy link
Contributor Author

@mckadesorensen sorry to change the PR after you had already reviewed it! I added some more docstrings as I read through the code to help me understand what was going on :) Hope that is useful too!

@yuvipanda
Copy link
Contributor Author

Also lmk what else I can do to help get this merged!

Copy link
Contributor

@reweeden reweeden left a comment

Choose a reason for hiding this comment

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

There's 1 line that becomes too long when adding the return type annotation. We'll need to fix that before being able to merge (see github actions).

Technically our coding standards say that docstrings should follow this format:

"""A multi line docstring has a one-line summary, less than 120 characters.

Then a new paragraph after a newline that explains in more detail any
general information about the function, class or method. Example usages
are also great to have here if it is a complex class or function.

When writing the docstring for a class, an extra line should be placed
after the closing quotations. For more in-depth explanations for these
decisions see <http://www.python.org/dev/peps/pep-0257/>

If you are going to describe parameters and return values, use Sphinx, the
appropriate syntax is as follows.

:param foo: the foo parameter
:param bar: the bar parameter
:returns: return_type -- description of the return value
:returns: description of the return value
:raises: AttributeError, KeyError
"""

This is straight from the OpenStack style guide.

Comment on lines +27 to +28
# If opentelemetry is not installed, we make dummy objects / functions here
# so we do not need to add conditionls throughout our codebase
Copy link
Contributor

Choose a reason for hiding this comment

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

Personally, I prefer to err on the side of having fewer comments because of the risk of them becoming out of date and straight up wrong over time. To me the 'except ImportError and replace with None' pattern seems standard enough that it doesn't warrant a comment. What do others think in this case?

Copy link
Contributor

Choose a reason for hiding this comment

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

I agree with @reweeden on this comment. I have been burned too many times by people not updating comments when they update 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.

My personal experience has been that the more common phenomena is lack of comments rather than wrong comments, and usually this 'mismatch' issue is dealt with during code review of the changing code itself rather than by not having comments at all.

And as for this specific pattern, I've personally seen a different pattern (detailed in https://adamj.eu/tech/2021/12/29/python-type-hints-optional-imports/) be used more often. It also has the advantage of working better with mypy and other type checkers, rather than simply setting to None. This was the primary reason I added the comment here.

However, y'all are the maintainers so are ultimately responsible for the state of the code. So I'll happily remove the comment if you think that's what's best for this project.

Thanks for your consideration!

Copy link
Contributor

Choose a reason for hiding this comment

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

Unfortunately I don't think code reviews can be relied upon to catch comment mismatches. Stuff like that slips through unnoticed all the time which is the nature of the problem.

I'd say, lets go for no comment in this case.

assert app.lambda_context is not None

return app.lambda_context.aws_request_id


@with_trace()
def get_origin_request_id() -> Optional[str]:
"""
Return the *original* AWS Lambda Request ID
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm actually not sure what x-origin-request-id is. But I believe 'origin' has a specific meaning in HTTP and I think AWS is using it in a similar way.

@@ -367,12 +420,18 @@ def get_user_from_token(token):

@with_trace()
def cumulus_log_message(outcome: str, code: int, http_method: str, k_v: dict):
"""
Emit log message to stdout in a format that cumulus understands
Copy link
Contributor

Choose a reason for hiding this comment

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

Does anyone know if this actually interacts with cumulus at all?

Copy link
Contributor

Choose a reason for hiding this comment

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

@benbart Might know the answer to that.

Copy link
Contributor

Choose a reason for hiding this comment

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

More like cumulus interacts with the log message generated. This outputs the log message in a format that cumulus likes, if memory serves. The Issue #275 isn't very descriptive on this matter.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

My understanding would be that there's some elasticsearch / kibana rule that is looking for log messages in this format to expose specially, and this is writing things out in that format. If you think a different kinda comment would be more clarifying here, I can do that.

lambda/app.py Outdated Show resolved Hide resolved
@yuvipanda
Copy link
Contributor Author

Thank you for the comments! I'm going to try to come back to this whenever I can, and do bits and pieces whenever I get the time :)

@yuvipanda
Copy link
Contributor Author

@mckadesorensen re: docstrings, my hope is that adding some is better than none. I often find it difficult to write param dosctrings, as they often are repetitive. However, if you would like me to make sure all docstrings are 'complete', I can try (although would prefer not to haha).

@reweeden
Copy link
Contributor

reweeden commented Jan 23, 2023

Params in docstrings are optional. Just format what you have so it matches the example and that will be fine.

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