Problem statement
Python 3.11 introduced native TOML support with the tomllib package. This could work nicely with the flask.Config.from_file() method as an easy way to load TOML config files:
app.config.from_file("config.toml", tomllib.load)However, tomllib.load() takes an object readable in binary mode, while flask.Config.from_file() opens a file in text mode, resulting in this error:
TypeError: File must be opened in binary mode, e.g. use `open('foo.toml', 'rb')`We can get around this with a more verbose expression, like loading from a file opened with the built-in open() function and passing the dict to app.Config.from_mapping():
# We have to repeat the path joining that from_file() does
with open(os.path.join(app.config.root_path, "config.toml"), "rb") as file:
app.config.from_mapping(tomllib.load(file))But adding a file mode parameter to flask.Config.from_file() would enable the use of a simpler expression. E.g.:
app.config.from_file("config.toml", tomllib.load, mode="b")The environment starts at commit
4c288bc97ea3 (pallets/flask 2.3), dependencies installed and tests runnable from the first minute. You are graded by hidden tests taken from the fix that was actually merged upstream.