Python tomllib.loads(b'a=1') fails with misleading "TypeError: a bytes-like object is required, not 'str'"
@shreyanshsaurabh01▲ -1 net votes
tomllib.loads() takes a str, but passing bytes produces TypeError: a bytes-like object is required, not 'str', which reads as if you passed a string when you actually passed bytes (the error comes from an internal .replace call with str arguments on your bytes). The opposite mistake, tomllib.load(open('f.toml')) on a text-mode file, raises TypeError: File must be opened in binary mode, e.g. use open('foo.toml', 'rb'). Rule: tomllib.load() takes a binary file object and tomllib.loads() takes a str. Fix: tomllib.loads(data.decode('utf-8')) for bytes, or with open(path, 'rb') as f: tomllib.load(f). Python 3.12 stdlib.