Skip to content

Commit

Permalink
Fix errors and issues picked up by type checking
Browse files Browse the repository at this point in the history
There was a handful of issues where static type
checking either picked up errors or ran into
code that could not verified by the mypy static
type checking. Specifically:

1. in configure.py: Fix a typo in logging (changing
"eror" to "error" that was not covered in our pytest
test cases. This would have resulted in an error
had we hit this condition.

2. in __init__.py: We now always return an integer
from main (so we sys.exit 0 if everything works).
While None being passed into sys.exit isn't an
error in python, it does break static type checking.

3. in __init__.py: We also avoid reusing the variable
name "output" for the subrocess output (bytes) and
the cats output type that goes into the subprocess.
While this isn't an error in python, it does break
static type checking.
  • Loading branch information
andreww committed Apr 17, 2024
1 parent 95bd73c commit a724229
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
5 changes: 3 additions & 2 deletions cats/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ def to_json(self, dateformat: str = "", **kwargs) -> str:
def schedule_at(output: CATSOutput, args: list[str]) -> None:
"Schedule job with optimal start time using at(1)"
proc = subprocess.Popen(args, stdout=subprocess.PIPE)
output = subprocess.check_output(
proc_output = subprocess.check_output(
(
"at",
"-t",
Expand All @@ -205,7 +205,7 @@ def schedule_at(output: CATSOutput, args: list[str]) -> None:
)


def main(arguments=None) -> Optional[int]:
def main(arguments=None) -> int:
parser = parse_arguments()
args = parser.parse_args(arguments)
if args.command and not args.scheduler:
Expand Down Expand Up @@ -263,6 +263,7 @@ def main(arguments=None) -> Optional[int]:
print(output)
if args.command and args.scheduler == "at":
schedule_at(output, args.command.split())
return 0


if __name__ == "__main__":
Expand Down
5 changes: 3 additions & 2 deletions cats/configure.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def get_runtime_config(args) -> tuple[dict, APIInterface, str, int]:
try:
duration = int(args.duration)
except ValueError:
logging.eror(msg)
logging.error(msg)
raise ValueError
if duration <= 0:
logging.error(msg)
Expand Down Expand Up @@ -91,13 +91,14 @@ def CI_API_from_config_or_args(args, config) -> APIInterface:
api = "carbonintensity.org.uk" # default value
logging.warning(f"Unspecified carbon intensity forecast service, using {api}")
try:
return API_interfaces[api]
interface = API_interfaces[api]
except KeyError:
logging.error(
f"Error: {api} is not a valid API choice. It must be one of " "\n".join(
API_interfaces.keys()
)
)
return interface


def get_location_from_config_or_args(args, config) -> str:
Expand Down

0 comments on commit a724229

Please sign in to comment.