際際滷

際際滷Share a Scribd company logo
How we use it and what we love about it
Type checking in Python
Making
culture
more
accessible
Why do we use Python type checking?
Better
Developer Experience
No guessing & no surprises
Less cognitive load
Type checking at commit &
build time
Realize mistakes early
Type checks are easier to
write & maintain
Avoid trivial unit tests
Type checking at runtime
with Pydantic
Data validation
Type Checking in Python at Tiqets
What types can I use?
Collections
Tuple, Dict, MutableMapping,
List, NamedTuple...
Composers
Union, Optional
Callable
Generics
TypeVar, Generic, Protocol
...and all simple types
int, str, bytes...
Checks: Green
No more
typing issues
Add typing
to new code
Add typing
on existing code
How did we introduce type checks?
Make the changes needed
to have mypy run
without errors.
Add a mypy step to your
CI build and
pre-commit hooks.
From now onwards, no new
typing problems can be
introduced.
Things can only get better!
Teach your team
about typing and mypy.
Agree on adding type hints
to all new code.
Gradually add type hints
to all existing code.
If you touch it,
you add types.
Pitfalls
False sense of security
It wont catch all issues.
No runtime checks.
Type checking: an extra safety step,
not a replacement.
No optimizations
Python wont use the knowledge
about types to make any
optimizations on your code.
Untyped libraries will lead to typing
mistakes.
Type hints can get ugly
Tip on that is: if your type is ugly, is
there a better approach?
Beyond
the
basics
Type Checking in Python at Tiqets
Type Checking in Python at Tiqets
Type Checking in Python at Tiqets
Type Checking in Python at Tiqets
Type Checking in Python at Tiqets
A 鍖nal class cant be inherited from.
A 鍖nal method cant be overloaded.
A 鍖nal variable cant be reassigned.
Trickier types
Type Checking in Python at Tiqets
Type Checking in Python at Tiqets
Type Checking in Python at Tiqets
Type Checking in Python at Tiqets
Type Checking in Python at Tiqets
Type Checking in Python at Tiqets
Type Checking in Python at Tiqets
Type checkers
Reference implementation.
We use it at Tiqets
mypy
Faster; runs on nodejs.
pyright (Microsoft)
Can infer types from
unannotated code.
pytype (Google)
Can do incremental checking
pyre (Facebook)
Whats
coming up?
Uni鍖ed error codes
Easier integration of type checkers
with other tools
Less CamelCase
list[dict] instead of List[Dict]
Shorter syntax
int | float instead of Union[int, float]
?str instead of Optional[str]
Check out our blog at tiqets.engineering
Thanks!
Type Checking in Python at Tiqets

More Related Content

Type Checking in Python at Tiqets

  • 1. How we use it and what we love about it Type checking in Python
  • 3. Why do we use Python type checking?
  • 4. Better Developer Experience No guessing & no surprises Less cognitive load Type checking at commit & build time Realize mistakes early Type checks are easier to write & maintain Avoid trivial unit tests Type checking at runtime with Pydantic Data validation
  • 6. What types can I use? Collections Tuple, Dict, MutableMapping, List, NamedTuple... Composers Union, Optional Callable Generics TypeVar, Generic, Protocol ...and all simple types int, str, bytes...
  • 7. Checks: Green No more typing issues Add typing to new code Add typing on existing code How did we introduce type checks? Make the changes needed to have mypy run without errors. Add a mypy step to your CI build and pre-commit hooks. From now onwards, no new typing problems can be introduced. Things can only get better! Teach your team about typing and mypy. Agree on adding type hints to all new code. Gradually add type hints to all existing code. If you touch it, you add types.
  • 8. Pitfalls False sense of security It wont catch all issues. No runtime checks. Type checking: an extra safety step, not a replacement. No optimizations Python wont use the knowledge about types to make any optimizations on your code. Untyped libraries will lead to typing mistakes. Type hints can get ugly Tip on that is: if your type is ugly, is there a better approach?
  • 15. A 鍖nal class cant be inherited from. A 鍖nal method cant be overloaded. A 鍖nal variable cant be reassigned.
  • 24. Type checkers Reference implementation. We use it at Tiqets mypy Faster; runs on nodejs. pyright (Microsoft) Can infer types from unannotated code. pytype (Google) Can do incremental checking pyre (Facebook)
  • 25. Whats coming up? Uni鍖ed error codes Easier integration of type checkers with other tools Less CamelCase list[dict] instead of List[Dict] Shorter syntax int | float instead of Union[int, float] ?str instead of Optional[str]
  • 26. Check out our blog at tiqets.engineering Thanks!