Skip to content

Commit

Permalink
[pre-commit.ci] auto fixes from pre-commit.com hooks
Browse files Browse the repository at this point in the history
for more information, see https://pre-commit.ci
  • Loading branch information
pre-commit-ci[bot] committed Jul 14, 2024
1 parent e69b3b4 commit a4df1e1
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 20 deletions.
2 changes: 1 addition & 1 deletion dargs/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
from dargs.cli import main

if __name__ == "__main__":
main()
main()
4 changes: 4 additions & 0 deletions dargs/_test.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
from __future__ import annotations

from typing import List

from dargs.dargs import Argument


Expand All @@ -16,6 +19,7 @@ def test_arguments() -> list[Argument]:
),
]


__all__ = [
"test_arguments",
]
44 changes: 30 additions & 14 deletions dargs/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,35 +10,53 @@

def main_parser() -> argparse.ArgumentParser:
"""Create the main parser for the command line interface.
Returns
-------
argparse.ArgumentParser
The main parser
"""
parser = argparse.ArgumentParser(description='dargs: Argument checking for Python programs')
parser = argparse.ArgumentParser(
description="dargs: Argument checking for Python programs"
)
subparsers = parser.add_subparsers(help="Sub-commands")
parser_check = subparsers.add_parser("check", help="Check a JSON file against an Argument")
parser_check.add_argument("func", type=str, help="Function that returns an Argument object. E.g., `dargs._test.test_arguments`")
parser_check.add_argument("jdata", type=argparse.FileType('r'),
default=sys.stdin,
help="Path to the JSON file. If not given, read from stdin.", )
parser_check.add_argument("--no-strict", action="store_false", dest="strict", help="Do not raise an error if the key is not pre-defined")
parser_check = subparsers.add_parser(
"check", help="Check a JSON file against an Argument"
)
parser_check.add_argument(
"func",
type=str,
help="Function that returns an Argument object. E.g., `dargs._test.test_arguments`",
)
parser_check.add_argument(
"jdata",
type=argparse.FileType("r"),
default=sys.stdin,
help="Path to the JSON file. If not given, read from stdin.",
)
parser_check.add_argument(
"--no-strict",
action="store_false",
dest="strict",
help="Do not raise an error if the key is not pre-defined",
)
parser_check.set_defaults(entrypoint=check_cli)

# --version
parser.add_argument("--version", action="version", version=__version__)
return parser


def main():
"""Main entry point for the command line interface."""
parser = main_parser()
args = parser.parse_args()

args.entrypoint(**vars(args))


def check_cli(*,
def check_cli(
*,
func: str,
jdata: argparse.FileType,
strict: bool,
Expand Down Expand Up @@ -69,9 +87,7 @@ def check_cli(*,
)

if not hasattr(mod, attr_name):
raise RuntimeError(
f'Module "{module_name}" has no attribute "{attr_name}"'
)
raise RuntimeError(f'Module "{module_name}" has no attribute "{attr_name}"')
func = getattr(mod, attr_name)
arginfo = func()

Check failure on line 92 in dargs/cli.py

View workflow job for this annotation

GitHub Actions / type_check

Object of type "str" is not callable   Attribute "__call__" is unknown (reportCallIssue)
data = json.load(jdata)

Check failure on line 93 in dargs/cli.py

View workflow job for this annotation

GitHub Actions / type_check

Argument of type "FileType" cannot be assigned to parameter "fp" of type "SupportsRead[str | bytes]" in function "load"   "FileType" is incompatible with protocol "SupportsRead[str | bytes]"     "read" is not present (reportArgumentType)
Expand Down
1 change: 0 additions & 1 deletion dargs/sphinx.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,4 +217,3 @@ def _test_argument() -> Argument:
),
],
)

2 changes: 1 addition & 1 deletion tests/test_arguments.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"test1": 1,
"test2": 2
}
}
24 changes: 21 additions & 3 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,30 @@
from __future__ import annotations

import subprocess
import sys
import unittest

from pathlib import Path

this_directory = Path(__file__).parent


class TestCli(unittest.TestCase):
def test_check(self):
subprocess.check_call(["dargs", "check", "dargs._test.test_arguments", str(this_directory / "test_arguments.json")])
subprocess.check_call([sys.executable, "-m", "dargs", "check", "dargs._test.test_arguments", str(this_directory / "test_arguments.json")])
subprocess.check_call(
[
"dargs",
"check",
"dargs._test.test_arguments",
str(this_directory / "test_arguments.json"),
]
)
subprocess.check_call(
[
sys.executable,
"-m",
"dargs",
"check",
"dargs._test.test_arguments",
str(this_directory / "test_arguments.json"),
]
)

0 comments on commit a4df1e1

Please sign in to comment.