typeca


Nametypeca JSON
Version 0.1.1 PyPI version JSON
download
home_pageNone
SummaryA type-checking utility for Python functions
upload_time2024-10-28 13:05:34
maintainerNone
docs_urlNone
authorNone
requires_python>=3.9
licenseCopyright (c) 2024 Georgii Viktorov 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 decorators python types runtime type checking static typing type annotations type checking type enforcement type hints validation
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # Typeca

**Typeca** is a Python decorator for enforcing type checks on both positional and keyword arguments
on functions with annotated types.

It ensures that arguments passed to functions and the function's return value match their specified types, 
raising a TypeError if any type mismatch is found.

P.S. Anyway, this decorator would negatively affect a function`s performance, so the best approach would be to use it 
during development and testing phases.

```python
%timeit -n 10 -r 7 gen_array(1_000_000)
50.5 ms ± 1.53 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)

%timeit -n 10 -r 7 gen_array_type_enforced(1_000_000)
474 ms ± 14.9 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)
```

## Supported Python Versions
* Python 3.9 and later.

## Features
* **Flexible Enforcement**: Skips type checking for arguments without annotations.
* **Nested Annotation Check**: The decorator supports recursive type checking for nested data structures.
* **Enable/Disable Type Checking**: Users can enable or disable type enforcement on a function by using the enable parameter, defaults to True.
* **Error Handling**: Raises a TypeError if a type mismatch is detected for either function args or the return value.

## Supported Types
* **Standard Types**: Such as int, str, float, bool, and other built-in types.
* **Annotated Data Structures**:
  1. **list[T]**: Checks that the value is a list and that every element conforms to type T.
  2. **dict[K, V]**: Checks that the value is a dictionary, and that each key has type K and each value has type V.
  3. **tuple[T1, T2, ...]**: Checks that the value is a tuple, and that each element has specified type (e.g., tuple[int, str] for (41, 'Saturday')).

## Installation

```bash
pip install typeca
```

## Usage
Use **@type_enforcer** to enforce type checks on your functions:

```python
from typeca import type_enforcer 

@type_enforcer()
def two_num_product(a: int, b: int) -> int:
    return a * b

# Valid usage
print(two_num_product(2, 3))  # Output: 6

# Invalid usage
print(two_num_product(2, '3'))  # Raises TypeError
```

## Examples
### Example 1: Simple Type Enforcement

```python
@type_enforcer()
def add(a: int, b: int) -> int:
    return a + b

add(3, 4)  # Works fine
add(3, '4')  # Raises TypeError
```

### Example 2: Complex Data Structures

Supports lists, dictionaries, and tuples with type annotations:

```python
@type_enforcer()
def process_items(items: list[int]) -> list[int]:
    return [item * 2 for item in items]

process_items([1, 2, 3])  # Works fine
process_items(['a', 'b', 'c'])  # Raises TypeError
```

### Example 3: Disable Type Enforcement

At any moment you can disable check to improve performance of the function:
```python
@type_enforcer(enable=False)
def process_array(*args) -> list[int]:
    return list(args) * 2

