Skip to content

Commit

Permalink
fix some issues with the types (#41)
Browse files Browse the repository at this point in the history
- fix type error when the `return_completed_process` argument isn't
specified, which was causing it to resolve to the wrong overload:
  ```py
exit_code: int = node(["foo"]) # error: Expression of type "int |
CompletedProcess[str | bytes]" is incompatible with declared type "int"
  ```
- change the type of `args` to `Iterable[str]` instead of `list[str]`,
which is more flexible. for example, when wrapping it with a function
that passes it a tuple:
  ```py
  def run_node(*args: str)
node(args) # error: Argument of type "tuple[str, ...]" cannot be
assigned to parameter "args" of type "list[str] | None"
  ```

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
DetachHead and pre-commit-ci[bot] authored May 13, 2024
1 parent d1b0d74 commit 14a8183
Showing 1 changed file with 48 additions and 18 deletions.
66 changes: 48 additions & 18 deletions nodejs_wheel/executable.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,43 @@
import os
import subprocess
import sys
from typing import Any, Literal, overload
from typing import Any, Iterable, Literal, overload

ROOT_DIR = os.path.dirname(__file__)


@overload
def _program(
name: str, args: list[str], return_completed_process: Literal[True], **kwargs: Any
name: str,
args: Iterable[str],
return_completed_process: Literal[True],
**kwargs: Any,
) -> subprocess.CompletedProcess[str | bytes]: ...


@overload
def _program(
name: str, args: list[str], return_completed_process: Literal[False], **kwargs: Any
name: str,
args: Iterable[str],
return_completed_process: Literal[False] = ...,
**kwargs: Any,
) -> subprocess.CompletedProcess[str | bytes]: ...


@overload
def _program(
name: str, args: list[str], return_completed_process: bool = False, **kwargs: Any
name: str,
args: Iterable[str],
return_completed_process: bool = False,
**kwargs: Any,
) -> int | subprocess.CompletedProcess[str | bytes]: ...


def _program(
name: str, args: list[str], return_completed_process: bool = False, **kwargs: Any
name: str,
args: Iterable[str],
return_completed_process: bool = False,
**kwargs: Any,
) -> int | subprocess.CompletedProcess[str | bytes]:
bin_dir = ROOT_DIR if os.name == "nt" else os.path.join(ROOT_DIR, "bin")
complete_process = subprocess.run([os.path.join(bin_dir, name), *args], **kwargs)
Expand All @@ -44,7 +56,7 @@ def call_node(

@overload
def call_node(
*args: str, return_completed_process: Literal[False], **kwargs: Any
*args: str, return_completed_process: Literal[False] = ..., **kwargs: Any
) -> int: ...


Expand All @@ -68,24 +80,30 @@ def call_node(

@overload
def node(
args: list[str] | None, return_completed_process: Literal[True], **kwargs: Any
args: Iterable[str] | None, return_completed_process: Literal[True], **kwargs: Any
) -> subprocess.CompletedProcess[str | bytes]: ...


@overload
def node(
args: list[str] | None, return_completed_process: Literal[False], **kwargs: Any
args: Iterable[str] | None,
return_completed_process: Literal[False] = ...,
**kwargs: Any,
) -> int: ...


@overload
def node(
args: list[str] | None = None, return_completed_process: bool = False, **kwargs: Any
args: Iterable[str] | None = None,
return_completed_process: bool = False,
**kwargs: Any,
) -> int | subprocess.CompletedProcess[str | bytes]: ...


def node(
args: list[str] | None = None, return_completed_process: bool = False, **kwargs: Any
args: Iterable[str] | None = None,
return_completed_process: bool = False,
**kwargs: Any,
) -> int | subprocess.CompletedProcess[str | bytes]:
"""Call the node executable with the given arguments.
Expand All @@ -111,24 +129,30 @@ def node(

@overload
def npm(
args: list[str] | None, return_completed_process: Literal[True], **kwargs: Any
args: Iterable[str] | None, return_completed_process: Literal[True], **kwargs: Any
) -> subprocess.CompletedProcess[str | bytes]: ...


@overload
def npm(
args: list[str] | None, return_completed_process: Literal[False], **kwargs: Any
args: Iterable[str] | None,
return_completed_process: Literal[False] = ...,
**kwargs: Any,
) -> int: ...


@overload
def npm(
args: list[str] | None = None, return_completed_process: bool = False, **kwargs: Any
args: Iterable[str] | None = None,
return_completed_process: bool = False,
**kwargs: Any,
) -> int | subprocess.CompletedProcess[str | bytes]: ...


def npm(
args: list[str] | None = None, return_completed_process: bool = False, **kwargs: Any
args: Iterable[str] | None = None,
return_completed_process: bool = False,
**kwargs: Any,
) -> int | subprocess.CompletedProcess[str | bytes]:
"""Call the npm executable with the given arguments.
Expand Down Expand Up @@ -159,24 +183,30 @@ def npm(

@overload
def npx(
args: list[str] | None, return_completed_process: Literal[True], **kwargs: Any
args: Iterable[str] | None, return_completed_process: Literal[True], **kwargs: Any
) -> subprocess.CompletedProcess[str | bytes]: ...


@overload
def npx(
args: list[str] | None, return_completed_process: Literal[False], **kwargs: Any
args: Iterable[str] | None,
return_completed_process: Literal[False] = ...,
**kwargs: Any,
) -> int: ...


@overload
def npx(
args: list[str] | None = None, return_completed_process: bool = False, **kwargs: Any
args: Iterable[str] | None = None,
return_completed_process: bool = False,
**kwargs: Any,
) -> int | subprocess.CompletedProcess[str | bytes]: ...


def npx(
args: list[str] | None = None, return_completed_process: bool = False, **kwargs: Any
args: Iterable[str] | None = None,
return_completed_process: bool = False,
**kwargs: Any,
) -> int | subprocess.CompletedProcess[str | bytes]:
"""Call the npx executable with the given arguments.
Expand Down

0 comments on commit 14a8183

Please sign in to comment.