Effortless type-safe user input for integers, floats, dates, and more...
Project description
Typed Input
Effortless type-safe user input for integers, floats, dates, and more...
Installation
PyPI
pip install typed-input
uv
uv add typed-input
Supported Functions:
Each function has this structure:
<type>_input(
prompt: str | None = None,
min_value: <type> | None = None,
max_value: <type> | None = None,
default_value: <type> | None = None,
type_error_message: str | None = None,
) -> <type>
Parameters:
prompt
: (Optional) Message displayed to the user when prompting for input.min_value
/max_value
: (Optional) Bounds for input validation.default_value
: (Optional) Value returned if no input is provided. Must fall within bounds.type_error_message
: (Optional) Error message shown when input cannot be converted to expected type. Defaults are provided for each function.
Default Type Error Messages:
Function | Default type_error_message |
---|---|
int_input |
Error: You must enter a valid integer. |
float_input |
Error: You must enter a valid float. |
decimal_input |
Error: You must enter a valid Decimal. |
datetime_input |
Error: You must enter a valid datetime in valid ISO 8601 format e.g. YYYY-MM-DD. See documentation for all allowed options. |
Example Usage
int_input
- Function for Integer Input
>>> int_input(prompt="Enter a number (1-10): ", min_value=1, max_value=10)
Enter a number (1-10): abc
Error: You must enter a valid integer.
Enter a number (1-10): 20
Error: Value must be between 1 and 10.
Enter a number (1-10): 5
5
Floating-Point Input
>>> float_input(prompt="Enter a temperature (-50 to 50): ", min_value=-50.0, max_value=50.0)
Enter a temperature (-50 to 50):
Error: You must enter a valid float.
Enter a temperature (-50 to 50): 100
Error: Value must be between -50.0 and 50.0.
Enter a temperature (-50 to 50): 22.5
22.5
Decimal Input
>>> from decimal import Decimal
>>> decimal_input(prompt="Enter a price (min 0.01): ", min_value=Decimal("0.01"))
Enter a price (min 0.01): -10
Error: Value must be at least 0.01.
Enter a price (min 0.01): 19.99
Decimal('19.99')
Datetime Input
>>> from datetime import datetime
>>> datetime_input(prompt="Enter a date (YYYY-MM-DD): ", min_value=datetime(2023, 1, 1))
Enter a date (YYYY-MM-DD): invalid
Error: You must enter a valid datetime in ISO 8601 format (e.g., YYYY-MM-DD).
Enter a date (YYYY-MM-DD): 2022-12-31
Error: Date must be on or after 2023-01-01.
Enter a date (YYYY-MM-DD): 2023-01-15
datetime.datetime(2023, 1, 15, 0, 0)
❌ Error Handling
All functions raise a ValueError
for:
- Invalid Range:
min_value > max_value
. - Default Out of Bounds:
default_value
outsidemin_value
/max_value
.
Example:
>>> int_input(min_value=10, max_value=5)
Traceback (most recent call last):
...
ValueError: min_value (10) cannot be greater than max_value (5).
🛠️ Development
- Run formater:
uv run ruff check --select I --fix && uv run ruff format
- Run type checking:
uv run mypy .
- Run all unit tests:
uv run typed_input_test.py
- Run specific unit test:
uv run python -m unittest int_input_test.py
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
typed_input-1.0.0.tar.gz
(140.0 kB
view details)
Built Distribution
File details
Details for the file typed_input-1.0.0.tar.gz
.
File metadata
- Download URL: typed_input-1.0.0.tar.gz
- Upload date:
- Size: 140.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.5.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d3407325cc8c585ebf74de3c22a0fc582c0c88f85452142ae42b3b4fdeaaa1b6 |
|
MD5 | 0362bf892f63481c4d23574296474277 |
|
BLAKE2b-256 | 919641228b757729e2acae2908950a0231b24518dff2108ffc575d45769ddebb |
File details
Details for the file typed_input-1.0.0-py3-none-any.whl
.
File metadata
- Download URL: typed_input-1.0.0-py3-none-any.whl
- Upload date:
- Size: 6.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.5.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 873f5a3ee0fbc1fa1c4bf79a4fdb77cd8667281e6c5d3f9f8bad56cc4588e5e3 |
|
MD5 | 4f0fef1ad018eebad7a2a4a267ac867c |
|
BLAKE2b-256 | 7a784c04b6fda454c49b4ebd2f4437f280d62a96b1d7a828d189a58c538ab209 |