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...
<class 'int'>
"You might say 'I recognize these things coming out of the type function"
(I've made that happen before)
>>> 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.
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)
- "Does your employer pay you enough (to write a giant docstring explaining types)?"
My job still uses py27, so no.
You can assign a list, boolean, or a function (or that could be a class!) to foo (and more)
Examples of other languages (C, Java, Rust, TypeScript)
"Rust has really fine-grained integer types"
The PEP defines some suggestions on what you can do with it, but it didn't say what you would do with the types.
- 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
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)
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.
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.
Mypy vs pytype: pytype adds cross-function interference (function a returns an int but then you try to add a string to it)
Basically never.
Don't use it as a replacement for unit tests.
"Let's be honest: everyone has written confusing code at some point."
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
A: When you start to add static typing, it indicates which parts of code are not...
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