Python typing module

Preview:

Citation preview

The typing module in Python 3.5

Ryan Blunden@ryan_blunden

A N O T H E R C O M P E L L I N G R E A S O N T O S T A R T U S I N G P Y T H O N 3 !

Recently returned to Australia after three years at LinkedIn in

Silicon Valley.

Now running RabbitBird as a freelance developer, agile coach and technical

trainer.

Who am I?

What do you know about Python and its approach to typing?

Is there anything specific you want to learn from this session?

I F  Y O U ’ R E N O T A N A D VA N C E D P Y T H O N D E V E L O P E R , D O N ’ T W O R R Y.

About you!

• Background and goals of the typing module

• Syntax

• Live coding demonstration

• Tips for getting started with the typing module in your own projects

• Review questions

• Q&A.

Agenda

By the end of this session, you will be able to:

• Explain why the typing module is useful

• Describe its basic capabilities

• Apply type hinting to a simple function and run a type checker against it

• Start thinking about how to use it in your own projects.

Session objectives

Any Questions?

?

?

Types!A C R A S H C O U R S E I N B A S I C T Y P I N G T E R M I N O L O G Y F O R P Y T H O N

a = 5  # a starts as a number. a = ‘hello’  # Now a is a string. Python is cool with that.

Variable are not bound to specific type.

Dynamic typing

‘9’ + 5  # TypeError: Can't convert 'int' object to str implicitly

No help from the (C)Python interpreter if you do something illegal with variable types.

// java int a = 5; a = “str”;  // This would cause a compilation error

Types are defined and code will not compile unless type information matches what’s defined in code.

Static typing

Not concerned about the actual underlying type, just has to satisfy the requirements of the calling code.

"When I see a bird that walks like a duck and swims like a duck and quacks like a duck, I call that bird a duck.”

-  JAMES WHITCOMB RILEY

# Knows nothing about the person, object but simply expects # that it will have a method `doGreeting` to call.

def greetPerson(person):   print(person.doGreeting())  

Duck typing

Better Documentation Improve code by including typing information that can be validated with an external type checker.

Find bugs Enable static analysis tools to find bugs before code is run.

Standardisation Standardise the typing syntax for the good of the Python ecosystem.

So after all these years, why add a typing system to Python?

What started as a presentation in from Guido van Rossum in 2000

eventually evolved into two PEP proposals:

PEP 3107 - Syntax

PEP 484 - Type hints

The recent mypy library provided much of the inspiration and push to finally

get a type hinting solution into the Python standard library.

The typing module - 15 years in the making!

I T ’ S G O A L I S T O I M P R O V E C O D E Q U A L I T Y.

• Allows type information (function annotations) to be defined for function parameters and function return values.

• Standardises the syntax for these type annotations.

• Allows for adding type information outside of the actual code itself in “stub” files.

class Person():     def greeting() -> str:         return ‘Hello there!' #… def greetPerson(person: Person) -> None:     print(person.doGreeting());

What does the typing module do?

T H E T Y P I N G M O D U L E H E L P S D E F I N E T Y P E S B U T D O E S N ’ T C H E C K T H E M .

Common Questions About

The Typing Module

Q

A

Q

A

Python 2 compatible?

Not in code. Yes with stubs.

Q

A

Which versions of Python 3 are supported?

Python 3.5 has the typing module but mypy-lang supports

Python 3.2 onwards.

Syntax of type annotations introduced in Python 3.0.

Q

A

Will it slow down, break, crash or do any harm to my code?

No. Not. Ever! It does not affect how code runs.

Q

A

Is Python dictating that I have to use this now?

No, and it never will.

This is not a plot to turn Python into Java.

Q

A

Who uses static analysis tools for Python?

• Python std lib.

• Code review and quality tools (Quantified Code, Codacy).

• IDE’s.

• Google, Dropbox, probably others.

Let’s Code!https://github.com/ryan-blunden/python-typing

Python type hinting calls for embracing “gradual typing"

Apply the type hinting to what you can, when you can. It

doesn’t affect how your code runs.

Tips for getting started• Focus more on adding type hints as documentation. The mypy-lang checker still

has a long way to go.

• If you haven’t already, trial PyCharm and use the type annotations on some of your code to see how it can aid your workflow.

• If you’re working with Python 2 code, try working with stubs.

• If you’re working with Python 3.2-3.4, use mypy.

• Choose one of your favourite open source Python libraries and create a pull request that includes type definitions.

• If you use mypy either for type hinting or checking, install it from source 

Conclusion• Helps find bugs

• Gradual typing approach

• Not required. Use it if you want to.

• Improves code documentation (as it is runnable and verifiable)

• Helps our IDE’s to help us

• Can be used in Python 2 and Python 3

Review Questions• What are some of the benefits that could come from using type hints in Python?

• What versions of Python can you use the type hints in?

• What risk do the type hints pose if you apply them to existing code?

• List one way you could get started in applying type hints to one of your projects today?

?

Wrapping up - Our objectives were...

• Explain why the typing module is useful

• Describe its basic capabilities

• Apply type hinting to a simple function and run a type checker against it

• Start thinking about how to use it in your own projects.

Thanks!

Recommended