Type hint for `open(..., encoding=...)`?

Functions like open() and Path.write_text have the encoding= arg which accepts a codec in the form of a string. I would like to type hint these for auto completion. I checked typeshed hoping to find something since typeshed does have type aliases for open modes but I couldn’t find one for encoding.

I’m hoping there’s a better way than to manually type out all of these https://docs.python.org/3/library/codecs.html#standard-encodings

encoding can’t be safely (i.e. without false negatives) type hinted to anything but str since it’s possible to register custom encodings with whatever names you want. Also, name normalization means that a pretty huge number of strings map to the same encoding.


Oh, that explains why typeshed doesn’t. Thank you.