Skip to content

Commit 8d1e192

Browse files
don't use inputimeout
1 parent fd567d2 commit 8d1e192

File tree

3 files changed

+78
-32
lines changed

3 files changed

+78
-32
lines changed

pyproject.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ dependencies = [
4545
"pandas>=1.5.0",
4646
"pooch>=1.8.1",
4747
"posthog",
48-
"inputimeout",
4948
]
5049

5150
[project.urls]

src/pybamm/config.py

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
import platformdirs
44
from pathlib import Path
55
import pybamm
6-
from inputimeout import inputimeout, TimeoutOccurred
6+
import select
7+
import sys
78

89

910
def is_running_tests(): # pragma: no cover
@@ -48,7 +49,7 @@ def ask_user_opt_in(timeout=10):
4849
4950
Parameters
5051
----------
51-
timeout: float, optional
52+
timeout : float, optional
5253
The timeout for the user to respond to the prompt. Default is 10 seconds.
5354
5455
Returns
@@ -67,25 +68,21 @@ def ask_user_opt_in(timeout=10):
6768
)
6869

6970
while True:
70-
try:
71-
user_input = (
72-
inputimeout(
73-
prompt="Do you want to enable telemetry? (Y/n): ", timeout=timeout
74-
)
75-
.strip()
76-
.lower()
77-
)
78-
except TimeoutOccurred:
71+
print("Do you want to enable telemetry? (Y/n): ", end="", flush=True)
72+
73+
rlist, _, _ = select.select([sys.stdin], [], [], timeout)
74+
if rlist:
75+
user_input = sys.stdin.readline().strip().lower()
76+
if user_input in ["yes", "y", ""]:
77+
return True
78+
elif user_input in ["no", "n"]:
79+
return False
80+
else:
81+
print("Invalid input. Please enter 'yes/y' for yes or 'no/n' for no.")
82+
else:
7983
print("\nTimeout reached. Defaulting to not enabling telemetry.")
8084
return False
8185

82-
if user_input in ["yes", "y", ""]:
83-
return True
84-
elif user_input in ["no", "n"]:
85-
return False
86-
else:
87-
print("\nInvalid input. Please enter 'yes'/'y' or 'no'/'n'.")
88-
8986

9087
def generate(): # pragma: no cover
9188
if is_running_tests():

tests/unit/test_config.py

Lines changed: 63 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import pytest
2-
from inputimeout import TimeoutOccurred
2+
import select
3+
import sys
34

45
import pybamm
56
import uuid
@@ -33,24 +34,73 @@ def test_write_read_uuid(self, tmp_path, write_opt_in):
3334
assert config_dict["enable_telemetry"] is False
3435

3536
@pytest.mark.parametrize("user_opted_in, user_input", [(True, "y"), (False, "n")])
36-
def test_ask_user_opt_in(self, monkeypatch, user_opted_in, user_input):
37-
# Mock the inputimeout function to return invalid input first, then valid input
38-
inputs = iter(["invalid", user_input])
39-
monkeypatch.setattr(
40-
"pybamm.config.inputimeout", lambda prompt, timeout: next(inputs)
41-
)
37+
def test_ask_user_opt_in(self, monkeypatch, capsys, user_opted_in, user_input):
38+
# Mock select.select to simulate user input
39+
def mock_select(*args, **kwargs):
40+
return [sys.stdin], [], []
41+
42+
monkeypatch.setattr(select, "select", mock_select)
43+
44+
# Mock sys.stdin.readline to return the desired input
45+
monkeypatch.setattr(sys.stdin, "readline", lambda: user_input + "\n")
4246

4347
# Call the function to ask the user if they want to opt in
4448
opt_in = pybamm.config.ask_user_opt_in()
49+
50+
# Check the result
4551
assert opt_in is user_opted_in
4652

47-
def test_ask_user_opt_in_timeout(self, monkeypatch):
48-
# Mock the inputimeout function to raise a TimeoutOccurred exception
49-
def mock_inputimeout(*args, **kwargs):
50-
raise TimeoutOccurred
53+
# Check that the prompt was printed
54+
captured = capsys.readouterr()
55+
assert "Do you want to enable telemetry? (Y/n):" in captured.out
56+
57+
def test_ask_user_opt_in_invalid_input(self, monkeypatch, capsys):
58+
# Mock select.select to simulate user input and then timeout
59+
def mock_select(*args, **kwargs):
60+
nonlocal call_count
61+
if call_count == 0:
62+
call_count += 1
63+
return [sys.stdin], [], []
64+
else:
65+
return [], [], []
5166

52-
monkeypatch.setattr("pybamm.config.inputimeout", mock_inputimeout)
67+
monkeypatch.setattr(select, "select", mock_select)
68+
69+
# Mock sys.stdin.readline to return invalid input
70+
monkeypatch.setattr(sys.stdin, "readline", lambda: "invalid\n")
71+
72+
# Initialize call count
73+
call_count = 0
5374

5475
# Call the function to ask the user if they want to opt in
55-
opt_in = pybamm.config.ask_user_opt_in()
76+
opt_in = pybamm.config.ask_user_opt_in(timeout=1)
77+
78+
# Check the result (should be False for timeout after invalid input)
5679
assert opt_in is False
80+
81+
# Check that the prompt, invalid input message, and timeout message were printed
82+
captured = capsys.readouterr()
83+
assert "Do you want to enable telemetry? (Y/n):" in captured.out
84+
assert (
85+
"Invalid input. Please enter 'yes/y' for yes or 'no/n' for no."
86+
in captured.out
87+
)
88+
assert "Timeout reached. Defaulting to not enabling telemetry." in captured.out
89+
90+
def test_ask_user_opt_in_timeout(self, monkeypatch, capsys):
91+
# Mock select.select to simulate a timeout
92+
def mock_select(*args, **kwargs):
93+
return [], [], []
94+
95+
monkeypatch.setattr(select, "select", mock_select)
96+
97+
# Call the function to ask the user if they want to opt in
98+
opt_in = pybamm.config.ask_user_opt_in(timeout=1)
99+
100+
# Check the result (should be False for timeout)
101+
assert opt_in is False
102+
103+
# Check that the prompt and timeout message were printed
104+
captured = capsys.readouterr()
105+
assert "Do you want to enable telemetry? (Y/n):" in captured.out
106+
assert "Timeout reached. Defaulting to not enabling telemetry." in captured.out

0 commit comments

Comments
 (0)