Profile picture
, 37 tweets, 24 min read Read on Twitter
Next: @di_codes talking about static typing in Python. @djangocon
@di_codes @djangocon He's a dev advocate at Google and the chair of @pytexas
2020.

"Quick pop quiz for everyone in the room: is Python dynamically or statically typed? ... Who thinks this is a trick question? The answer is that Python is dynamically typed but can optionally be as statically typed...
@di_codes @djangocon @pytexas ...as you want it to be."
@di_codes @djangocon @pytexas >>> type(42)
<class 'int'>

"You might say 'I recognize these things coming out of the type function"
@di_codes @djangocon @pytexas "Who's seen a really ugly list like this where a string is getting turned into a list?"

(I've made that happen before)
@di_codes @djangocon @pytexas There are also NoneType, function, and ellipsis types. There are lots of types.

>>> import types
>>> dir(types)

This returns a massive list. They can be used to instantiate new objects. We don't do this because it looks really ugly.
@di_codes @djangocon @pytexas Dynamic typing means that args and return values of functions can be any type.

def frobnicate(a, b, c):
return a + b +c

You can call that func on ints, floats, or strings, but you can't mix ints + strings (TypeError)
@di_codes @djangocon @pytexas How can we enforce the types we want?

- "Does your employer pay you enough (to write a giant docstring explaining types)?"

My job still uses py27, so no.
@di_codes @djangocon @pytexas Or you could run assertions for each arg before you run your function. It's a small performance penalty and just ugly.
@di_codes @djangocon @pytexas Python uses duck typing. If it walks like a duck...

You can assign a list, boolean, or a function (or that could be a class!) to foo (and more)
@di_codes @djangocon @pytexas Enter static typing.

Examples of other languages (C, Java, Rust, TypeScript)

"Rust has really fine-grained integer types"
@di_codes @djangocon @pytexas You can also add Ruby, Clojure, and JS to dynamically typed languages, but Ruby is getting this sort of optional static typing next year.
@di_codes @djangocon @pytexas Enter PEP 3107: function annotations. Submitted in 2007, py3 only.
@di_codes @djangocon @pytexas Annotations are stored in the __annotations__ dunder attribute on the function.

The PEP defines some suggestions on what you can do with it, but it didn't say what you would do with the types.
@di_codes @djangocon @pytexas "Just because I can annotate a function doesn't mean I can evaluate whether the function is being used correctly elsewhere."
@di_codes @djangocon @pytexas Jukka Lehtosalo was working on his PhD on unifying static and dynamic types by slowly adding type annotations. He published his Cambridge thesis in 2011. You can use an optional pluggable type system to do this without changing the language. He introduced mypy at PyCon US 2013.
@di_codes @djangocon @pytexas This is not what mypy is now. At the time, it was an experimental Python variant.
@di_codes @djangocon @pytexas He chatted with Guido van Rossum at PyCon, where GvR convinced him to drop the custom syntax and roll this into Python 3. Thus in 2014 comes PEP 483: the theory of type hinting.
@di_codes @djangocon @pytexas - optional typing, which only gets in your way if you _want_ it to get in your way

- Gradual typing, so let's not do it all at once

- Variable annotations (for more than just functions)

This also meant type hinting in py2
@di_codes @djangocon @pytexas Use # type: foo style of contents in py2 for type annotations
@di_codes @djangocon @pytexas Also special type constructs: any, union, optional, tuple, callable
@di_codes @djangocon @pytexas Container types: List, Set, Dict (so you can define what's inside them)

Generic types: Iterable

Also relationships between types, subtypes, and classes.

Typing types aren't directly instantiable.

You can also define aliases and use them (so if you want a JS-style number)
@di_codes @djangocon @pytexas PEP 484: Type Hints

Standardizes everything in what PEP 483 did. Released in Python 3.5

PEP 526: syntax for variable annotations in py36
That had almost everything we needed, except for the type checker.
@di_codes @djangocon @pytexas There are two types of checkers: static (like MyPy) and dynamic (at runtime)

Mostly going to talk about static checkers.
Dropbox, Google, Facebook, Microsoft, and PyCharm each have their own.

Lots of dynamic ones, which intro a runtime overhead.
@di_codes @djangocon @pytexas What's the difference between them?

Mypy vs pytype: pytype adds cross-function interference (function a returns an int but then you try to add a string to it)
@di_codes @djangocon @pytexas Mypy will complain if you append a list to a string, but pytype will let it through.
@di_codes @djangocon @pytexas When shouldn't you use static typing?

Basically never.

Don't use it as a replacement for unit tests.
@di_codes @djangocon @pytexas Also use static typing when your code is confusing.

"Let's be honest: everyone has written confusing code at some point."
@di_codes @djangocon @pytexas Use static typing when your code is for public consumption. It helps devs know how to use your APIs (and their IDEs too)
@di_codes @djangocon @pytexas Use it before migrating or refactoring too. That way you can ensure you're not breaking type compatibility.
@di_codes @djangocon @pytexas Also use it to experiment with static typing.
@di_codes @djangocon @pytexas Wrap up! 5 steps to use it:

1. Move to py36+ (optional)
2. Install a type checker locally (and integrate w/ your IDE)
3. Start optionally checking your code base
4. Run a type checker with your linting run (you are doing that as part of your CI, right?)
5. Get coworkers to join
@di_codes @djangocon @pytexas Q from Ryan Sullivan: I accidentally found static typing yesterday but discovered I ran into a circular import error to get the types I need to statically type my code. Any thoughts on that mess?

A: When you start to add static typing, it indicates which parts of code are not...
...very well written. It probably means the args are ambiguous. You can also use strings ('MyType') and mypy will figure that out.
Last Q: How do you feel about Django (and mypy-django). Do you think it should be in the Django code base?

A: I can't speak much for Django. I think there are some ideas about it. They also ship typeship for the stdlib as well. Also django-type-annotations for the Django code
...base. From what I've heard, there's some magic internally to Django that makes things difficult. From what I understand, it's very difficult to understand to some parts of your Django app.
And that's it. Time for lightning talks.
Missing some Tweet in this thread?
You can try to force a refresh.

Like this thread? Get email updates or save it to PDF!

Subscribe to Drew
Profile picture

Get real-time email alerts when new unrolls are available from this author!

This content may be removed anytime!

Twitter may remove this content at anytime, convert it as a PDF, save and print for later use!

Try unrolling a thread yourself!

how to unroll video

1) Follow Thread Reader App on Twitter so you can easily mention us!

2) Go to a Twitter thread (series of Tweets by the same owner) and mention us with a keyword "unroll" @threadreaderapp unroll

You can practice here first or read more on our help page!

Follow Us on Twitter!

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just three indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3.00/month or $30.00/year) and get exclusive features!

Become Premium

Too expensive? Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal Become our Patreon

Thank you for your support!