grantjenks / blue

The slightly less uncompromising Python code formatter.
https://blue.readthedocs.io/
Other
395 stars 21 forks source link

tests/test_blue.py::test_good_dirs[config_pyproject] fails #66

Closed pgajdos closed 2 years ago

pgajdos commented 2 years ago

I get:

[    8s] /home/abuild/rpmbuild/BUILD/blue-0.8.0/blue/__init__.py:397: in main
[    8s]     black.main()
[    8s] /usr/lib/python3.9/site-packages/click/core.py:1128: in __call__
[    8s]     return self.main(*args, **kwargs)
[    8s] /usr/lib/python3.9/site-packages/click/core.py:1052: in main
[    8s]     with self.make_context(prog_name, args, **extra) as ctx:
[    8s] /usr/lib/python3.9/site-packages/click/core.py:914: in make_context
[    8s]     self.parse_args(ctx, args)
[    8s] /usr/lib/python3.9/site-packages/click/core.py:1370: in parse_args
[    8s]     value, args = param.handle_parse_result(ctx, opts, args)
[    8s] /usr/lib/python3.9/site-packages/click/core.py:2347: in handle_parse_result
[    8s]     value = self.process_value(ctx, value)
[    8s] /usr/lib/python3.9/site-packages/click/core.py:2309: in process_value
[    8s]     value = self.callback(ctx, self, value)
[    8s] /home/abuild/rpmbuild/BUILD/blue-0.8.0/blue/__init__.py:357: in read_configs
[    8s]     result = black.read_pyproject_toml(ctx, param, value)
[    8s] /usr/lib/python3.9/site-packages/black/__init__.py:119: in read_pyproject_toml
[    8s]     config = parse_pyproject_toml(value)
[    8s] /home/abuild/rpmbuild/BUILD/blue-0.8.0/blue/__init__.py:267: in parse_pyproject_toml
[    8s]     pyproject_toml = tomli.load(f)  # type: ignore  # due to deprecated API usage
[    8s] _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
[    8s]
[    8s] __fp = <_io.TextIOWrapper name='/home/abuild/rpmbuild/BUILD/blue-0.8.0/tests/config_pyproject/pyproject.toml' mode='r' encoding='utf8'>
[    8s]
[    8s]     def load(__fp: BinaryIO, *, parse_float: ParseFloat = float) -> dict[str, Any]:
[    8s]         """Parse TOML from a binary file object."""
[    8s]         b = __fp.read()
[    8s]         try:
[    8s]             s = b.decode()
[    8s]         except AttributeError:
[    8s] >           raise TypeError(
[    8s]                 "File must be opened in binary mode, e.g. use `open('foo.toml', 'rb')`"
[    8s]             ) from None
[    8s] E           TypeError: File must be opened in binary mode, e.g. use `open('foo.toml', 'rb')`
[    8s]
[    8s] /usr/lib/python3.9/site-packages/tomli/_parser.py:63: TypeError

Details at: https://build.opensuse.org/package/live_build_log/devel:languages:python/python-blue/openSUSE_Tumbleweed/x86_64

bnavigator commented 2 years ago

openSUSE Tumbleweed unpins black and currently uses 21.12b0. It even ignores that black pins tomli to <2 until black 22.1.0

tomli 2 expects a BinaryIO object.

Fixed by

--- blue-0.8.0.orig/blue/__init__.py
+++ blue-0.8.0/blue/__init__.py
@@ -263,8 +263,8 @@ def parse_pyproject_toml(path_config: st

     If parsing fails, will raise a tomli.TOMLDecodeError
     """
-    with open(path_config, encoding="utf8") as f:
-        pyproject_toml = tomli.load(f)  # type: ignore  # due to deprecated API usage
+    with open(path_config, mode='rb') as f:
+        pyproject_toml = tomli.load(f)
     config = pyproject_toml.get("tool", {}).get("blue", {})
     return {k.replace("--", "").replace("-", "_"): v for k, v in config.items()}
warsaw commented 2 years ago

I believe these issues and the mentioned change will be available in 0.9.0, which I intend to release today. If not, please open a new issue.