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

Use mypy annotations #9

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

bergercookie
Copy link
Contributor

@bergercookie bergercookie commented Nov 9, 2020

Since colcon packages mandate Pytyhon 3.5 or higher, we can use explicit mypy annotations to document function parameters and output types instead of docstrings. I'm also removing the docstrings for the types since sphinx can parse and create documentation for mypy types.

Current PR also includes: Fix KeyError exception when dependencies Cargo key is not found (it's tightly coupled with the change in mypy types - hence I can't make a separate PR for it.

Fix error when `dependencies` Cargo key is not found
Co-authored-by: Jeremy Kolb <kjeremy@gmail.com>
import os
import shutil

from colcon_core.environment_variable import EnvironmentVariable

"""Environment variable to override the Cargo executable"""
# Environment variable to override the Cargo executable
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd keep this as docstring

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Depends what you use to generate the documentation, but if we're talking about sphinx, it doesn't support extracting docstrings from variables unfortunately

return os.environ[environment_variable]

which = shutil.which(executable_name)
if which:
Copy link
Contributor

Choose a reason for hiding this comment

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

This already returns None if the executable_name can't be found, so it can be simplified as:

return shutil.which(executable_name)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes, agree, I 'll change it back

if value:
return value
return shutil.which(executable_name)
if environment_variable in os.environ:
Copy link
Contributor

Choose a reason for hiding this comment

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

This change is equivalent to the existing code, why the change?

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 find queyring directly the os.environ more readable in a couple of ways:

  • There's no need for the intermediate value variable, which doesn't say much about the value it holds anyway
  • the environment in python is just a dictionary, so using an os function, getenv , to manipulate it sounds unnecessary since there are already established ways of querying a dictionary.

In any case, it's not terribly important, I can revert the change if you think it was better before.

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

Successfully merging this pull request may close these issues.

3 participants