Skip to content

Commit

Permalink
Merge branch 'master' into feat/autocompletion
Browse files Browse the repository at this point in the history
  • Loading branch information
bckohan authored Nov 8, 2024
2 parents 8b300ba + 299ad70 commit 00e6af5
Show file tree
Hide file tree
Showing 8 changed files with 148 additions and 5 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/latest-changes.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ jobs:
if: ${{ github.event_name == 'workflow_dispatch' && github.event.inputs.debug_enabled == 'true' }}
with:
limit-access-to-actor: true
- uses: tiangolo/[email protected].1
- uses: tiangolo/[email protected].2
with:
token: ${{ secrets.GITHUB_TOKEN }}
latest_changes_file: docs/release-notes.md
Expand Down
9 changes: 9 additions & 0 deletions docs/release-notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,17 @@

## Latest Changes

### Internal

* ⬆ Bump tiangolo/latest-changes from 0.3.1 to 0.3.2. PR [#1044](https://github.com/fastapi/typer/pull/1044) by [@dependabot[bot]](https://github.com/apps/dependabot).
* ⬆ Update pytest-cov requirement from <6.0.0,>=2.10.0 to >=2.10.0,<7.0.0. PR [#1033](https://github.com/fastapi/typer/pull/1033) by [@dependabot[bot]](https://github.com/apps/dependabot).

## 0.13.0

### Features

* ✨ Handle `KeyboardInterrupt` separately from other exceptions. PR [#1039](https://github.com/fastapi/typer/pull/1039) by [@patrick91](https://github.com/patrick91).
* ✨ Update `launch` to not print anything when opening urls. PR [#1035](https://github.com/fastapi/typer/pull/1035) by [@patrick91](https://github.com/patrick91).
* ✨ Show help items in order of definition. PR [#944](https://github.com/fastapi/typer/pull/944) by [@svlandeg](https://github.com/svlandeg).

### Fixes
Expand Down
2 changes: 1 addition & 1 deletion requirements-tests.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
-e .

pytest >=4.4.0,<9.0.0
pytest-cov >=2.10.0,<6.0.0
pytest-cov >=2.10.0,<7.0.0
coverage[toml] >=6.2,<8.0
pytest-xdist >=1.32.0,<4.0.0
pytest-sugar >=0.9.4,<1.1.0
Expand Down
13 changes: 13 additions & 0 deletions tests/test_exit_errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,19 @@ def main():
assert result.exit_code == 1


def test_keyboardinterrupt():
# Mainly for coverage/completeness
app = typer.Typer()

@app.command()
def main():
raise KeyboardInterrupt()

result = runner.invoke(app)
assert result.exit_code == 130
assert result.stdout == ""


def test_oserror():
# Mainly for coverage/completeness
app = typer.Typer()
Expand Down
51 changes: 51 additions & 0 deletions tests/test_launch.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import subprocess
from unittest.mock import patch

import pytest
import typer

url = "http://example.com"


@pytest.mark.parametrize(
"system, command",
[
("Darwin", "open"),
("Linux", "xdg-open"),
("FreeBSD", "xdg-open"),
],
)
def test_launch_url_unix(system: str, command: str):
with patch("platform.system", return_value=system), patch(
"shutil.which", return_value=True
), patch("subprocess.Popen") as mock_popen:
typer.launch(url)

mock_popen.assert_called_once_with(
[command, url], stdout=subprocess.DEVNULL, stderr=subprocess.STDOUT
)


def test_launch_url_windows():
with patch("platform.system", return_value="Windows"), patch(
"webbrowser.open"
) as mock_webbrowser_open:
typer.launch(url)

mock_webbrowser_open.assert_called_once_with(url)


def test_launch_url_no_xdg_open():
with patch("platform.system", return_value="Linux"), patch(
"shutil.which", return_value=None
), patch("webbrowser.open") as mock_webbrowser_open:
typer.launch(url)

mock_webbrowser_open.assert_called_once_with(url)


def test_calls_original_launch_when_not_passing_urls():
with patch("typer.main.click.launch", return_value=0) as launch_mock:
typer.launch("not a url")

launch_mock.assert_called_once_with("not a url")
4 changes: 2 additions & 2 deletions typer/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""Typer, build great CLIs. Easy to code. Based on Python type hints."""

__version__ = "0.12.5"
__version__ = "0.13.0"

from shutil import get_terminal_size as get_terminal_size

Expand All @@ -12,7 +12,6 @@
from click.termui import echo_via_pager as echo_via_pager
from click.termui import edit as edit
from click.termui import getchar as getchar
from click.termui import launch as launch
from click.termui import pause as pause
from click.termui import progressbar as progressbar
from click.termui import prompt as prompt
Expand All @@ -28,6 +27,7 @@

from . import colors as colors
from .main import Typer as Typer
from .main import launch as launch
from .main import run as run
from .models import CallbackParam as CallbackParam
from .models import Context as Context
Expand Down
4 changes: 3 additions & 1 deletion typer/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,9 +211,11 @@ def _main(
# even always obvious that `rv` indicates success/failure
# by its truthiness/falsiness
ctx.exit()
except (EOFError, KeyboardInterrupt) as e:
except EOFError as e:
click.echo(file=sys.stderr)
raise click.Abort() from e
except KeyboardInterrupt as e:
raise click.exceptions.Exit(130) from e
except click.ClickException as e:
if not standalone_mode:
raise
Expand Down
68 changes: 68 additions & 0 deletions typer/main.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import inspect
import os
import platform
import shutil
import subprocess
import sys
import traceback
from datetime import datetime
Expand Down Expand Up @@ -1090,3 +1093,68 @@ def run(function: Callable[..., Any]) -> None:
app = Typer(add_completion=False)
app.command()(function)
app()


def _is_macos() -> bool:
return platform.system() == "Darwin"


def _is_linux_or_bsd() -> bool:
if platform.system() == "Linux":
return True

return "BSD" in platform.system()


def launch(url: str, wait: bool = False, locate: bool = False) -> int:
"""This function launches the given URL (or filename) in the default
viewer application for this file type. If this is an executable, it
might launch the executable in a new session. The return value is
the exit code of the launched application. Usually, ``0`` indicates
success.
This function handles url in different operating systems separately:
- On macOS (Darwin), it uses the 'open' command.
- On Linux and BSD, it uses 'xdg-open' if available.
- On Windows (and other OSes), it uses the standard webbrowser module.
The function avoids, when possible, using the webbrowser module on Linux and macOS
to prevent spammy terminal messages from some browsers (e.g., Chrome).
Examples::
typer.launch("https://typer.tiangolo.com/")
typer.launch("/my/downloaded/file", locate=True)
:param url: URL or filename of the thing to launch.
:param wait: Wait for the program to exit before returning. This
only works if the launched program blocks. In particular,
``xdg-open`` on Linux does not block.
:param locate: if this is set to `True` then instead of launching the
application associated with the URL it will attempt to
launch a file manager with the file located. This
might have weird effects if the URL does not point to
the filesystem.
"""

if url.startswith("http://") or url.startswith("https://"):
if _is_macos():
return subprocess.Popen(
["open", url], stdout=subprocess.DEVNULL, stderr=subprocess.STDOUT
).wait()

has_xdg_open = _is_linux_or_bsd() and shutil.which("xdg-open") is not None

if has_xdg_open:
return subprocess.Popen(
["xdg-open", url], stdout=subprocess.DEVNULL, stderr=subprocess.STDOUT
).wait()

import webbrowser

webbrowser.open(url)

return 0

else:
return click.launch(url)

0 comments on commit 00e6af5

Please sign in to comment.