Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
MYPY: A PYTHON VARIANTWITH SEAMLESS DYNAMIC  AND STATIC TYPING                 Jukka Lehtosalo University of Cambridge Com...
SPEAKER BIO2000-2006 Software engineer (QPR Software and Kielikone)2007-2009 Development manager (Kielikone)2009→ PhD rese...
mypy is an experimental    Python variant    with seamlessdynamic and static typing
Mostly, but not 100% compatible with       Python           mypy is an experimental               Python variant          ...
Research project,              in developmentmypy is an experimental    Python variant    with seamlessdynamic and static ...
mypy is an experimental              Python variant              with seamless          dynamic and static typing  Freely ...
LANGUAGE GAP    Python         Java, C++, C, C# Scripting, webdevelopment, UI,    Heavy lifting  prototyping
VISION     mypy                                 mypy (dynamic typing)                             (static typing)    Pytho...
WHAT DOES IT LOOK LIKE?mypy, dynamic typing    mypy, static typingdef fib(n):             void fib(int n):    a, b = 0, 1 ...
WHAT DOES IT LOOK LIKE? mypy, dynamic typing    mypy, static typing def fib(n):             void fib(int n):     a, b = 0,...
WHAT DOES IT LOOK LIKE?mypy, dynamic typing           mypy, static typingdef fib(n):                    void fib(int n):  ...
WHAT DOES IT LOOK LIKE?mypy, dynamic typing       mypy, static typingdef fib(n):                void fib(int n):    a, b =...
DYNAMIC VS STATIC TYPINGmypy, dynamic typingdef err():    return 1 + ‘x’     # no error (not called)
DYNAMIC VS STATIC TYPINGmypy, dynamic typingdef err():    return 1 + ‘x’     # runtime error (when called)err()
DYNAMIC VS STATIC TYPINGmypy, dynamic typingdef err():    return 1 + ‘x’     # runtime error (when called)err()mypy, stati...
TYPESint, str, bool, MyClass        any     simple type                     dynamic (duck) typelist<int>, dict<str, int>  ...
EXAMPLE SCRIPTimport sys, reif not sys.argv[1:]:    raise RuntimeError(Usage: wordfreq FILE)dict<str, int> d = {}s = open(...
EXAMPLE SCRIPTimport sys, reif not sys.argv[1:]:    raise RuntimeError(Usage: wordfreq FILE)dict<str, int> d = {}s = open(...
EXAMPLE CLASS             dynamic typingclass BankAccount:    def __init__(self, initial_balance=0):        self.balance =...
EXAMPLE CLASS               static typingclass BankAccount:    void __init__(self, int initial_balance=0):        self.bal...
EXAMPLE CLASS               static typingclass BankAccount:    void __init__(self, int initial_balance=0):        self.bal...
MYPY LIBRARY SUPPORT                Mypy program Mypy VM      Native mypy librariesTranslation                object mappi...
NO GILCPython uses Global Interpreter Lockonly one thread can usually run at a time
NO GIL    CPython uses Global Interpreter Lock    only one thread can usually run at a time     Not ok in the multicore er...
WHY STATIC TYPING?
PERFORMANCE                                          mypyspeed                                      static typing,        ...
TYPE CHECKINGStatic type checker pinpoints errors in yourprogram, before running itsome_function(foo[n], self.bar(x))     ...
TOOL SUPPORTStatic type information makes many tools more powerful: • IDEs   with reliable and precise code completion • R...
SEAMLESS DYNAMIC AND    STATIC TYPING        Use the best tool for the job Use dynamic typing when it makes sense  Use sta...
Static typing may be a win • if efficiency is important • if your project is large or complex • if you need to maintain cod...
Dynamically typed      programStatically typed libraries
hot spot             Sprinkle static types           for better performance
Statically typed interfaces   Dynamically typed   implementations
Evolve programs from dynamic to static typingv 0.5         v 1.0                 v 2.0
IMPLEMENTATION PLAN                All dates tentative!Phase 1(2012)Mypy type checker + mypy-to-Python translatorPhase 2 (...
MYPY VS OTHER PROJECTS
CYTHON•   Cython is a Python variant that   cpdef int sum3d(int[:, :, :] a):    compiles to Python C modules          cdef...
MYPY VS CYTHON   Mypy (planned)                CythonPowerful type system       Simple type system                       S...
SHED SKIN• Compiler   for a statically typed subset of Python• Large   speedups to some programs (100x or more)• Code    l...
MYPY VS SHED SKIN      Mypy (planned)                    Shed SkinLarge set of Python features   Restricted Python feature...
MYPY VS RPYTHON• RPython   is a Python subset used by PyPy to implement the JIT compiler• RPython   uses whole-program typ...
CONCLUSION       Tell me what you think!All help and feedback is very welcome!              Web site:http://www.mypy-lang....
ANDROID: OVERVIEW• Over   500 M Android devices activated• Android   native APIs mostly based on Java (Dalvik)• Performanc...
MYPY ON ANDROID?   Android Wishlist   1. Python syntax + libs               Can’t have all of these   2. Good performance ...
Upcoming SlideShare
Loading in …5
×

Mypy pycon-fi-2012

848 views

Published on

mypy: A Python Variant with Seamless Dynamic and Static Typing

  • Be the first to comment

Mypy pycon-fi-2012

  1. 1. MYPY: A PYTHON VARIANTWITH SEAMLESS DYNAMIC AND STATIC TYPING Jukka Lehtosalo University of Cambridge Computer Laboratory
  2. 2. SPEAKER BIO2000-2006 Software engineer (QPR Software and Kielikone)2007-2009 Development manager (Kielikone)2009→ PhD research (University of Cambridge)Projects spanning web apps, Windows apps, mobileapps,distributed apps, document databases, information retrieval,pattern recognition, natural language processing, data mining,garbage collectors, compilers, localization tools, automated testingtools etc.
  3. 3. mypy is an experimental Python variant with seamlessdynamic and static typing
  4. 4. Mostly, but not 100% compatible with Python mypy is an experimental Python variant with seamless dynamic and static typing
  5. 5. Research project, in developmentmypy is an experimental Python variant with seamlessdynamic and static typing
  6. 6. mypy is an experimental Python variant with seamless dynamic and static typing Freely mix static and dynamic (duck) typesCombine the benefits of static and dynamic typing
  7. 7. LANGUAGE GAP Python Java, C++, C, C# Scripting, webdevelopment, UI, Heavy lifting prototyping
  8. 8. VISION mypy mypy (dynamic typing) (static typing) Python Java, C++, C, C# Scripting, webdevelopment, UI, Heavy lifting prototyping
  9. 9. WHAT DOES IT LOOK LIKE?mypy, dynamic typing mypy, static typingdef fib(n): void fib(int n): a, b = 0, 1 a, b = 0, 1 while a < n: while a < n: print(a) print(a) a, b = b, a+b a, b = b, a+b
  10. 10. WHAT DOES IT LOOK LIKE? mypy, dynamic typing mypy, static typing def fib(n): void fib(int n): a, b = 0, 1 a, b = 0, 1 while a < n: while a < n: print(a) print(a) a, b = b, a+b a, b = b, a+b Dynamicallytyped function
  11. 11. WHAT DOES IT LOOK LIKE?mypy, dynamic typing mypy, static typingdef fib(n): void fib(int n): a, b = 0, 1 a, b = 0, 1 while a < n: while a < n: print(a) Static print(a) a, b = b, a+b a, b = b, a+b type signature
  12. 12. WHAT DOES IT LOOK LIKE?mypy, dynamic typing mypy, static typingdef fib(n): void fib(int n): a, b = 0, 1 a, b = 0, 1 while a < n: while a < n: print(a) print(a) a, b = b, a+b a, b = b, a+b Type inference
  13. 13. DYNAMIC VS STATIC TYPINGmypy, dynamic typingdef err(): return 1 + ‘x’ # no error (not called)
  14. 14. DYNAMIC VS STATIC TYPINGmypy, dynamic typingdef err(): return 1 + ‘x’ # runtime error (when called)err()
  15. 15. DYNAMIC VS STATIC TYPINGmypy, dynamic typingdef err(): return 1 + ‘x’ # runtime error (when called)err()mypy, static typingint err(): return 1 + ‘x’ # compile (static) error
  16. 16. TYPESint, str, bool, MyClass any simple type dynamic (duck) typelist<int>, dict<str, int> func<str, int> generic type function typetuple<int, str> tuple type
  17. 17. EXAMPLE SCRIPTimport sys, reif 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 comprehensionl = [(freq, word) for word, freq in d.items()]for freq, word in sorted(l): print(%-6d %s % (freq, word))
  18. 18. EXAMPLE SCRIPTimport sys, reif 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 comprehensionl = [(freq, word) for word, freq in d.items()]for freq, word in sorted(l): print(%-6d %s % (freq, word))
  19. 19. EXAMPLE CLASS dynamic typingclass 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 < 0my_account = BankAccount(15)my_account.withdraw(5)print(my_account.balance) (source: Python Wiki)
  20. 20. EXAMPLE CLASS static typingclass 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 < 0my_account = BankAccount(15)my_account.withdraw(5)print(my_account.balance)
  21. 21. EXAMPLE CLASS static typingclass 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 < 0my_account = BankAccount(15)my_account.withdraw(5)print(my_account.balance)
  22. 22. MYPY LIBRARY SUPPORT Mypy program Mypy VM Native mypy librariesTranslation object mapping layerCPython VM Python libraries
  23. 23. NO GILCPython uses Global Interpreter Lockonly one thread can usually run at a time
  24. 24. NO GIL CPython uses Global Interpreter Lock only one thread can usually run at a time Not ok in the multicore era!Mypy will have a new ✝, custom VM without GIL. ✝ Based on the Alore VM (http://www.alorelang.org/)
  25. 25. WHY STATIC TYPING?
  26. 26. PERFORMANCE mypyspeed static typing, ahead-of-time compiler PyPy JIT compiler CPython interpreter t (conceptual, not in scale)
  27. 27. TYPE CHECKINGStatic type checker pinpoints errors in yourprogram, before running itsome_function(foo[n], self.bar(x)) ^StaticError: Argument has invalid type list<str>, list<int> expected➠ Less debugging➠ Easier maintenance and refactoring
  28. 28. TOOL SUPPORTStatic type information makes many tools more powerful: • IDEs with reliable and precise code completion • Refactoring tools • Static analysis and linting tools ➠ Better productivity and quality
  29. 29. SEAMLESS DYNAMIC AND STATIC TYPING Use the best tool for the job Use dynamic typing when it makes sense Use static typing when it makes sense As real programs are not uniform: use both
  30. 30. Static typing may be a win • if efficiency is important • if your project is large or complex • if you need to maintain code for several yearsEven when the above are true, dynamic typing is useful• for tests• for the user interface• for glue code• for extensions, scripts and plugins
  31. 31. Dynamically typed programStatically typed libraries
  32. 32. hot spot Sprinkle static types for better performance
  33. 33. Statically typed interfaces Dynamically typed implementations
  34. 34. Evolve programs from dynamic to static typingv 0.5 v 1.0 v 2.0
  35. 35. IMPLEMENTATION PLAN All dates tentative!Phase 1(2012)Mypy type checker + mypy-to-Python translatorPhase 2 (2013)Compiler to native code (C/LLVM back end), new VMPhase N (?)More Python featuresIDEJVM back end?Android port? (etc.)
  36. 36. MYPY VS OTHER PROJECTS
  37. 37. CYTHON• Cython is a Python variant that cpdef int sum3d(int[:, :, :] a): compiles to Python C modules cdef int total = 0 I = a.shape[0] J = a.shape[1]• Cython can speed up tight K = a.shape[2] loops by a lot (100 x or more) for i in range(I): by using type declarations for j in range(J): for k in range(K): total += a[i, j, k]• Cython programs can also call return total C functions directly (adapted from Cython Users Guide)• Similar to Pyrex
  38. 38. MYPY VS CYTHON Mypy (planned) CythonPowerful type system Simple type system Speedups mainly to low-levelGeneral code speedup code New, efficient VM CPython VM No GIL GIL still a problem
  39. 39. SHED SKIN• Compiler for a statically typed subset of Python• Large speedups to some programs (100x or more)• Code looks like Python (explicit type declarations not needed) • Whole-program type inference
  40. 40. MYPY VS SHED SKIN Mypy (planned) Shed SkinLarge set of Python features Restricted Python feature set Dynamic and static typing Static typing only Modular, (relatively) fast Very slow compilation for compiles large programs Python lib access layer Not many Python libs
  41. 41. MYPY VS RPYTHON• RPython is a Python subset used by PyPy to implement the JIT compiler• RPython uses whole-program type inference (like Shed Skin)• Differences vs mypy: similar to mypy vs Shed Skin
  42. 42. CONCLUSION Tell me what you think!All help and feedback is very welcome! Web site:http://www.mypy-lang.org Thank you!
  43. 43. ANDROID: OVERVIEW• Over 500 M Android devices activated• Android native APIs mostly based on Java (Dalvik)• Performance important for apps • Cheap devices with low-end CPUs • UI responsiveness key to good user experience • Battery conservation
  44. 44. MYPY ON ANDROID? Android Wishlist 1. Python syntax + libs Can’t have all of these 2. Good performance right now 3. Easy native API access• Mypy is a potential solution (by compiling to Dalvik)• But there’s a lot of work to do...

×