Stéphan Tulkens

NLP Person

Using overload to handle tagged union return types

Here’s a function with an idiom I’ve seen a lot (probably copied from sentence-transformers):

Read More

Protocols to make untyped code behave

Working with external untyped code in a typed code base can be challenging, you’ll get lots of Any or Unknown, which might propagate through your codebase. This can force you to reach for typing.cast, or # type: ignore statements, which kind of defeats the purpose of using static typing in the first place.

Read More

Rethinking evaluation and relative performance

Here’s a pop quiz: classifier A scores 90% accuracy on some benchmark. Classifier B scores 80%. How much better is A?

Read More

Exposing string types to maximize user happiness

Regular users of my blog will know that I am opposed to what is known as stringly typing: using strings in place of more strongly typed identifiers. As an example, consider a language-specific tokenizer:

Read More

String casing in python

Below are two ways to check if a string is lower-cased in Python.

Read More

Correctly typing cached functions

Caching, or memoization, is a useful way to speed up repeated calls to expensive, pure, functions. When calling a function, we save the output, using the parameters of the function as a key to the cache. Then, instead of re-calculating the result of a function on each call, we simply return the value that was stored in the cache.

Read More

NewType in python

New week, new post! This post is about NewType, an underused construct in Python, in my opinion, and a good way to show the difference between typingtime and runtime.

Read More

TypeVars and Unions in python

This post will be about Unions, TypeVars, and unions of types with a relevant common subtype (i.e., not object). I’ll show how union types are often incorrectly used, and how using a TypeVar can solve some of these problems. So, having said that, let’s dive in!

Read More

Enums with superclasses

In the previous post, I wrote about enumerations, and how they can be really handy when refactoring code. One thing I didn’t touch upon in that post is typed enumerations, which are enumerations that also have a type. As we saw in the previous post, an enumeration member is an association between a name and a value. But this means we need to call .value to get the actual value of an enumeration member. This can lead to overly verbose code. Take a logger for example:

Read More

Enums and refactoring

Enumerations are types that take a set of pre-defined options, called members which are also assigned values. Usually, enumerations members are, as the name implies, simply mapped to a integer values, but any arbitrary value might work. Here’s an example of an enumeration in Python for colors:

Read More