Recent Posts (Page 2)

Python Type Hints - How to Use @overload

Precisely clamped type relationships

Sometimes the types of several variables are related, such as “if x is type A, y is type B, else y is type C”. Basic type hints cannot describe such relationships, making type checking cumbersome or inaccurate. We can instead use @typing.overload to represent type relationships properly.


Python Type Hints - How to Manage "type: ignore" Comments with Mypy

Clamp down on them comments!

It seems inevitable that large projects need some # type: ignore comments, to work around type checking in tricky cases. I’ve found Mypy has a couple of opt-in flags to make such ignore comments more precise and manageable.


Python Type Hints - Mypy doesn’t allow variables to change type

Iron clad types.

Mypy does not allow variables to change type. I found this a little bit of a shock at first, but after I adapted, I found my code was more readable.


Python Type Hints - How to Upgrade Syntax with pyupgrade

Ratcheting up the Python versions.

A couple of recent PEP’s have made writing type hints easier:


Python Type Hints - What’s the point of NoReturn?

Hey, who did a no return of half my letters!?

Sometimes functions never return, for example by always raising an exception. For such functions’ return types, we can “get away” with using None, but it’s best to use the special NoReturn type). This allows Mypy to better find unreachable code (as covered previously). It also shows future readers that the lack of return is intentional.


Python Type Hints - How to use Mypy’s unreachable code detection

My code before removing the unreachable parts (...and after)

I recently discovered Mypy has a secondary function as an unreachable code detector. This feature is a great way to highlight places bugs may be hiding, as code paths that can’t possibly run normally show a logical error.


Python Type Hints - Duck typing with Protocol

Quack to the future.

Duck typing says “if it quacks like a duck, treat it like a duck.” Type checking seems at odds with duck typing: we restrict variables to named types (classes), allowing only that type or subtypes. This seems like it would stop us from passing in arbitrary objects that can “quack the right way”. But since PEP 0544, we can declare “duckish” types with typing.Protocol.


Python Type Hints - How to Narrow Types with isinstance(), assert, and Literal

Narrow typographer’s tweezers.

Type narrowing is the ability for a type checker to infer that inside some branch, a variable has a more specific (narrower) type than its definition. This allows us to perform type-specific operations without any explicit casting.


Python Type Hints - How to Specify a Class Rather Than an Instance Thereof

Cats & Dogs 4: The Battle of the Types

In a type hint, if we specify a type (class), then we mark the variable as containing an instance of that type. To specify that a variable instead contains a type, we need to use type[Cls] (or the old syntax typing.Type).


Python Type Hints - How to Enable Postponed Evaluation With __future__.annotations

from future import ant-onations

Python 3.7 added a new “postponed evaluation” mode for type hints. This is opt-in for now, and may become the default in Python 3.11. In this post we’ll cover how it changes the behaviour of type hints, how to activate it, and the outstanding problems.