Python 2 and 3 compatibility testing via optional run-time type checking

Similar documents
Introduction to Python for Text Analysis

MYPY: A PYTHON VARIANT WITH SEAMLESS DYNAMIC AND STATIC TYPING. Jukka Lehtosalo University of Cambridge Computer Laboratory

Static vs. Dynamic. Lecture 10: Static Semantics Overview 1. Typical Semantic Errors: Java, C++ Typical Tasks of the Semantic Analyzer

Introduction to Python

6.170 Tutorial 3 - Ruby Basics

Semester Review. CSC 301, Fall 2015

10 awesome features of Python that you can't use because you refuse to upgrade to Python 3

CS 378 Big Data Programming. Lecture 9 Complex Writable Types

Introduction to Python

The C Programming Language course syllabus associate level

Java Interview Questions and Answers

Developing a Web Server Platform with SAPI Support for AJAX RPC using JSON

Serializing Data with Protocol Buffers. Vinicius Vielmo Cogo Smalltalks, DI, FC/UL. February 12, 2014.

Type Classes with Functional Dependencies

Crash Course in Java

Introduction to Python

Apache Thrift and Ruby

Specialized Programme on Web Application Development using Open Source Tools

Building and breaking a Python sandbox

e ag u g an L g ter lvin v E ram Neal G g ro va P Ja

CS 111 Classes I 1. Software Organization View to this point:

CommentTemplate: A Lightweight Code Generator for Java built with Eclipse Modeling Technology

Software Engineering Techniques

Using EDA Databases: Milkyway & OpenAccess

Java (12 Weeks) Introduction to Java Programming Language

Crash Dive into Python

The Clean programming language. Group 25, Jingui Li, Daren Tuzi

Moving from CS 61A Scheme to CS 61B Java

#820 Computer Programming 1A

CSE 373: Data Structure & Algorithms Lecture 25: Programming Languages. Nicki Dell Spring 2014

2/1/2010. Background Why Python? Getting Our Feet Wet Comparing Python to Java Resources (Including a free textbook!) Hathaway Brown School

Dart a modern web language

CSE 307: Principles of Programming Languages

Programming in Python. Basic information. Teaching. Administration Organisation Contents of the Course. Jarkko Toivonen. Overview of Python

Overview. Elements of Programming Languages. Advanced constructs. Motivating inner class example

ML for the Working Programmer

Chapter 3 Writing Simple Programs. What Is Programming? Internet. Witin the web server we set lots and lots of requests which we need to respond to

CSCE 110 Programming I Basics of Python: Variables, Expressions, and Input/Output

IVR Studio 3.0 Guide. May Knowlarity Product Team

LEARNING TO PROGRAM WITH PYTHON. Richard L. Halterman

Mrs: MapReduce for Scientific Computing in Python

Semantic Analysis: Types and Type Checking

Practical Generic Programming with OCaml

Profiling, debugging and testing with Python. Jonathan Bollback, Georg Rieckh and Jose Guzman

Handout 1. Introduction to Java programming language. Java primitive types and operations. Reading keyboard Input using class Scanner.

Objects and classes. Objects and classes. Jarkko Toivonen (CS Department) Programming in Python 1

pyownet Documentation

Chapter 1. Dr. Chris Irwin Davis Phone: (972) Office: ECSS CS-4337 Organization of Programming Languages

CSE 130 Programming Language Principles & Paradigms

Crash Dive into Python

Lecture 9. Semantic Analysis Scoping and Symbol Table

opalang - Rapid & Secure Web Development

1/20/2016 INTRODUCTION

Computer Programming I

Python Loops and String Manipulation

Teaching an Introductory Computer Science Sequence with Python

Analyse et Conception Formelle. Lesson 5. Crash Course on Scala

Instructor: Betty O Neil

The programming language C. sws1 1

CIS 192: Lecture 10 Web Development with Flask

Object-Oriented Design Lecture 4 CSU 370 Fall 2007 (Pucella) Tuesday, Sep 18, 2007

socketio Documentation

The gradual typing approach to mixing static and dynamic typing

Chapter 2: Elements of Java

Chapter 5 Names, Bindings, Type Checking, and Scopes

Specialized Programme on Web Application Development using Open Source Tools

Supporting Data Set Joins in BIRT

TypeScript. Language Specification. Version 1.4

Organization of Programming Languages CS320/520N. Lecture 05. Razvan C. Bunescu School of Electrical Engineering and Computer Science

Operator Overloading. Lecture 8. Operator Overloading. Running Example: Complex Numbers. Syntax. What can be overloaded. Syntax -- First Example

Intro to scientific programming (with Python) Pietro Berkes, Brandeis University

CS506 Web Design and Development Solved Online Quiz No. 01

C++ Programming Language

University of Hull Department of Computer Science. Wrestling with Python Week 01 Playing with Python

Topics. Parts of a Java Program. Topics (2) CS 146. Introduction To Computers And Java Chapter Objectives To understand:

How to Design and Create Your Own Custom Ext Rep

