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. |
Seamless Input API Compatibility
The typed_input library is designed to feel natural for Python developers by
supporting the same API as the standard input() function.
Example Usage (like input())
>>> from typed_input import int_input
>>> x = int_input()
a
Error: You must enter a valid integer.
42
>>> type(x)
<class 'int'>
>>> x
42
>>> from typed_input import float_input
>>> x = float_input()
3.14
>>> type(x)
<class 'float'>
>>> from typed_input import decimal_input
>>> x = decimal_input()
1.45
>>> type(x)
<class 'decimal.Decimal'>
>>> from typed_input import datetime_input
>>> dt = datetime_input()
2024-01-01
>>> type(dt)
<class 'datetime.datetime'>
Example with a prompt
Just like input(), you can also pass a prompt to guide the user:
>>> from typed_input import int_input
>>> int_input('Enter a number: ')
Enter a number: 7
7
This compatibility makes typed_input a drop-in replacement for input() in
many scenarios, with the added benefit of type safety and validation!
More Examples Showing Range Validation
int_input for validated integer input
>>> from typed_input import int_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
float_input for validated floating-point input
>>> from typed_input import float_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 for validated decimal input
>>> from typed_input import 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 for validated datetime input
>>> from typed_input import 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_valueoutsidemin_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
Metadata
Release files for typed-input 1.3.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| typed_input-1.3.1.tar.gz | 141.1 kB | Without provenance | Details for typed_input-1.3.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| typed_input-1.3.1-py3-none-any.whl | Python 3 | none | any | Details for typed_input-1.3.1-py3-none-any.whl |
Total release size: 147.8 kB
Release files / typed_input-1.3.1.tar.gz
| Download URL | typed_input-1.3.1.tar.gz |
|---|---|
| Size | 141.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
e1a87e7401df1e87e1a18a39866815e32d10d041a450d65b1acb0f0a66896dc1
|
|
BLAKE2b-256 checksum How to use checksums |
691fcd96e19b6e44ad9be0a265ea2a512e8a583371344b6d2293f655e0ca0b8f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.5.2
|
Release files / typed_input-1.3.1-py3-none-any.whl
| Download URL | typed_input-1.3.1-py3-none-any.whl |
|---|---|
| Size | 6.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
ad2d1d8683934b4caac3cf3c886f3f4d7313cffb8a72c209796c5d1d76b87411
|
|
BLAKE2b-256 checksum How to use checksums |
88aea498c3e11641b53022ef838c4ad608d26eb6bf621fec729d12b64bde7f79
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.5.2
|
Security for typed-input 1.3.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.