rtoml icon indicating copy to clipboard operation
rtoml copied to clipboard

Replace `TextIO` with `IO[…]` in `rtoml.load` signature

Open homeworkprod opened this issue 2 years ago • 0 comments

I've had typing issues with rtoml.load accepting TextIO twice by now. Can't fully recreate the first situation, but the second is loading a TOML config file in Flask.

Flask's flask.Config.from_file has a load parameter whose type is documented as

Callable[[Reader], Mapping] where Reader implements a read method

and implemented as Callable[[IO[Any]], Mapping].

Trying to use rtoml with it like so:

import rtoml
app.config.from_file(config_filename, load=rtoml.load)

makes mypy unhappy:

Argument "load" to "from_file" of "Config" has incompatible type "Callable[[Union[str, Path, TextIO]], Dict[str, Any]]"; expected "Callable[[IO[Any]], Mapping[Any, Any]]"

Since type TextIO is a subclass of IO[str] with a few additional methods that rtoml does not use I suggest to change the signature to use IO[str] (or maybe even IO[Any] plus encoding handling?) instead of TextIO.

For comparison,

What do you think?

homeworkprod avatar Dec 16 '22 11:12 homeworkprod