Mypy Examples

Here are some mypy example programs. Each example has dynamically typed and equivalent statically typed mypy code side by side. All differences between the variants are highlighted. Note that as mypy is still evolving, the code in the examples may not represent the final mypy syntax.

Note that the examples are somewhat complex. Simple examples tend to be identical to Python and thus pretty boring.

Word frequencies with a dictionary

Mypy with dynamic typing
# Display the frequencies of words in a file.

import sys
import re

if not sys.argv[1:]:
    raise RuntimeError('Usage: wordfreq FILE')

d = {}

s = open(sys.argv[1]).read()
for word in re.sub('\W', ' ', s).split():
    d[word] = d.get(word, 0) + 1

# Use list comprehension
l = [(freq, word) for word, freq in d.items()]

for freq, word in sorted(l):
    print('%-6d %s' % (freq, word))
Mypy with static typing
# Display the frequencies of words in a file.

import sys
import re

if not sys.argv[1:]:
    raise RuntimeError('Usage: wordfreq FILE')

dict<str, int> d = {}

s = open(sys.argv[1]).read()
for word in re.sub('\W', ' ', s).split():
    d[word] = d.get(word, 0) + 1

# Use list comprehension
l = [(freq, word) for word, freq in d.items()]

for freq, word in sorted(l):
    print('%-6d %s' % (freq, word))

In this example we add an explicit type declaration for the variable d, as it is not obvious from the local context.

Simple class

Mypy with dynamic typing
class BankAccount:
    def __init__(self, initial_balance=0):
        self.balance = initial_balance
    def deposit(self, amount):
        self.balance += amount
    def withdraw(self, amount):
        self.balance -= amount
    def overdrawn(self):
        return self.balance < 0

my_account = BankAccount(15)
my_account.withdraw(5)
print(my_account.balance)
Mypy with static typing
class BankAccount:
    void __init__(self, int initial_balance=0):
        self.balance = initial_balance
    void deposit(self, int amount):
        self.balance += amount
    void withdraw(self, int amount):
        self.balance -= amount
    bool overdrawn(self):
        return self.balance < 0

my_account = BankAccount(15)
my_account.withdraw(5)
print(my_account.balance)

In this example we chose to use integers to represent balance. This would be fine in a game, for example, but in other applications a different type would make more sense.

This example was adapted from the Python wiki (with the standard Python license).

Prime number sieve with generators

Mypy with dynamic typing
import itertools

def iter_primes():
     # An iterator of all numbers between 2 and
     # +infinity
     numbers = itertools.count(2)

     # Generate primes forever
     while True:
         # Get the first number from the iterator
         # (always a prime)
         prime = numbers.next()
         yield prime

         # This code iteratively builds up a chain
         # of filters...
         numbers = itertools.ifilter(prime.__rmod__,
                                     numbers)

for p in iter_primes():
    if p > 1000:
        break
    print(p)
Mypy with static typing
import itertools

Iterator<int> iter_primes():
     # An iterator of all numbers between 2 and
     # +infinity
     numbers = itertools.count(2)

     # Generate primes forever
     while True:
         # Get the first number from the iterator
         # (always a prime)
         prime = numbers.next()
         yield prime

         # This code iteratively builds up a chain
         # of filters...
         numbers = itertools.ifilter(prime.__rmod__,
                                     numbers)

for p in iter_primes():
    if p > 1000:
        break
    print(p)

Like the bank account example, this was adapted from the Python wiki.