Skip to content

Commit

Permalink
Merge pull request #286 from clumio-oss/typing
Browse files Browse the repository at this point in the history
Add more typing to the codebase and minor fixes.
  • Loading branch information
sodul authored Feb 5, 2024
2 parents 24927c6 + 2d0d157 commit a0e89b8
Show file tree
Hide file tree
Showing 14 changed files with 654 additions and 486 deletions.
8 changes: 6 additions & 2 deletions green/cmdline.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
"""The green command line entry point."""

from __future__ import annotations


Expand Down Expand Up @@ -53,7 +55,8 @@ def _main(argv: Sequence[str] | None, testing: bool) -> int:

# Add debug logging for stuff that happened before this point here
if config.files_loaded:
debug("Loaded config file(s): {}".format(", ".join(config.files_loaded)))
loaded_files = ", ".join(str(path) for path in config.files_loaded)
debug(f"Loaded config file(s): {loaded_files}")

# Discover/Load the test suite
if testing:
Expand Down Expand Up @@ -81,7 +84,7 @@ def _main(argv: Sequence[str] | None, testing: bool) -> int:
return int(not result.wasSuccessful())


def main(argv: Sequence[str] | None = None, testing: bool = False):
def main(argv: Sequence[str] | None = None, testing: bool = False) -> int:
# create the temp dir only once (i.e., not while in the recursed call)
if not os.environ.get("TMPDIR"): # pragma: nocover
try:
Expand All @@ -97,6 +100,7 @@ def main(argv: Sequence[str] | None = None, testing: bool = False):
if os_error.errno == 39:
# "Directory not empty" when trying to delete the temp dir can just be a warning
print(f"warning: {os_error.strerror}")
return 0
else:
raise os_error
else:
Expand Down
41 changes: 29 additions & 12 deletions green/command.py
Original file line number Diff line number Diff line change
@@ -1,46 +1,63 @@
"""Registers the green command with setuptools."""

from __future__ import annotations

import functools
import sys
from typing import TYPE_CHECKING

from setuptools import Command

from green.config import parseArguments
from green.cmdline import main

if TYPE_CHECKING:
from argparse import Action


def get_user_options():
def get_user_options() -> list[tuple[str, str | None, str | None]]:
# When running "python setup.py --help-commands", setup.py will call this
# function -- but green isn't actually being called.
if "--help-commands" in sys.argv:
return []

r = parseArguments()
options = []
args = parseArguments()
options: list[tuple[str, str | None, str | None]] = []

for action in r.store_opt.actions:
names = [str(name.lstrip("-")) for name in action.option_strings]
action: Action
for action in args.store_opt.actions:
names = [name.lstrip("-") for name in action.option_strings]
short_name: str | None
if len(names) == 1:
names.insert(0, None)
full_name = names[0]
short_name = None
else:
# TODO: We might want to pick the longer of the two for full_name.
full_name = names[1]
short_name = names[0]
if not action.const:
names[1] += "="
options.append((names[1], names[0], action.help))
full_name += "="
options.append((full_name, short_name, action.help))

return options


class green(Command):
command_name = "green"
description = "Run unit tests using green"
user_options = get_user_options()

def initialize_options(self):
@functools.cached_property
def user_options(self) -> list[tuple[str, str | None, str | None]]:
return get_user_options()

def initialize_options(self) -> None:
for name, _, _ in self.user_options:
setattr(self, name.replace("-", "_").rstrip("="), None)

def finalize_options(self):
def finalize_options(self) -> None:
pass

def run(self):
def run(self) -> None:
self.ensure_finalized()

if self.distribution.install_requires:
Expand Down
Loading

0 comments on commit a0e89b8

Please sign in to comment.