Skip to content

Commit d74e2bb

Browse files
committed
WIP
1 parent 372e2a1 commit d74e2bb

File tree

3 files changed

+33
-10
lines changed

3 files changed

+33
-10
lines changed

mininterface/_lib/cli_parser.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
# CLI and config file parsing.
33
#
44
from dataclasses import asdict
5+
from functools import reduce
56
import sys
67
from collections import deque
78
from contextlib import ExitStack
@@ -137,9 +138,17 @@ def parse_cli(
137138
)
138139
if cond
139140
]
141+
140142
def annot(type_form):
141143
if annotations:
142-
return Annotated[type_form, *annotations]
144+
if sys.version_info >= (3, 11):
145+
from .future_compatibility import spread_annotated
146+
147+
return spread_annotated(type_form, annotations)
148+
else:
149+
from warnings import warn
150+
151+
warn(f"Cannot apply {annotations} on Python <= 3.11.")
143152
return type_form
144153

145154
try:
@@ -165,7 +174,9 @@ def annot(type_form):
165174
m.env = env
166175
except BaseException as exception:
167176
if ask_for_missing and getattr(exception, "code", None) == 2 and failed_fields.get():
168-
env = _dialog_missing(env_classes, kwargs, m, cf, ask_for_missing, args, cli_settings, _crawled, _req_fields)
177+
env = _dialog_missing(
178+
env_classes, kwargs, m, cf, ask_for_missing, args, cli_settings, _crawled, _req_fields
179+
)
169180

170181
if final_call:
171182
# Ask for the wrong fields
Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11

2-
from typing import Literal
2+
from typing import Annotated, Literal
33

44

55
def literal(c):
6-
return Literal[*c]
6+
return Literal[*c]
7+
8+
def spread_annotated(obj, annotations):
9+
return Annotated[obj, *annotations]

tests/test_subcommands.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -451,12 +451,21 @@ def test_upper_command(self):
451451
)
452452

453453
# omitting the prefixes can be set via CLI
454-
with self.assertForms(*args):
455-
env = runm(UpperCommandA, args=["upper-command2", "run"], settings=CliSettings(omit_subcommand_prefixes=True)).env
456-
self.assertEqual(
457-
"""UpperCommandA(command=UpperCommand2(command=Run(bot_id='id-two', _subcommands=Message(kind='pop', msg=None, foo='hello'))))""",
458-
repr(env),
459-
)
454+
# This does not work in Python3.10
455+
if sys.version_info[:2] >= (3, 11):
456+
with self.assertForms(*args):
457+
env = runm(UpperCommandA, args=["upper-command2", "run"], settings=CliSettings(omit_subcommand_prefixes=True)).env
458+
self.assertEqual(
459+
"""UpperCommandA(command=UpperCommand2(command=Run(bot_id='id-two', _subcommands=Message(kind='pop', msg=None, foo='hello'))))""",
460+
repr(env),
461+
)
462+
else:
463+
with self.assertWarns(UserWarning) as cm:
464+
with self.assertForms(*args):
465+
env = runm(UpperCommandA, args=["command:upper-command2", "command.command:run"], settings=CliSettings(omit_subcommand_prefixes=True)).env
466+
467+
# # cm je context manager, můžeš zkontrolovat zprávu
468+
self.assertIn("Cannot apply", str(cm.warning))
460469

461470
def test_run_arg(self):
462471
with self.assertForms(

0 commit comments

Comments
 (0)