Skip to content

Commit d5d33d7

Browse files
committed
Import platform later
It takes a while to import platform, so use sys.platform to check for macOS/Linux, and only import platform to detect BSD dialects.
1 parent 9cdc5b5 commit d5d33d7

File tree

2 files changed

+14
-11
lines changed

2 files changed

+14
-11
lines changed

tests/test_launch.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,19 @@
88

99

1010
@pytest.mark.parametrize(
11-
"system, command",
11+
"system, sys_platform, command",
1212
[
13-
("Darwin", "open"),
14-
("Linux", "xdg-open"),
15-
("FreeBSD", "xdg-open"),
13+
("Darwin", "darwin", "open"),
14+
("Linux", "linux", "xdg-open"),
15+
("FreeBSD", "freebsd8", "xdg-open"),
1616
],
1717
)
18-
def test_launch_url_unix(system: str, command: str):
18+
def test_launch_url_unix(system: str, sys_platform: str, command: str):
1919
with patch("platform.system", return_value=system), patch(
20-
"shutil.which", return_value=True
21-
), patch("subprocess.Popen") as mock_popen:
20+
"sys.platform", sys_platform
21+
), patch("shutil.which", return_value=True), patch(
22+
"subprocess.Popen"
23+
) as mock_popen:
2224
typer.launch(url)
2325

2426
mock_popen.assert_called_once_with(
@@ -27,7 +29,7 @@ def test_launch_url_unix(system: str, command: str):
2729

2830

2931
def test_launch_url_windows():
30-
with patch("platform.system", return_value="Windows"), patch(
32+
with patch("sys.platform", "windows"), patch(
3133
"webbrowser.open"
3234
) as mock_webbrowser_open:
3335
typer.launch(url)

typer/main.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import inspect
22
import os
3-
import platform
43
import shutil
54
import subprocess
65
import sys
@@ -1072,13 +1071,15 @@ def run(function: Callable[..., Any]) -> None:
10721071

10731072

10741073
def _is_macos() -> bool:
1075-
return platform.system() == "Darwin"
1074+
return sys.platform == "darwin"
10761075

10771076

10781077
def _is_linux_or_bsd() -> bool:
1079-
if platform.system() == "Linux":
1078+
if sys.platform == "linux":
10801079
return True
10811080

1081+
import platform
1082+
10821083
return "BSD" in platform.system()
10831084

10841085

0 commit comments

Comments
 (0)