History OOP languages Year Language 1967 Simula Smalltalk

El Dorado Union High School District Educational Services

Computer Programming I & II*

CS 141: Introduction to (Java) Programming: Exam 1 Jenny Orr Willamette University Fall 2013

Bridging Python to C++ - and vice-versa

C Compiler Targeting the Java Virtual Machine

Pemrograman Dasar. Basic Elements Of Java

Introduction to Java

Django & Python 3. Aymeric Augustin PyConFR - September 16th, 2012

Language Evaluation Criteria. Evaluation Criteria: Readability. Evaluation Criteria: Writability. ICOM 4036 Programming Languages

CRASH COURSE PYTHON. Het begint met een idee

Perdix: A Query Language for Security Logs

AP Computer Science Java Subset

TECHNOLOGY Computer Programming II Grade: 9-12 Standard 2: Technology and Society Interaction

What is An Introduction

An Overview of Java. overview-1

Getting Started with the Internet Communications Engine

Programming and Software Development CTAG Alignments

Introduction to Java. CS 3: Computer Programming in Java

Introduction to Object-Oriented Programming

Transcription:

Python 2 and 3 compatibility testing via optional run-time type checking Raoul-Gabriel Urma Work carried out during a Google internship & PhD https://github.com/google/pytypedecl

Python 2 vs. Python 3

Valid in Python 2 def pivot_index(len): return len/2 list = [4, 1, 3, 10, 4] index = pivot_index(len(list)) print(list[index])

Error in Python 3 File "integer.py", line 6, in <module> print(list[index]) TypeError: list indices must be integers, not float

Valid in Python 2 def concat(l1, l2): return l1 + l2 d = {1:'a', 2:'b'} l = concat(d.keys(), d.values())

Error in Python 3 File "dict.py", line 5, in <module> l = concat(d.keys(), d.values()) File "dict.py", line 2, in concat return l1 + l2 TypeError: unsupported operand type(s) for +: 'dict_keys' and 'dict_values'

Valid in Python 2 def assertequals(s1, s2): assert(s1 == s2) def slurp(filename, mode): return open(filename, mode).read() data = slurp("data.txt","rb") # "hello" assertequals("hello", data)

Error in Python 3 File "open.py", line 8, in <module> assertequals("hello", data) File "open.py", line 2, in assertequals assert(s1 == s2) AssertionError

Python 2 to Python 3 complaints On unicode: this will be maybe an impossible task to do because of one feature: unicode strings. We currently extensively use unicode [...]. On using 2to3: the script doesn t work with python 3. I have tried 2to3 script [...] but it still doesn t work. On using 2to3: this is something that 2to3 does not pick up and most likely you will not either. At least in my situation it took me a long time to track down the problem because some other implicit conversion was happening at another place.

Our idea Before After File "open.py", line 8, in <module> TYPE_ERROR: Function: slurp, returns <class 'bytes'> assertequals("hello", data) File "open.py", line 2, in assertequals but EXPECTED <class 'str'> assert(s1 == s2) AssertionError def assertequals(s1: str, s2: str) -> bool def slurp(filename: str, mode: str) -> str

Pytypedecl

In a nutshell Type declaration language for Python Optional run-time type checking using type annotations Design constraints: Engineering: integrates naturally into existing development infrastructure (no external tools) Usability: syntax familiar to Python programmers

Normal Python code class Logger: def log(messages, buffer):... def setstatus(status):... def find(elements, key):...

What we are adding exceptions qualified parametric types class Logger: def log(messages: list<str>, buffer: Readable and Writeable) -> None raises IOException overloading def log(messages: list<str>) -> None def setstatus(status: int or str) interface Readable: union intersection def read interfaces interface Writable: def write parametric polymorphism and type bounds def <T <: UserId> find(elements: list<t>, key<t>) -> T? none-able type

Type checking

Basic blueprint qualified parametric types container integrity: assume iterator provided to type check elements structure integrity: optional iterator to type check integrity parametric types unification system with inequations resolved online extend iscompatible() with a type parameter table overloading at least one signature valid

Implementation

Prototype Type declaration language types defined in an external file parser implemented with Python Lex-Yacc Type checking Implemented as a Python library checker.py Project is open source http://www.github.com/google/pytypedecl

How to use # emailer.py # emailer.pytd import sys class Emailer: def MakeAnnouncement(cls, emails:list[str]) -> None from pytypedecl import checker def SendEmail(cls, addr:str) -> None class Emailer(object): @classmethod def MakeAnnouncement(cls, emails): for addr in emails: cls.sendemail(addr) add two lines to opt for type checking @classmethod def SendEmail(cls, addr): checker.checkfromfile(sys.modules[ name ], file + "td")

Currently in the pipeline Type system formalisation Interaction between parametric polymorphism, overloading and generators Evaluation Case study: porting a Python 2 project to Python 3 using type annotations Performance impact of implementation

Questions? Raoul-Gabriel Urma raoul@urma.com @raouluk Java 8 in Action: Lambdas, Streams and functional-style programming