Giter VIP home page Giter VIP logo

safe-assert's Introduction

safe-assert

wemake.services test codecov Python Version wemake-python-styleguide

Allows users to write composable asserts that are not stripped away in optimized mode.

Features

  • Single simple, pythonic, fast, tested, typed, documented function. That's it!
  • Because safe_assert is a function, it can be easily composed with other functions
  • Fully typed with annotations and checked with mypy, PEP561 compatible

Installation

pip install safe-assert

Examples

The usage is identical to assert keyword, but a function:

from safe_assert import safe_assert

def sort_positive_numbers(numbers: List[int]) -> List[int]:
    safe_assert(all(num >= 0 for num in numbers), 'found negative')
    return sorted(numbers)

sort_positive_numbers([1, 2, 3])  # => will work
sort_positive_numbers([-1, 2, 3])
# => will fail in runtime with `AssertionError`

How is it different from regular assert? The major one is that it would not be stripped away with -O flag. So, it still allows to write declarative checks that are safe in production.

The second one is that you can compose it as any other regular function. Useful in conjunction with dry-python projects.

Internals

How does it work internally? It internally raises AssertionError that is also used by the assert keyword itself.

See docs to learn more.

License

MIT.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.