ref: typehints, enum instead of raw string, abc and other (#1)

Full code coverage with annotations, fixing errors in various linters: ruff, wps, etc. Fixing errors in type checkers: ty, mypy, pyright. Formatting and bringing code to a consistent style, applying best practices in various aspects.
This commit is contained in:
kolo
2025-10-08 13:37:31 +03:00
committed by GitHub
parent 22f1171192
commit 73303b1c08
45 changed files with 983 additions and 996 deletions
@@ -5,10 +5,10 @@ import io
import re
from argenta.app import App
from argenta.command import Command
from argenta.command import Command, PredefinedFlags
from argenta.command.flag.models import ValidationStatus
from argenta.router import Router
from argenta.command.flag.flags.models import Flags
from argenta.command.flag.defaults import PredefinedFlags
from argenta.orchestrator import Orchestrator
from argenta.response import Response
@@ -22,13 +22,13 @@ class TestSystemHandlerNormalWork(TestCase):
orchestrator = Orchestrator()
@router.command(Command('test'))
def test(response: Response):
def test(response: Response) -> None: # pyright: ignore[reportUnusedFunction]
print('test command')
app = App(override_system_messages=True,
print_func=print)
app.include_router(router)
app.set_unknown_command_handler(lambda command: print(f'Unknown command: {command.get_trigger()}'))
app.set_unknown_command_handler(lambda command: print(f'Unknown command: {command.trigger}'))
orchestrator.start_polling(app)
output = mock_stdout.getvalue()
@@ -43,14 +43,14 @@ class TestSystemHandlerNormalWork(TestCase):
orchestrator = Orchestrator()
@router.command(Command('test'))
def test(response: Response):
def test(response: Response) -> None: # pyright: ignore[reportUnusedFunction]
print('test command')
app = App(ignore_command_register=False,
override_system_messages=True,
print_func=print)
app.include_router(router)
app.set_unknown_command_handler(lambda command: print(f'Unknown command: {command.get_trigger()}'))
app.set_unknown_command_handler(lambda command: print(f'Unknown command: {command.trigger}'))
orchestrator.start_polling(app)
output = mock_stdout.getvalue()
@@ -65,8 +65,10 @@ class TestSystemHandlerNormalWork(TestCase):
orchestrator = Orchestrator()
@router.command(Command('test'))
def test(response: Response):
print(f'test command with undefined flag: {response.undefined_flags.get_flag('help').get_string_entity()}')
def test(response: Response) -> None: # pyright: ignore[reportUnusedFunction]
undefined_flag = response.input_flags.get_flag_by_name('help')
if undefined_flag and undefined_flag.status == ValidationStatus.UNDEFINED:
print(f'test command with undefined flag: {undefined_flag.string_entity}')
app = App(override_system_messages=True,
print_func=print)
@@ -85,9 +87,12 @@ class TestSystemHandlerNormalWork(TestCase):
orchestrator = Orchestrator()
@router.command(Command('test'))
def test(response: Response):
flag = response.undefined_flags.get_flag("port")
print(f'test command with undefined flag with value: {flag.get_string_entity()} {flag.get_value()}')
def test(response: Response) -> None: # pyright: ignore[reportUnusedFunction]
undefined_flag = response.input_flags.get_flag_by_name("port")
if undefined_flag and undefined_flag.status == ValidationStatus.UNDEFINED:
print(f'test command with undefined flag with value: {undefined_flag.string_entity} {undefined_flag.input_value}')
else:
raise
app = App(override_system_messages=True,
print_func=print)
@@ -104,12 +109,13 @@ class TestSystemHandlerNormalWork(TestCase):
def test_input_correct_command_with_one_correct_flag_an_one_incorrect_flag(self, mock_stdout: _io.StringIO, magick_mock: MagicMock):
router = Router()
orchestrator = Orchestrator()
flags = Flags(PredefinedFlags.HOST)
flags = Flags([PredefinedFlags.HOST])
@router.command(Command('test', flags=flags))
def test(response: Response):
flag = response.undefined_flags.get_flag("port")
print(f'connecting to host with flag: {flag.get_string_entity()} {flag.get_value()}')
def test(response: Response) -> None: # pyright: ignore[reportUnusedFunction]
undefined_flag = response.input_flags.get_flag_by_name("port")
if undefined_flag and undefined_flag.status == ValidationStatus.UNDEFINED:
print(f'connecting to host with flag: {undefined_flag.string_entity} {undefined_flag.input_value}')
app = App(override_system_messages=True,
print_func=print)
@@ -128,13 +134,13 @@ class TestSystemHandlerNormalWork(TestCase):
orchestrator = Orchestrator()
@router.command(Command('test'))
def test(response: Response):
def test(response: Response) -> None: # pyright: ignore[reportUnusedFunction]
print(f'test command')
app = App(override_system_messages=True,
print_func=print)
app.include_router(router)
app.set_unknown_command_handler(lambda command: print(f'Unknown command: {command.get_trigger()}'))
app.set_unknown_command_handler(lambda command: print(f'Unknown command: {command.trigger}'))
orchestrator.start_polling(app)
output = mock_stdout.getvalue()
@@ -149,17 +155,17 @@ class TestSystemHandlerNormalWork(TestCase):
orchestrator = Orchestrator()
@router.command(Command('test'))
def test(response: Response):
def test(response: Response) -> None: # pyright: ignore[reportUnusedFunction]
print(f'test command')
@router.command(Command('more'))
def test(response: Response):
def test1(response: Response) -> None: # pyright: ignore[reportUnusedFunction]
print(f'more command')
app = App(override_system_messages=True,
print_func=print)
app.include_router(router)
app.set_unknown_command_handler(lambda command: print(f'Unknown command: {command.get_trigger()}'))
app.set_unknown_command_handler(lambda command: print(f'Unknown command: {command.trigger}'))
orchestrator.start_polling(app)
output = mock_stdout.getvalue()
@@ -174,7 +180,7 @@ class TestSystemHandlerNormalWork(TestCase):
orchestrator = Orchestrator()
@router.command(Command('test'))
def test(response: Response):
def test(response: Response) -> None: # pyright: ignore[reportUnusedFunction]
print(f'test command')
app = App(override_system_messages=True,
@@ -195,7 +201,7 @@ class TestSystemHandlerNormalWork(TestCase):
orchestrator = Orchestrator()
@router.command(Command('test'))
def test(response: Response):
def test(response: Response) -> None: # pyright: ignore[reportUnusedFunction]
print(f'test command')
app = App(override_system_messages=True,
@@ -216,7 +222,7 @@ class TestSystemHandlerNormalWork(TestCase):
orchestrator = Orchestrator()
@router.command(Command('test', flags=PredefinedFlags.PORT))
def test(response: Response):
def test(response: Response) -> None: # pyright: ignore[reportUnusedFunction]
print('test command')
app = App(override_system_messages=True,
@@ -227,4 +233,25 @@ class TestSystemHandlerNormalWork(TestCase):
output = mock_stdout.getvalue()
self.assertIn("\nRepeated input flags: \"test --port 22 --port 33\"\n", output)
self.assertIn('Repeated input flags: "test --port 22 --port 33"', output)
@patch("builtins.input", side_effect=["test --help", "q"])
@patch("sys.stdout", new_callable=io.StringIO)
def test_input_correct_command_with_unregistered_flag3(self, mock_stdout: _io.StringIO, magick_mock: MagicMock):
router = Router()
orchestrator = Orchestrator()
@router.command(Command('test'))
def test(response: Response) -> None: # pyright: ignore[reportUnusedFunction]
undefined_flag = response.input_flags.get_flag_by_name('help')
if undefined_flag and undefined_flag.status == ValidationStatus.UNDEFINED:
print(f'test command with undefined flag: {undefined_flag.string_entity}')
app = App(override_system_messages=True,
print_func=print)
app.include_router(router)
orchestrator.start_polling(app)
output = mock_stdout.getvalue()
self.assertIn('\ntest command with undefined flag: --help\n', output)
@@ -5,13 +5,13 @@ import io
import re
from argenta.app import App
from argenta.command import Command
from argenta.command import Command, PredefinedFlags
from argenta.command.flag.models import PossibleValues, ValidationStatus
from argenta.response import Response
from argenta.router import Router
from argenta.orchestrator import Orchestrator
from argenta.command.flag import Flag
from argenta.command.flag.flags import Flags
from argenta.command.flag.defaults import PredefinedFlags
@@ -23,7 +23,7 @@ class TestSystemHandlerNormalWork(TestCase):
orchestrator = Orchestrator()
@router.command(Command('test'))
def test(response):
def test(response: Response) -> None: # pyright: ignore[reportUnusedFunction]
print('test command')
app = App(override_system_messages=True,
@@ -43,7 +43,7 @@ class TestSystemHandlerNormalWork(TestCase):
orchestrator = Orchestrator()
@router.command(Command('test'))
def test(response):
def test(response: Response) -> None: # pyright: ignore[reportUnusedFunction]
print('test command')
app = App(ignore_command_register=True,
@@ -62,11 +62,13 @@ class TestSystemHandlerNormalWork(TestCase):
def test_input_correct_command_with_custom_flag(self, mock_stdout: _io.StringIO, magick_mock: MagicMock):
router = Router()
orchestrator = Orchestrator()
flag = Flag('help', '--', False)
flag = Flag('help', prefix='--', possible_values=PossibleValues.NEITHER)
@router.command(Command('test', flags=flag))
def test(response: Response):
print(f'\nhelp for {response.valid_flags.get_flag('help').get_name()} flag\n')
def test(response: Response) -> None: # pyright: ignore[reportUnusedFunction]
valid_flag = response.input_flags.get_flag_by_name('help')
if valid_flag and valid_flag.status == ValidationStatus.VALID:
print(f'\nhelp for {valid_flag.name} flag\n')
app = App(override_system_messages=True,
print_func=print)
@@ -82,12 +84,13 @@ class TestSystemHandlerNormalWork(TestCase):
def test_input_correct_command_with_custom_flag2(self, mock_stdout: _io.StringIO, magick_mock: MagicMock):
router = Router()
orchestrator = Orchestrator()
flag = Flag('port', '--', re.compile(r'^\d{1,5}$'))
flag = Flag('port', prefix='--', possible_values=re.compile(r'^\d{1,5}$'))
@router.command(Command('test', flags=flag))
def test(response: Response):
input_flag = response.valid_flags.get_flag('port')
print(f'flag value for {input_flag.get_name()} flag : {input_flag.get_value()}')
def test(response: Response) -> None: # pyright: ignore[reportUnusedFunction]
valid_flag = response.input_flags.get_flag_by_name('port')
if valid_flag and valid_flag.status == ValidationStatus.VALID:
print(f'flag value for {valid_flag.name} flag : {valid_flag.input_value}')
app = App(override_system_messages=True,
print_func=print)
@@ -107,8 +110,10 @@ class TestSystemHandlerNormalWork(TestCase):
flag = PredefinedFlags.SHORT_HELP
@router.command(Command('test', flags=flag))
def test(response: Response):
print(f'help for {response.valid_flags.get_flag('H').get_name()} flag')
def test(response: Response) -> None: # pyright: ignore[reportUnusedFunction]
valid_flag = response.input_flags.get_flag_by_name('H')
if valid_flag and valid_flag.status == ValidationStatus.VALID:
print(f'help for {valid_flag.name} flag')
app = App(override_system_messages=True,
print_func=print)
@@ -128,8 +133,9 @@ class TestSystemHandlerNormalWork(TestCase):
flag = PredefinedFlags.INFO
@router.command(Command('test', flags=flag))
def test(response: Response):
if response.valid_flags.get_flag('info'):
def test(response: Response) -> None: # pyright: ignore[reportUnusedFunction]
valid_flag = response.input_flags.get_flag_by_name('info')
if valid_flag and valid_flag.status == ValidationStatus.VALID:
print('info about test command')
app = App(override_system_messages=True,
@@ -150,8 +156,10 @@ class TestSystemHandlerNormalWork(TestCase):
flag = PredefinedFlags.HOST
@router.command(Command('test', flags=flag))
def test(response: Response):
print(f'connecting to host {response.valid_flags.get_flag('host').get_value()}')
def test(response: Response) -> None: # pyright: ignore[reportUnusedFunction]
valid_flag = response.input_flags.get_flag_by_name('host')
if valid_flag and valid_flag.status == ValidationStatus.VALID:
print(f'connecting to host {valid_flag.input_value}')
app = App(override_system_messages=True,
print_func=print)
@@ -168,12 +176,14 @@ class TestSystemHandlerNormalWork(TestCase):
def test_input_correct_command_with_two_flags(self, mock_stdout: _io.StringIO, magick_mock: MagicMock):
router = Router()
orchestrator = Orchestrator()
flags = Flags(PredefinedFlags.HOST, PredefinedFlags.PORT)
flags = Flags([PredefinedFlags.HOST, PredefinedFlags.PORT])
@router.command(Command('test', flags=flags))
def test(response: Response):
valid_flags = response.valid_flags
print(f'connecting to host {valid_flags.get_flag('host').get_value()} and port {valid_flags.get_flag('port').get_value()}')
def test(response: Response) -> None: # pyright: ignore[reportUnusedFunction]
host_flag = response.input_flags.get_flag_by_name('host')
port_flag = response.input_flags.get_flag_by_name('port')
if (host_flag and host_flag.status == ValidationStatus.VALID) and (port_flag and port_flag.status == ValidationStatus.VALID):
print(f'connecting to host {host_flag.input_value} and port {port_flag.input_value}')
app = App(override_system_messages=True,
print_func=print)
@@ -192,11 +202,11 @@ class TestSystemHandlerNormalWork(TestCase):
orchestrator = Orchestrator()
@router.command(Command('test'))
def test(response):
def test(response: Response) -> None: # pyright: ignore[reportUnusedFunction]
print(f'test command')
@router.command(Command('some'))
def test2(response):
def test2(response: Response) -> None: # pyright: ignore[reportUnusedFunction]
print(f'some command')
app = App(override_system_messages=True,
@@ -216,15 +226,15 @@ class TestSystemHandlerNormalWork(TestCase):
orchestrator = Orchestrator()
@router.command(Command('test'))
def test(response):
def test(response: Response) -> None: # pyright: ignore[reportUnusedFunction]
print(f'test command')
@router.command(Command('some'))
def test(response):
def test1(response: Response) -> None: # pyright: ignore[reportUnusedFunction]
print(f'some command')
@router.command(Command('more'))
def test(response):
def test2(response: Response) -> None: # pyright: ignore[reportUnusedFunction]
print(f'more command')
app = App(override_system_messages=True,