Accept suggestions from new toolchain · rec/fil@3a7d2f3
@@ -32,11 +32,11 @@
3232try:
3333import ujson as json
3434except ImportError:
35-import json # type: ignore[no-redef]
35+import json
36363737import safer
383839-__all__ = "read", "write", "SUFFIX_TO_CLASS"
39+__all__ = 'read', 'write', 'SUFFIX_TO_CLASS'
40404141# JSON is the type used by JSON, TOML, or Yaml
4242JSON = t.Union[t.Dict[str, t.Any], t.List, None, bool, float, int, str]
@@ -91,37 +91,37 @@ def write(
919192929393class _Json:
94-module_names: t.Sequence[str] = ("json",)
95-suffixes: t.Sequence[str] = (".json",)
94+module_names: t.Sequence[str] = ('json',)
95+suffixes: t.Sequence[str] = ('.json',)
9696use_safer = True
97979898def read(self, p, open=open):
99-with open(p, "r" + self.mode) as fp:
99+with open(p, 'r' + self.mode) as fp:
100100return self._read(fp)
101101102102def write(self, data, p, *, open=open, use_safer=None, **kwargs):
103103self._check_data(data)
104-fp = open(p, "w" + self.mode)
104+fp = open(p, 'w' + self.mode)
105105106106if use_safer or use_safer is None and self.use_safer:
107-fp = safer.open(p, "w" + self.mode)
107+fp = safer.open(p, 'w' + self.mode)
108108109109with fp:
110110self._write(data, fp, **kwargs)
111111112112@property
113113def mode(self) -> str:
114-return ""
114+return ''
115115116-def _check_data(self, data):
116+def _check_data(self, data: t.Any):
117117pass
118118119119def _import_error(self):
120120name = self.module_names[0]
121-name = "pyyaml" if name == "yaml" else name
121+name = 'pyyaml' if name == 'yaml' else name
122122123-sfx = ", ".join(self.suffixes)
124-raise ImportError(f"Install module `{name}` to use {sfx} files") from None
123+sfx = ', '.join(self.suffixes)
124+raise ImportError(f'Install module `{name}` to use {sfx} files') from None
125125126126@cached_property
127127def _module(self):
@@ -146,13 +146,13 @@ def _write(self):
146146147147148148class _Txt(_Json):
149-suffixes = (".txt",)
149+suffixes = ('.txt',)
150150module_names = ()
151151use_safer = False
152152153-def _check_data(self, d):
154-if not isinstance(d, str):
155-raise TypeError(f".txt files only accept strings, not {type(d)}")
153+def _check_data(self, data: t.Any):
154+if not isinstance(data, str):
155+raise TypeError(f'.txt files only accept strings, not {type(data)}')
156156157157def _read(self, p):
158158return p.read()
@@ -162,21 +162,21 @@ def _write(self, data, fp, **kwargs):
162162163163164164class _Toml(_Json):
165-suffixes = (".toml",)
166-module_names = "tomlkit", "tomllib", "tomli"
165+suffixes = ('.toml',)
166+module_names = 'tomlkit', 'tomllib', 'tomli'
167167168168def _check_data(self, data):
169169if not isinstance(data, dict):
170-raise TypeError(f"TOML files only accept dicts, not {type(data)}")
170+raise TypeError(f'TOML files only accept dicts, not {type(data)}')
171171172172@property
173173def mode(self) -> str:
174-return (self._module == "tomllib") * "b"
174+return (self._module == 'tomllib') * 'b'
175175176176177177class _Yaml(_Json):
178-suffixes = ".yaml", ".yml"
179-module_names = ("yaml",)
178+suffixes = '.yaml', '.yml'
179+module_names = ('yaml',)
180180181181@cached_property
182182def _read(self):
@@ -189,7 +189,7 @@ def _write(self):
189189190190class _JsonLines(_Json):
191191use_safer = False
192-suffixes = ".jl", ".jsonl", ".jsonlines"
192+suffixes = '.jl', '.jsonl', '.jsonlines'
193193194194def read(self, p, open=open):
195195with open(p) as fp:
@@ -198,21 +198,21 @@ def read(self, p, open=open):
198198199199def _write(self, data, fp, **kwargs):
200200if isinstance(data, t.Mapping):
201-raise ValueError("JsonLines cannot write a single dict")
201+raise ValueError('JsonLines cannot write a single dict')
202202203-if kwargs.get("indent") is not None:
204-raise ValueError("indent= not allowed for JSON Lines")
203+if kwargs.get('indent') is not None:
204+raise ValueError('indent= not allowed for JSON Lines')
205205206206for d in data:
207207print(json.dumps(d), file=fp)
208208209-def _check_data(self, d):
210-if not isinstance(d, (dict, str)):
209+def _check_data(self, data: t.Any):
210+if not isinstance(data, (dict, str)):
211211try:
212-return iter(d)
212+return iter(data)
213213except TypeError:
214214pass
215-raise TypeError("JSON Line data must be iterable and not dict or str")
215+raise TypeError('JSON Line data must be iterable and not dict or str')
216216217217218218CLASSES = _Json(), _JsonLines(), _Toml(), _Txt(), _Yaml()
@@ -223,4 +223,4 @@ def _get_class(p):
223223try:
224224return SUFFIX_TO_CLASS[p.suffix]
225225except KeyError:
226-raise ValueError("Do not understand file {p}")
226+raise ValueError('Do not understand file {p}') from None