Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Explicitly typed version of Python?

I rather like Python's syntactic sugar; and standard library functions.

However the one feature which I dislike; is implicit typing.

Is there a distribution of Python with explicit typing; which is still compatible with e.g.: packages on PyPi?

[I was looking into RPython]

like image 639
A T Avatar asked Mar 13 '14 07:03

A T


1 Answers

Now in 2021, there's a library called Deal that not only provides a robust static type checker, but also allows you to specify pre- and post-conditions, loop invariants, explicitly state expectations regarding exceptions and IO/side-effects, and even formally prove correctness of code (for an albeit small subset of Python).

Here's an example from their GitHub:

# the result is always non-negative
@deal.post(lambda result: result >= 0)
# the function has no side-effects
@deal.pure
def count(items: List[str], item: str) -> int:
    return items.count(item)

# generate test function
test_count = deal.cases(count)

Now we can:

  • Run python3 -m deal lint or flake8 to statically check errors.
  • Run python3 -m deal test or pytest to generate and run tests.
  • Just use the function in the project and check errors in runtime.
like image 176
vasilescur Avatar answered Oct 16 '22 01:10

vasilescur