Question

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]

Was it helpful?

Solution

From python 3, the ability to use type annotation was introduced into the python standard with PEP 3017.

Fast-forward to python 3.5 and PEP 0484 builds on this to introduce type hinting along with the typing module which enables one to specify the types for a variable or the return type of a function.

from typing import Iterator

def fib(n: int) -> Iterator[int]:
    a, b = 0, 1
    while a < n:
        yield a
        a, b = b, a + b

Above example taken from https://pawelmhm.github.io

According to the 484 notes:

While these annotations are available at runtime through the usual __annotations__ attribute, no type checking happens at runtime. Instead, the proposal assumes the existence of a separate off-line type checker which users can run over their source code voluntarily. Essentially, such a type checker acts as a very powerful linter. (While it would of course be possible for individual users to employ a similar checker at run time for Design By Contract enforcement or JIT optimization, those tools are not yet as mature.)

tl;dr

Although python provides this form of "static typing", it is not enforced at run time and the python interpreter simply ignores any type specifications you have provided and will still use duck typing to infer types. Therefore, it is up to you to find a linter which will detect any issues with the types.

Furthermore

The motivation for including typing in the python standard was mostly influenced by mypy, so it might be worth checking them out. They also provide examples which may prove useful.

OTHER TIPS

The short answer is no. What you are asking for is deeply built into Python, and can't be changed without changing the language so drastically that is wouldn't be Python.

I'm assuming you don't like variables that are re-typed when re-assigned to? You might consider other ways to check for this if this is a problem with your code.

No You can not have cake and eat cake.

Python is great because its dynamically typed! Period. (That's why it have such nice standard library too)

There is only 2 advantages of statically typed languages 1) speed - when algorithms are right to begin with and 2) compilation errors

As for 1)

  • Use PyPi,
  • Profile,
  • Use ctype libs for great performance.

Its typical to have only 10% or less code that is performance critical. All that other 90%? Enjoy advantages of dynamic typing.

As for 2)

  • Use Classes (And contracts)
  • Use Unit Testing
  • Use refactoring
  • Use good code editor

Its typical to have data NOT FITTING into standard data types, which are too strict or too loose in what they allow to be stored in them. Make sure that You validate Your data on Your own.

Unit Testing is must have for algorithm testing, which no compiler can do for You, and should catch any problems arising from wrong data types (and unlike compiler they are as fine grained as you need them to be)

Refactoring solves all those issues when You are not sure if given changes wont break Your code (and again, strongly typed data can not guarantee that either).

And good code editor can solve so many problems... Use Sublime Text for a while. And then You will know what I mean.

(To be sure, I do not give You answer You want to have. But rather I question Your needs, especially those that You did not included in Your question)

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.

Since comments are limited...

As an interpreted language Python is by definition weakly typed. This is not a bad thing more as a control in place for the programmer to preempt potential syntactical bugs, but in truth that won't stop logical bugs from happening any less, and thus makes the point moot.

Even though the paper on RPython makes it's point, it is focused on Object Oriented Programming. You must bear in mind that Python is more an amalgamation of OOP and Functional Programming, likely others too.

I encourage reading of this page, it is very informative.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top