Skip to main content
  1. Dispatches/

TIL - Importing annotations from future for concise Python typing

·110 words·1 min
TIL Python
Daniel Andrlik
Author
Daniel Andrlik lives in the suburbs of Philadelphia. By day he manages product teams. The rest of the time he is a podcast host and producer, writer of speculative fiction, a rabid reader, and a programmer.

Python 3.10 introduced several improvements for type hints, including a much more concise Union operator. This means that instead of using:

def square(number: Union[int, float]) -> Union[int, float]:
    return number ** 2

You can instead write this:

def square(number: int | float) -> int | float:
    return number ** 2

The examples above can be found in the Python 3.10 release notes.

I already knew about these changes, but what I didn’t know is that you can make use of these same improvements in your code as early as Python 3.7 if you do the following.

from __future__ import annotations

Thanks to Will McGugan for pointing this out on Twitter!

Related

TIL - Running make apidocs for Read the Docs
·196 words·1 min
TIL Python
I was hacking on my quote service the other day, and notice that Read the Docs wasn’t building my module level documentation, which meant that anyone that wanted to look at said modules in more detail was getting a 404 error.
TIL - Podcast hosts serve unreliable art mime types
·362 words·2 mins
TIL Podcasting Python Django Mime Types Rss
I’ve been toying with a small Django project that can be given a list of podcast feeds, and then regularly checks those feeds and updates some interesting metrics. Initially, I wanted to see how many comparable podcasts to my own made use of various RSS namespace elements, and also to pull some aggregate statistics around release frequency, episode length, etc…
TIL - Getting asdf Python with tkinter working on a M2 Mac
·312 words·2 mins
TIL Macos Python Homebrew Tkinter
This week I wanted to play around with tkinter a bit. But this proved to be difficult on my M2 Mac. I use asdf to manage various runtime versions, so first I checked to see if it was already working with my existing Python installation using the built in test method.