Skip to content

Commit

Permalink
Improve type annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
nat-n committed Mar 22, 2023
1 parent 5f6c2b3 commit b4e3ac0
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 12 deletions.
6 changes: 4 additions & 2 deletions poethepoet/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,8 @@ def print_help(
from .task.args import PoeTaskArgs

if isinstance(error, str):
error == PoeException(error)
error = PoeException(error)

tasks_help: Dict[str, Tuple[str, Sequence[Tuple[Tuple[str, ...], str]]]] = {
task_name: (
(
Expand All @@ -185,4 +186,5 @@ def print_help(
)
for task_name, content in self.config.tasks.items()
}
self.ui.print_help(tasks=tasks_help, info=info, error=error) # type: ignore

self.ui.print_help(tasks=tasks_help, info=info, error=error)
7 changes: 7 additions & 0 deletions poethepoet/exceptions.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
from typing import Optional


class PoeException(RuntimeError):
cause: Optional[str]

def __init__(self, msg, *args):
self.msg = msg
self.cause = args[0].args[0] if args else None
Expand All @@ -14,6 +19,8 @@ class ExpressionParseError(PoeException):


class ExecutionError(RuntimeError):
cause: Optional[str]

def __init__(self, msg, *args):
self.msg = msg
self.cause = args[0].args[0] if args else None
Expand Down
2 changes: 1 addition & 1 deletion poethepoet/helpers/python.py
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ def _get_name_source_segment(source: str, node: ast.Name):
performant in common cases.
"""
if sys.version_info.minor >= 8:
return ast.get_source_segment(source, node) # type: ignore
return ast.get_source_segment(source, node)

partial_result = (
re.split(r"(?:\r\n|\r|\n)", source)[node.lineno - 1]
Expand Down
19 changes: 11 additions & 8 deletions poethepoet/ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from typing import IO, TYPE_CHECKING, List, Mapping, Optional, Sequence, Tuple, Union

from .__version__ import __version__
from .exceptions import PoeException
from .exceptions import ExecutionError, PoeException

if TYPE_CHECKING:
from argparse import ArgumentParser, Namespace
Expand Down Expand Up @@ -142,7 +142,9 @@ def set_default_verbosity(self, default_verbosity: int):

def print_help(
self,
tasks: Optional[Mapping[str, Tuple[str, Sequence[Tuple[str, str]]]]] = None,
tasks: Optional[
Mapping[str, Tuple[str, Sequence[Tuple[Tuple[str, ...], str]]]]
] = None,
info: Optional[str] = None,
error: Optional[PoeException] = None,
):
Expand All @@ -165,9 +167,10 @@ def print_help(

if error:
# TODO: send this to stderr instead?
result.append([f"<error>Error: {error.msg} </error>"])
error_line = [f"<error>Error: {error.msg} </error>"]
if error.cause:
result[-1].append(f"<error> From: {error.cause} </error>") # type: ignore
error_line.append(f"<error> From: {error.cause} </error>")
result.append(error_line)

if verbosity >= 0:
# Use argparse for usage summary
Expand Down Expand Up @@ -233,10 +236,10 @@ def print_msg(self, message: str, verbosity=0, end="\n"):
if verbosity <= self.verbosity:
self._print(message, end=end)

def print_error(self, error: Exception):
self._print(f"<error>Error: {error.msg} </error>") # type: ignore
if error.cause: # type: ignore
self._print(f"<error> From: {error.cause} </error>") # type: ignore
def print_error(self, error: Union[PoeException, ExecutionError]):
self._print(f"<error>Error: {error.msg} </error>")
if error.cause:
self._print(f"<error> From: {error.cause} </error>")

def print_version(self):
if self.verbosity >= 0:
Expand Down
4 changes: 3 additions & 1 deletion poethepoet/virtualenv.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,9 @@ def valid(self) -> bool:
and bin_dir.joinpath("python").is_file()
and bool(
next(
self.path.glob(os.path.sep.join(("lib", "python3*", "site-packages"))), # type: ignore
self.path.glob(
os.path.sep.join(("lib", "python3*", "site-packages"))
),
False,
)
)
Expand Down

0 comments on commit b4e3ac0

Please sign in to comment.