parameter-checks


Nameparameter-checks JSON
Version 1.0.0 PyPI version JSON
download
home_page
SummaryExtend typehints to include dynamic checks (that might otherwise be dealt with by assertions) in Python.
upload_time2022-12-22 15:26:05
maintainer
docs_urlNone
authorSebastian Müller
requires_python>=3.8
license
keywords parameters return-values checks types dynamic decorator typehint
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            ![Wheel](https://img.shields.io/pypi/wheel/parameter_checks)
![Tests](https://github.com/snimu/parameter-checks/actions/workflows/tests.yml/badge.svg)
![License](https://img.shields.io/github/license/snimu/parameter-checks)
![Python Versions](https://img.shields.io/pypi/pyversions/parameter_checks)


For more info, please [visit the GitHub page of this project](https://github.com/snimu/parameter-checks).

### Basic example


```python 
import parameter_checks as pc


@pc.hints.cleanup   # be left with only type-annotations
@pc.hints.enforce   # enforce the lambda but not the types
def div(a: int, b: pc.annotations.Checks[int, lambda b: b != 0]):
    return a / b 

div(1, 1)   # returns 1.0
div(1, 0)   # raises ValueError
```

As can be seen in this example, this package provides a new type-annotation: 
[pc.annotations.Checks](https://github.com/snimu/parameter-checks#pcannotationschecks)
(it also provides [pc.annotations.Hooks](https://github.com/snimu/parameter-checks#pcannotationshooks), see below). 
Using [@pc.hints.enforce](https://github.com/snimu/parameter-checks#pchintsenforce) on a function 
will enforce the checks given to those annotations (but not the types). 
[@pc.hints.cleanup](https://github.com/snimu/parameter-checks#pchintscleanup) would produce the 
`div.__annotations__` of `{"a": int, "b": int}` in the example above.


### Complex example

```python
import parameter_checks as pc


def decision_boundary(fct, parameter, parameter_name, typehint):
    if type(parameter) is not typehint:
        err_str = f"In function {fct}, parameter {parameter_name}={parameter} " \
                  f"is not of type {typehint}!"
        raise TypeError(err_str)
    
    return 3 + 4 * parameter - parameter ** 2


@pc.hints.enforce
def classify(
        x: pc.annotations.Hooks[float, decision_boundary],
        additional_offset: pc.annotations.Checks[float, lambda b: 0 <= b <= 100] = 0.
) -> bool:
    return (x + additional_offset) >= 0
    

if __name__ == '__main__':
    assert classify(1.) is True   # 6.
    assert classify(2.) is True   # 7.
    assert classify(5.) is False   # -2.
    assert classify(5., 2.) is True   # 0.
    
    classify("not a float!")  # raises TypeError
    classify(1., -1.)   # raises ValueError
```

For additional comments on this example (and much more), 
[visit the GitHub page of this project](https://github.com/snimu/parameter-checks#hooks-example-2).

            

Raw data

            {
    "_id": null,
    "home_page": "",
    "name": "parameter-checks",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.8",
    "maintainer_email": "",
    "keywords": "parameters,return-values,checks,types,dynamic,decorator,typehint",
    "author": "Sebastian M\u00fcller",
    "author_email": "sebastian.nicolas.mueller@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/b9/0b/7a02003923dfa7927f46c56134b52b99e2264edb241282bb9f3acb4e650f/parameter-checks-1.0.0.tar.gz",
    "platform": null,
    "description": "![Wheel](https://img.shields.io/pypi/wheel/parameter_checks)\n![Tests](https://github.com/snimu/parameter-checks/actions/workflows/tests.yml/badge.svg)\n![License](https://img.shields.io/github/license/snimu/parameter-checks)\n![Python Versions](https://img.shields.io/pypi/pyversions/parameter_checks)\n\n\nFor more info, please [visit the GitHub page of this project](https://github.com/snimu/parameter-checks).\n\n### Basic example\n\n\n```python \nimport parameter_checks as pc\n\n\n@pc.hints.cleanup   # be left with only type-annotations\n@pc.hints.enforce   # enforce the lambda but not the types\ndef div(a: int, b: pc.annotations.Checks[int, lambda b: b != 0]):\n    return a / b \n\ndiv(1, 1)   # returns 1.0\ndiv(1, 0)   # raises ValueError\n```\n\nAs can be seen in this example, this package provides a new type-annotation: \n[pc.annotations.Checks](https://github.com/snimu/parameter-checks#pcannotationschecks)\n(it also provides [pc.annotations.Hooks](https://github.com/snimu/parameter-checks#pcannotationshooks), see below). \nUsing [@pc.hints.enforce](https://github.com/snimu/parameter-checks#pchintsenforce) on a function \nwill enforce the checks given to those annotations (but not the types). \n[@pc.hints.cleanup](https://github.com/snimu/parameter-checks#pchintscleanup) would produce the \n`div.__annotations__` of `{\"a\": int, \"b\": int}` in the example above.\n\n\n### Complex example\n\n```python\nimport parameter_checks as pc\n\n\ndef decision_boundary(fct, parameter, parameter_name, typehint):\n    if type(parameter) is not typehint:\n        err_str = f\"In function {fct}, parameter {parameter_name}={parameter} \" \\\n                  f\"is not of type {typehint}!\"\n        raise TypeError(err_str)\n    \n    return 3 + 4 * parameter - parameter ** 2\n\n\n@pc.hints.enforce\ndef classify(\n        x: pc.annotations.Hooks[float, decision_boundary],\n        additional_offset: pc.annotations.Checks[float, lambda b: 0 <= b <= 100] = 0.\n) -> bool:\n    return (x + additional_offset) >= 0\n    \n\nif __name__ == '__main__':\n    assert classify(1.) is True   # 6.\n    assert classify(2.) is True   # 7.\n    assert classify(5.) is False   # -2.\n    assert classify(5., 2.) is True   # 0.\n    \n    classify(\"not a float!\")  # raises TypeError\n    classify(1., -1.)   # raises ValueError\n```\n\nFor additional comments on this example (and much more), \n[visit the GitHub page of this project](https://github.com/snimu/parameter-checks#hooks-example-2).\n",
    "bugtrack_url": null,
    "license": "",
    "summary": "Extend typehints to include dynamic checks (that might otherwise be dealt with by assertions) in Python.",
    "version": "1.0.0",
    "split_keywords": [
        "parameters",
        "return-values",
        "checks",
        "types",
        "dynamic",
        "decorator",
        "typehint"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "md5": "1aebe019f21e9e50edbd00284fef6eeb",
                "sha256": "d07ff8a84d45f902a324d50c97351522151e054c9e079b029efeb94450b70eb2"
            },
            "downloads": -1,
            "filename": "parameter_checks-1.0.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "1aebe019f21e9e50edbd00284fef6eeb",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.8",
            "size": 5406,
            "upload_time": "2022-12-22T15:26:03",
            "upload_time_iso_8601": "2022-12-22T15:26:03.708765Z",
            "url": "https://files.pythonhosted.org/packages/71/ff/e48678d085ffbca8de01f23df7c6ccf4ffc622aef68e71ddc1428f2cd7d5/parameter_checks-1.0.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "md5": "c44290efcc296dd54f4d1fdd4db74eff",
                "sha256": "f09b3043ea45f65376761ec6c1f78ac59695feb39f4e8a14e30d173e9d08c7e1"
            },
            "downloads": -1,
            "filename": "parameter-checks-1.0.0.tar.gz",
            "has_sig": false,
            "md5_digest": "c44290efcc296dd54f4d1fdd4db74eff",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8",
            "size": 9344,
            "upload_time": "2022-12-22T15:26:05",
            "upload_time_iso_8601": "2022-12-22T15:26:05.365362Z",
            "url": "https://files.pythonhosted.org/packages/b9/0b/7a02003923dfa7927f46c56134b52b99e2264edb241282bb9f3acb4e650f/parameter-checks-1.0.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2022-12-22 15:26:05",
    "github": false,
    "gitlab": false,
    "bitbucket": false,
    "lcname": "parameter-checks"
}
        
Elapsed time: 0.05682s