typed-input


Nametyped-input JSON
Version 1.3.1 PyPI version JSON
download
home_pageNone
SummaryEffortless type-safe user input for integers, floats, dates, and more...
upload_time2024-11-21 12:35:28
maintainerNone
docs_urlNone
authorNone
requires_python>=3.9
licenseMIT License Copyright (c) 2024 Sash Sinha Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
keywords input input-validation typing utilities
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            <p align="center">
<a href="https://github.com/sashsinha/typed_input"><img alt="Typed Input Logo" src="https://raw.githubusercontent.com/sashsinha/typed_input/main/logo.png"></a>
</p>

<h1 align="center">Typed Input</h1>

<h3 align="center">Effortless type-safe user input for integers, floats, dates, and more...</h3>

<br/>

<p align="center">
<a href="https://raw.githubusercontent.com/sashsinha/typed_input/main/LICENCE"><img alt="License: MIT" src="https://raw.githubusercontent.com/sashsinha/typed_input/main/license.svg"></a>
<a href="https://pypi.org/project/typed-input/"><img src="https://img.shields.io/badge/platform-windows%20%7C%20linux%20%7C%20macos-lightgrey" alt="Supported Platforms"></a>
<a href="https://pypi.org/project/typed-input/"><img 
alt="PyPI Supported Versions" src="https://img.shields.io/pypi/pyversions/typed-input.svg"></a>
<a href="https://pypi.org/project/typed-input/"><img alt="PyPI" src="https://img.shields.io/pypi/v/typed-input"></a>
<a href="https://pypi.org/project/typed-input/"><img alt="PyPI Status" src="https://img.shields.io/pypi/status/typed-input"></a>
<a href="https://pepy.tech/project/typed-input"><img alt="Downloads" src="https://pepy.tech/badge/typed-input"></a>
</p>

### Installation

#### PyPI
```
pip install typed-input
```

#### [`uv`](https://github.com/astral-sh/uv)
```
uv add typed-input
```


