Skip to content

Commit 9ed2444

Browse files
committed
Improved some type definitions
Fixes #29 Signed-off-by: Pedro Algarvio <[email protected]>
1 parent 6e0d62e commit 9ed2444

File tree

4 files changed

+34
-26
lines changed

4 files changed

+34
-26
lines changed

changelog/29.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Improved some type definitions

src/ptscripts/parser.py

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@
2222
from typing import Any
2323
from typing import cast
2424
from typing import ContextManager
25+
from typing import NoReturn
2526
from typing import TYPE_CHECKING
26-
from typing import TypedDict
2727

2828
import requests
2929
import rich
@@ -35,6 +35,11 @@
3535
from ptscripts.virtualenv import VirtualEnv
3636
from ptscripts.virtualenv import VirtualEnvConfig
3737

38+
if sys.version_info < (3, 11):
39+
from typing_extensions import TypedDict, NotRequired
40+
else:
41+
from typing import TypedDict, NotRequired
42+
3843
try:
3944
import importlib.metadata
4045

@@ -56,14 +61,15 @@ class ArgumentOptions(TypedDict):
5661
TypedDict class documenting the acceptable keys and their types for arguments.
5762
"""
5863

59-
flags: list[str]
60-
help: str
61-
action: str | argparse.Action
62-
nargs: int | str
63-
const: Any
64-
choices: list[str]
65-
required: bool
66-
metavar: str
64+
help: NotRequired[str]
65+
flags: NotRequired[list[str]]
66+
action: NotRequired[str | argparse.Action]
67+
nargs: NotRequired[int | str]
68+
const: NotRequired[Any]
69+
choices: NotRequired[list[str] | tuple[str, ...]]
70+
required: NotRequired[bool]
71+
metavar: NotRequired[str]
72+
default: NotRequired[Any]
6773

6874

6975
class FullArgumentOptions(ArgumentOptions):
@@ -73,7 +79,6 @@ class FullArgumentOptions(ArgumentOptions):
7379

7480
dest: str
7581
type: type[Any]
76-
default: Any
7782

7883

7984
class Context:
@@ -137,7 +142,7 @@ def error(self, *args):
137142
"""
138143
self.console.log(*args, style="log-error", _stack_offset=2)
139144

140-
def exit(self, status=0, message=None):
145+
def exit(self, status=0, message=None) -> NoReturn: # type: ignore[misc]
141146
"""
142147
Exit the command execution.
143148
"""
@@ -158,7 +163,7 @@ def _run(
158163
capture: bool = False,
159164
interactive: bool = False,
160165
**kwargs,
161-
) -> CompletedProcess[str]:
166+
) -> CompletedProcess[bytes]:
162167
"""
163168
Run a subprocess.
164169
"""
@@ -181,7 +186,7 @@ def run(
181186
capture: bool = False,
182187
interactive: bool = False,
183188
**kwargs,
184-
) -> CompletedProcess[str] | None:
189+
) -> CompletedProcess[bytes]:
185190
"""
186191
Run a subprocess.
187192
@@ -208,9 +213,11 @@ def run(
208213
**kwargs,
209214
)
210215
except subprocess.CalledProcessError as exc:
211-
self.error(str(exc))
212-
self.exit(exc.returncode)
213-
return None
216+
self._exit(str(exc), exc.returncode)
217+
218+
def _exit(self, msg: str, returncode: int) -> NoReturn:
219+
self.error(msg)
220+
self.exit(returncode)
214221

215222
@contextmanager
216223
def chdir(self, path: pathlib.Path) -> Iterator[pathlib.Path]:
@@ -653,7 +660,7 @@ def command(
653660
if not kwargs["help"].endswith("."):
654661
kwargs["help"] += "."
655662
kwargs["help"] += " [default: %(default)s]"
656-
flags = kwargs.pop("flags", None) # type: ignore[misc]
663+
flags = kwargs.pop("flags", None)
657664
if flags is None:
658665
flags = [f"--{parameter.name.replace('_', '-')}"]
659666
log.debug("Adding Command %r. Flags: %s; KwArgs: %s", name, flags, kwargs)
@@ -708,7 +715,7 @@ def command_group(
708715
help: str,
709716
description: str | None = None,
710717
venv_config: VirtualEnvConfig | None = None,
711-
parent: CommandGroup | None = None,
718+
parent: CommandGroup | str | list[str] | tuple[str] | None = None,
712719
) -> CommandGroup:
713720
"""
714721
Create a new command group.

src/ptscripts/process.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ def run(
254254
capture: bool = False,
255255
interactive: bool = False,
256256
**kwargs,
257-
) -> subprocess.CompletedProcess[str]:
257+
) -> subprocess.CompletedProcess[bytes]:
258258
"""
259259
Run a command.
260260
"""
@@ -276,7 +276,7 @@ def run(
276276
result = future.result()
277277
if check is True:
278278
result.check_returncode()
279-
return cast(subprocess.CompletedProcess[str], result)
279+
return cast(subprocess.CompletedProcess[bytes], result)
280280
finally:
281281
loop.run_until_complete(loop.shutdown_asyncgens())
282282
loop.close()

src/ptscripts/virtualenv.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@
1010
import subprocess
1111
import sys
1212
import textwrap
13+
from subprocess import CompletedProcess
1314
from typing import TYPE_CHECKING
14-
from typing import TypedDict
1515

16-
try:
17-
from typing import NotRequired # type: ignore[attr-defined]
18-
except ImportError:
19-
from typing_extensions import NotRequired
16+
if sys.version_info < (3, 11):
17+
from typing_extensions import TypedDict, NotRequired
18+
else:
19+
from typing import TypedDict, NotRequired
2020

2121
import attr
2222

@@ -227,7 +227,7 @@ def uninstall(self, *args, **kwargs):
227227
"""
228228
return self.run(self.venv_python, "-m", "pip", "uninstall", "-y", *args, **kwargs)
229229

230-
def run(self, *args, **kwargs):
230+
def run(self, *args, **kwargs) -> CompletedProcess[bytes]:
231231
"""
232232
Run a command in the context of the virtual environment.
233233
"""

0 commit comments

Comments
 (0)