31 Posts Tagged ‘mypy’

(All tags.)

Python Type Hints: How to Gradually Add Types for Third Party Packages

Gradually evolved cephalopods.

Hynek Schlawack recently described graduality as Python’s super power: the ability to prototype in the REPL, and gradually add linting, type checking, and other practices to refine your code into maintainable, production-ready software. You can also apply graduality within tools, activating checks one at a time and fixing the resulting errors as you go.


Python Type Hints - How to Split Types by Python Version

Tweezers, check. sys.version_info, check.

The typing module continues to evolve, with new features in every Python version. This can make it tricky if you’re trying to type code that supports multiple Python versions. To help write such code, Mypy identifies version checks using sys.version_info and reads the appropriate branch.


Python Type Hints - How to Handle Optional Imports

Import, or import not, there is no try.

This post is not about importing typing.Optional, but instead imports that are themselves optional. Libraries often have optional dependencies, and the code should work whether or not the import is there. A common pattern to solve this to catch ImportError and replace the module with None:


Python Type Hints - How to Type a Descriptor

Now invoking The Scroll of Descriptors.

The descriptor protocol allow us to completely customize attribute access. Python’s documentation describes the protocol with types involved described with words. Let’s look at how we can write those as type hints.


A Python Script Template with Sub-commands (and Type Hints)

Now there are two of them!

Earlier this week I shared my Python script template. Here’s an extended version with sub-command support, and an example script.


A Python Script Template, with and without Type Hints and Async

Read on for a “cat trick”!

Python is great for writing scripts for the command line. In this post we’ll look at my script template, an example script, and some alternative versions (without type hints, and in async flavour).


Python Type Hints - Use Cases for the types Module

Some kind of tool for working with types.

Writing type hints gives us some familiarity with the typing module. But Python also includes the similarly-named types module, which can also come in handy. Let’s look at the history of these two modules, some use cases of types, and one way in which it’s not so useful.


Python Type Hints - How to Work with Regular Expressions

Searching again for a needle in a haystack.

Python’s re module lets us search both str and bytes strings with regular expressions (regexes). Our type checker can ensure we call re functions with the correct types, thanks to some parametrized classes.


Python Type Hints - How to Vary Return Type Based on an Argument

So many ropes, so many overloads.

Here’s a recipe that combines typing.Literal with @overload to define a function that switches its return type based on the value of an argument.


Python Type Hints - How to Avoid “The Boolean Trap”

Well that looks like a finger trap.

“The Boolean Trap” is a programming anti-pattern where a boolean argument switches behaviour, leading to confusion. In this post we’ll look at the trap in more detail, and several ways to avoid it in Python, with added safety from type hints.