### Supported Functions:
- [`int_input`](#int_input-for-validated-integer-input)
- [`float_input`](#float_input-for-validated-floating-point-input)
- [`decimal_input`](#decimal_input-for-validated-decimal-input)
- [`datetime_input`](#datetime_input-for-validated-datetime-input)

Each function has this structure:

```python
<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.<br>See [documentation](https://docs.python.org/3/library/datetime.html#datetime.datetime.fromisoformat) 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()`](https://docs.python.org/3/library/functions.html#input) function.

#### Example Usage (like [`input()`](https://docs.python.org/3/library/functions.html#input))
```python
>>> 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()`](https://docs.python.org/3/library/functions.html#input), you can also pass a `prompt` to guide the user:
```python
>>> 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
```python
>>> 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
```python
>>> 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
```python
>>> 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
```python
>>> 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_value` outside `min_value`/`max_value`.

Example:
```python
>>> 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`


            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "typed-input",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.9",
    "maintainer_email": "Sash Sinha <sashsinha1@gmail.com>",
    "keywords": "input, input-validation, typing, utilities",
    "author": null,
    "author_email": "Sash Sinha <sashsinha1@gmail.com>",
    "download_url": "https://files.pythonhosted.org/packages/69/1f/cd96e19b6e44ad9be0a265ea2a512e8a583371344b6d2293f655e0ca0b8f/typed_input-1.3.1.tar.gz",
    "platform": null,
    "description": "<p align=\"center\">\n<a href=\"https://github.com/sashsinha/typed_input\"><img alt=\"Typed Input Logo\" src=\"https://raw.githubusercontent.com/sashsinha/typed_input/main/logo.png\"></a>\n</p>\n\n<h1 align=\"center\">Typed Input</h1>\n\n<h3 align=\"center\">Effortless type-safe user input for integers, floats, dates, and more...</h3>\n\n<br/>\n\n<p align=\"center\">\n<a href=\"https://raw.githubusercontent.com/sashsinha/typed_input/main/LICENCE\"><img alt=\"License: MIT\" src=\"https://raw.githubusercontent.com/sashsinha/typed_input/main/license.svg\"></a>\n<a href=\"https://pypi.org/project/typed-input/\"><img src=\"https://img.shields.io/badge/platform-windows%20%7C%20linux%20%7C%20macos-lightgrey\" alt=\"Supported Platforms\"></a>\n<a href=\"https://pypi.org/project/typed-input/\"><img \nalt=\"PyPI Supported Versions\" src=\"https://img.shields.io/pypi/pyversions/typed-input.svg\"></a>\n<a href=\"https://pypi.org/project/typed-input/\"><img alt=\"PyPI\" src=\"https://img.shields.io/pypi/v/typed-input\"></a>\n<a href=\"https://pypi.org/project/typed-input/\"><img alt=\"PyPI Status\" src=\"https://img.shields.io/pypi/status/typed-input\"></a>\n<a href=\"https://pepy.tech/project/typed-input\"><img alt=\"Downloads\" src=\"https://pepy.tech/badge/typed-input\"></a>\n</p>\n\n### Installation\n\n#### PyPI\n```\npip install typed-input\n```\n\n#### [`uv`](https://github.com/astral-sh/uv)\n```\nuv add typed-input\n```\n\n\n### Supported Functions:\n- [`int_input`](#int_input-for-validated-integer-input)\n- [`float_input`](#float_input-for-validated-floating-point-input)\n- [`decimal_input`](#decimal_input-for-validated-decimal-input)\n- [`datetime_input`](#datetime_input-for-validated-datetime-input)\n\nEach function has this structure:\n\n```python\n<type>_input(\n  prompt: str | None = None,\n  min_value: <type> | None = None,\n  max_value: <type> | None = None,\n  default_value: <type> | None = None,\n  type_error_message: str | None = None,\n) -> <type>\n```\n\n#### Parameters:\n- **`prompt`**: *(Optional)* Message displayed to the user when prompting for input.\n- **`min_value` / `max_value`**: *(Optional)* Bounds for input validation.\n- **`default_value`**: *(Optional)* Value returned if no input is provided. Must fall within bounds.\n- **`type_error_message`**: *(Optional)* Error message shown when input cannot be converted to expected type. Defaults are provided for each function.\n\n#### Default Type Error Messages:\n\n| Function         | Default `type_error_message`                                                                                                                                                                                          |\n| ---------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |\n| `int_input`      | **Error**: You must enter a valid integer.                                                                                                                                                                            |\n| `float_input`    | **Error**: You must enter a valid float.                                                                                                                                                                              |\n| `decimal_input`  | **Error**: You must enter a valid Decimal.                                                                                                                                                                            |\n| `datetime_input` | **Error**: You must enter a valid datetime in valid ISO 8601 format e.g. YYYY-MM-DD.<br>See [documentation](https://docs.python.org/3/library/datetime.html#datetime.datetime.fromisoformat) for all allowed options. |\n\n### Seamless Input API Compatibility\n\nThe `typed_input` library is designed to feel natural for Python developers by\nsupporting the same API as the standard [`input()`](https://docs.python.org/3/library/functions.html#input) function.\n\n#### Example Usage (like [`input()`](https://docs.python.org/3/library/functions.html#input))\n```python\n>>> from typed_input import int_input\n>>> x = int_input()\na\nError: You must enter a valid integer.\n42\n>>> type(x)\n<class 'int'>\n>>> x\n42\n\n>>> from typed_input import float_input\n>>> x = float_input()\n3.14\n>>> type(x)\n<class 'float'>\n\n>>> from typed_input import decimal_input\n>>> x = decimal_input()\n1.45\n>>> type(x)\n<class 'decimal.Decimal'>\n\n>>> from typed_input import datetime_input\n>>> dt = datetime_input()\n2024-01-01\n>>> type(dt)\n<class 'datetime.datetime'>\n```\n\n#### Example with a `prompt`\nJust like [`input()`](https://docs.python.org/3/library/functions.html#input), you can also pass a `prompt` to guide the user:\n```python\n>>> from typed_input import int_input\n>>> int_input('Enter a number: ')\nEnter a number: 7\n7\n```\n\nThis compatibility makes `typed_input` a drop-in replacement for `input()` in \nmany scenarios, with the added benefit of type safety and validation!\n\n### More Examples Showing Range Validation\n\n#### `int_input` for validated integer input\n```python\n>>> from typed_input import int_input\n>>> int_input(prompt=\"Enter a number (1-10): \", min_value=1, max_value=10)\nEnter a number (1-10): abc\nError: You must enter a valid integer.\nEnter a number (1-10): 20\nError: Value must be between 1 and 10.\nEnter a number (1-10): 5\n5\n```\n\n#### `float_input` for validated floating-point input\n```python\n>>> from typed_input import float_input\n>>> float_input(prompt=\"Enter a temperature (-50 to 50): \", min_value=-50.0, max_value=50.0)\nEnter a temperature (-50 to 50): \nError: You must enter a valid float.\nEnter a temperature (-50 to 50): 100\nError: Value must be between -50.0 and 50.0.\nEnter a temperature (-50 to 50): 22.5\n22.5\n```\n\n#### `decimal_input` for validated decimal input\n```python\n>>> from typed_input import decimal_input\n>>> from decimal import Decimal\n>>> decimal_input(prompt=\"Enter a price (min 0.01): \", min_value=Decimal(\"0.01\"))\nEnter a price (min 0.01): -10\nError: Value must be at least 0.01.\nEnter a price (min 0.01): 19.99\nDecimal('19.99')\n```\n\n#### `datetime_input` for validated datetime input\n```python\n>>> from typed_input import datetime_input\n>>> from datetime import datetime\n>>> datetime_input(prompt=\"Enter a date (YYYY-MM-DD): \", min_value=datetime(2023, 1, 1))\nEnter a date (YYYY-MM-DD): invalid\nError: You must enter a valid datetime in ISO 8601 format (e.g., YYYY-MM-DD).\nEnter a date (YYYY-MM-DD): 2022-12-31\nError: Date must be on or after 2023-01-01.\nEnter a date (YYYY-MM-DD): 2023-01-15\ndatetime.datetime(2023, 1, 15, 0, 0)\n```\n---\n\n### \u274c Error Handling\n\nAll functions raise a `ValueError` for:\n- **Invalid Range**: `min_value > max_value`.\n- **Default Out of Bounds**: `default_value` outside `min_value`/`max_value`.\n\nExample:\n```python\n>>> int_input(min_value=10, max_value=5)\nTraceback (most recent call last):\n  ...\nValueError: min_value (10) cannot be greater than max_value (5).\n```\n\n---\n\n### \ud83d\udee0\ufe0f Development\n\n- Run formater: \n  - `uv run ruff check --select I --fix && uv run ruff format`\n- Run type checking: \n  - `uv run mypy . `\n- Run all unit tests:\n  - `uv run typed_input_test.py`\n- Run specific unit test:\n  - `uv run python -m unittest int_input_test.py`\n\n",
    "bugtrack_url": null,
    "license": "MIT License  Copyright (c) 2024 Sash Sinha  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the \"Software\"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:  The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.  THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.",
    "summary": "Effortless type-safe user input for integers, floats, dates, and more...",
    "version": "1.3.1",
    "project_urls": {
        "Documentation": "https://github.com/sashsinha/typed_input/blob/main/README.md",
        "Homepage": "https://github.com/sashsinha/typed_input",
        "Repository": "https://github.com/sashsinha/typed_input.git"
    },
    "split_keywords": [
        "input",
        " input-validation",
        " typing",
        " utilities"
    ],
    "urls": [
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "88aea498c3e11641b53022ef838c4ad608d26eb6bf621fec729d12b64bde7f79",
                "md5": "b659a5497c2231227852068e2860cbfd",
                "sha256": "ad2d1d8683934b4caac3cf3c886f3f4d7313cffb8a72c209796c5d1d76b87411"
            },
            "downloads": -1,
            "filename": "typed_input-1.3.1-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "b659a5497c2231227852068e2860cbfd",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.9",
            "size": 6661,
            "upload_time": "2024-11-21T12:35:26",
            "upload_time_iso_8601": "2024-11-21T12:35:26.119525Z",
            "url": "https://files.pythonhosted.org/packages/88/ae/a498c3e11641b53022ef838c4ad608d26eb6bf621fec729d12b64bde7f79/typed_input-1.3.1-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "691fcd96e19b6e44ad9be0a265ea2a512e8a583371344b6d2293f655e0ca0b8f",
                "md5": "76705450912b7318b45203bc0f47d50f",
                "sha256": "e1a87e7401df1e87e1a18a39866815e32d10d041a450d65b1acb0f0a66896dc1"
            },
            "downloads": -1,
            "filename": "typed_input-1.3.1.tar.gz",
            "has_sig": false,
            "md5_digest": "76705450912b7318b45203bc0f47d50f",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.9",
            "size": 141118,
            "upload_time": "2024-11-21T12:35:28",
            "upload_time_iso_8601": "2024-11-21T12:35:28.180932Z",
            "url": "https://files.pythonhosted.org/packages/69/1f/cd96e19b6e44ad9be0a265ea2a512e8a583371344b6d2293f655e0ca0b8f/typed_input-1.3.1.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-11-21 12:35:28",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "sashsinha",
    "github_project": "typed_input",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "tox": true,
    "lcname": "typed-input"
}
        
Elapsed time: 0.39564s