36 Posts Tagged ‘mypy’

(All tags.)

Python Type Hints - How to use Mypy’s always-true boolean check detection

Redundant-looking science apparatus

Sometimes code uses boolean checks on variables that can only be true. This is normally a sign of a mistake, either in the type hints or the implementation. Mypy has an optional check that can find such problematic boolean usage with its truthy-bool error code.


Python Type Hints - Make Mypy disallow implicit optional types

Optional? ¯_(ツ)_/¯

The original type hint proposal, PEP 484, initially allowed implicit optional types in function signatures. That is, a parameter with a default value of None would have its type automatically interpreted as optional. For example, this signature:


Python Type Hints - Old and new ways to write the same types

This is how evolution works, right?

As type hints have evolved, Python has added simpler, more succinct syntaxes. But you still need to know the older forms, since Mypy uses them when reporting types.


Python Type Hints - How to Do Exhaustiveness Checking

Well, that looks exhuasting.

Exhaustiveness checking is a very handy type checker feature. It ensures that all possible types of a variable are handled. If your code changes to add another possible type, you can guarantee that exhaustiveness-checked code paths handle the new case.


Python Type Hints - Lambdas don’t support type hints, but that’s okay

A tangle of lambdas (generated by Stable Diffusion)

Python has no syntax to add type hints to lambdas, but that doesn’t mean you can’t use them in type-checked code. In this post we’ll look at how Mypy can infer the types for lambdas, based on where they’re used.


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.