Skip to content
GitLab
Projects Groups Snippets
  • /
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
    • Contribute to GitLab
  • Sign in / Register
  • A awesome-python
  • Project information
    • Project information
    • Activity
    • Labels
    • Members
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
  • Issues 13
    • Issues 13
    • List
    • Boards
    • Service Desk
    • Milestones
  • Merge requests 317
    • Merge requests 317
  • CI/CD
    • CI/CD
    • Pipelines
    • Jobs
    • Schedules
  • Deployments
    • Deployments
    • Environments
    • Releases
  • Packages and registries
    • Packages and registries
    • Package Registry
    • Infrastructure Registry
  • Monitor
    • Monitor
    • Incidents
  • Analytics
    • Analytics
    • Value stream
    • CI/CD
    • Repository
  • Wiki
    • Wiki
  • Snippets
    • Snippets
  • Activity
  • Graph
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
Collapse sidebar
  • Vinta Chen
  • awesome-python
  • Merge requests
  • !1343

Add Dynamic Analysis section

  • Review changes

  • Download
  • Email patches
  • Plain diff
Closed Administrator requested to merge github/fork/allrod5/patch-1 into master Sep 05, 2019
  • Overview 0
  • Commits 1
  • Pipelines 0
  • Changes 1

Created by: allrod5

Add a Dynamic Analysis section and include parameters-validation lib to it.

What is this Python project?

parameters-validation lib eases function parameters validation regarding type, content and/or any other custom validations one may wish to perform.

It leverages type-hint annotations to provide a clean and unobstructive way to adopt the Look Before You Leap principle when you need to.

What's the difference between this Python project and similar ones?

There are no similar projects currently. Usually, the developer will write their own validation helper functions and manually call'em. While this is fine, the end result pollutes function bodies with validation actions that are not the core responsibility of a function. Nevertheless, there are simple validations (such as checking if a string is not blank or that a number is not negative) that will be kinda frequent and repeated all over different projects while performing exactly the same functionality.

parameters-validation lib enables removing the validation logic from the function body, explicitly stating validations upfront in the function signature and provides ready-to-use common validations.

Here a simple comparison between runtime validations with and without using parameters-validation:

"""
Using parameters-validation lib: clean and explicit way of declaring
and performing runtime validations
"""
@validate_parameters
def register(
    token: strongly_typed(AuthToken),
    name: non_blank(str),
    age: non_negative(int),
    nickname: no_whitespaces(non_empty(str)),
    bio: str,
):
    # do register
"""
Adding validation code directly at the function body: explicit on the
validations but pollutes the function body
"""
@validate_parameters
def register(
    token: AuthToken,
    name: str,
    age: int,
    nickname: str,
    bio: str,
):
    if not isinstance(token, AuthToken):
        raise TypeError("Parameter 'token' must be of type 'AuthToken'")
    if not bool(name and name.strip()):
        raise ValueError("Parameter 'name' cannot be blank nor empty")
    if age < 0:
        raise ValueError("Parameter 'age' cannot be negative")
    if not nickname:
        raise ValueError("Paramater 'nickname' cannot be empty")
    if " " in nickname:
        raise ValueError("Parameter 'nickname' cannot contain whitespaces")
    # do register
"""
Adding validation code in a helper function: implicit on the
validations pollutes outer scope of the function with the companion
helper function. The companion helper is harder to keep in sync
with the register function and duplicates parameters declarations
"""
_validate_register_parameters(
    token: AuthToken,
    name: str,
    age: int,
    nickname: str,
    bio: str,
):
    if not isinstance(token, AuthToken):
        raise TypeError("Parameter 'token' must be of type 'AuthToken'")
    if not bool(name and name.strip()):
        raise ValueError("Parameter 'name' cannot be blank nor empty")
    if age < 0:
        raise ValueError("Parameter 'age' cannot be negative")
    if not nickname:
        raise ValueError("Paramater 'nickname' cannot be empty")
    if " " in nickname:
        raise ValueError("Parameter 'nickname' cannot contain whitespaces")

@validate_parameters
def register(
    token: AuthToken,
    name: str,
    age: int,
    nickname: str,
    bio: str,
):
    _validate_register_parameters(token, name, age, nickname, bio)
    # do register

--

Anyone who agrees with this pull request could vote for it by adding a 👍 to it, and usually, the maintainer will merge it when votes reach 20.

Assignee
Assign to
Reviewers
Request review from
Time tracking
Source branch: github/fork/allrod5/patch-1