process_array(1, 2, 3) # Works without type enforcement
```
            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "typeca",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.9",
    "maintainer_email": null,
    "keywords": "decorators, python types, runtime type checking, static typing, type annotations, type checking, type enforcement, type hints, validation",
    "author": null,
    "author_email": "Georgii Viktorov <nowayisleft@gmail.com>",
    "download_url": "https://files.pythonhosted.org/packages/8c/d5/1303031ab208ad71e9efda29e7d631494b8c51a742a2b4a568d7bd7b6842/typeca-0.1.1.tar.gz",
    "platform": null,
    "description": "# Typeca\n\n**Typeca** is a Python decorator for enforcing type checks on both positional and keyword arguments\non functions with annotated types.\n\nIt ensures that arguments passed to functions and the function's return value match their specified types, \nraising a TypeError if any type mismatch is found.\n\nP.S. Anyway, this decorator would negatively affect a function`s performance, so the best approach would be to use it \nduring development and testing phases.\n\n```python\n%timeit -n 10 -r 7 gen_array(1_000_000)\n50.5 ms \u00b1 1.53 ms per loop (mean \u00b1 std. dev. of 7 runs, 10 loops each)\n\n%timeit -n 10 -r 7 gen_array_type_enforced(1_000_000)\n474 ms \u00b1 14.9 ms per loop (mean \u00b1 std. dev. of 7 runs, 10 loops each)\n```\n\n## Supported Python Versions\n* Python 3.9 and later.\n\n## Features\n* **Flexible Enforcement**: Skips type checking for arguments without annotations.\n* **Nested Annotation Check**: The decorator supports recursive type checking for nested data structures.\n* **Enable/Disable Type Checking**: Users can enable or disable type enforcement on a function by using the enable parameter, defaults to True.\n* **Error Handling**: Raises a TypeError if a type mismatch is detected for either function args or the return value.\n\n## Supported Types\n* **Standard Types**: Such as int, str, float, bool, and other built-in types.\n* **Annotated Data Structures**:\n  1. **list[T]**: Checks that the value is a list and that every element conforms to type T.\n  2. **dict[K, V]**: Checks that the value is a dictionary, and that each key has type K and each value has type V.\n  3. **tuple[T1, T2, ...]**: Checks that the value is a tuple, and that each element has specified type (e.g., tuple[int, str] for (41, 'Saturday')).\n\n## Installation\n\n```bash\npip install typeca\n```\n\n## Usage\nUse **@type_enforcer** to enforce type checks on your functions:\n\n```python\nfrom typeca import type_enforcer \n\n@type_enforcer()\ndef two_num_product(a: int, b: int) -> int:\n    return a * b\n\n# Valid usage\nprint(two_num_product(2, 3))  # Output: 6\n\n# Invalid usage\nprint(two_num_product(2, '3'))  # Raises TypeError\n```\n\n## Examples\n### Example 1: Simple Type Enforcement\n\n```python\n@type_enforcer()\ndef add(a: int, b: int) -> int:\n    return a + b\n\nadd(3, 4)  # Works fine\nadd(3, '4')  # Raises TypeError\n```\n\n### Example 2: Complex Data Structures\n\nSupports lists, dictionaries, and tuples with type annotations:\n\n```python\n@type_enforcer()\ndef process_items(items: list[int]) -> list[int]:\n    return [item * 2 for item in items]\n\nprocess_items([1, 2, 3])  # Works fine\nprocess_items(['a', 'b', 'c'])  # Raises TypeError\n```\n\n### Example 3: Disable Type Enforcement\n\nAt any moment you can disable check to improve performance of the function:\n```python\n@type_enforcer(enable=False)\ndef process_array(*args) -> list[int]:\n    return list(args) * 2\n\nprocess_array(1, 2, 3) # Works without type enforcement\n```",
    "bugtrack_url": null,
    "license": "Copyright (c) 2024 Georgii Viktorov  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": "A type-checking utility for Python functions",
    "version": "0.1.1",
    "project_urls": {
        "Homepage": "https://github.com/GeorgeVictorov/typeca",
        "Issues": "https://github.com/GeorgeVictorov/typeca/issues"
    },
    "split_keywords": [
        "decorators",
        " python types",
        " runtime type checking",
        " static typing",
        " type annotations",
        " type checking",
        " type enforcement",
        " type hints",
        " validation"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "db1e70060442da5792d1bc40a805d8adee341894be2a6b06d85718c8369b6197",
                "md5": "2be97683cbc8aaecf4df0dc26760a550",
                "sha256": "2b6b2a1971cb1e40b8c58ae30aed7556a7be7c9d56e1b11d914d48bd98e1f1e0"
            },
            "downloads": -1,
            "filename": "typeca-0.1.1-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "2be97683cbc8aaecf4df0dc26760a550",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.9",
            "size": 5194,
            "upload_time": "2024-10-28T13:05:33",
            "upload_time_iso_8601": "2024-10-28T13:05:33.031015Z",
            "url": "https://files.pythonhosted.org/packages/db/1e/70060442da5792d1bc40a805d8adee341894be2a6b06d85718c8369b6197/typeca-0.1.1-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "8cd51303031ab208ad71e9efda29e7d631494b8c51a742a2b4a568d7bd7b6842",
                "md5": "384a0d7ebc1da1d47351e43c66ee2cee",
                "sha256": "a6cf82ee6296f3be5f474569881cbf75bcaa8c103b070979d8d3a8fac32a49cb"
            },
            "downloads": -1,
            "filename": "typeca-0.1.1.tar.gz",
            "has_sig": false,
            "md5_digest": "384a0d7ebc1da1d47351e43c66ee2cee",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.9",
            "size": 5908,
            "upload_time": "2024-10-28T13:05:34",
            "upload_time_iso_8601": "2024-10-28T13:05:34.089523Z",
            "url": "https://files.pythonhosted.org/packages/8c/d5/1303031ab208ad71e9efda29e7d631494b8c51a742a2b4a568d7bd7b6842/typeca-0.1.1.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-10-28 13:05:34",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "GeorgeVictorov",
    "github_project": "typeca",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "requirements": [],
    "lcname": "typeca"
}
        
Elapsed time: 0.37457s