Accept suggestions from new toolchain · rec/fil@3a7d2f3

@@ -32,11 +32,11 @@

3232

try:

3333

import ujson as json

3434

except ImportError:

35-

import json # type: ignore[no-redef]

35+

import json

36363737

import 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

4242

JSON = t.Union[t.Dict[str, t.Any], t.List, None, bool, float, int, str]

@@ -91,37 +91,37 @@ def write(

919192929393

class _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',)

9696

use_safer = True

97979898

def read(self, p, open=open):

99-

with open(p, "r" + self.mode) as fp:

99+

with open(p, 'r' + self.mode) as fp:

100100

return self._read(fp)

101101102102

def write(self, data, p, *, open=open, use_safer=None, **kwargs):

103103

self._check_data(data)

104-

fp = open(p, "w" + self.mode)

104+

fp = open(p, 'w' + self.mode)

105105106106

if 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)

108108109109

with fp:

110110

self._write(data, fp, **kwargs)

111111112112

@property

113113

def mode(self) -> str:

114-

return ""

114+

return ''

115115116-

def _check_data(self, data):

116+

def _check_data(self, data: t.Any):

117117

pass

118118119119

def _import_error(self):

120120

name = 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

127127

def _module(self):

@@ -146,13 +146,13 @@ def _write(self):

146146147147148148

class _Txt(_Json):

149-

suffixes = (".txt",)

149+

suffixes = ('.txt',)

150150

module_names = ()

151151

use_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)}')

156156157157

def _read(self, p):

158158

return p.read()

@@ -162,21 +162,21 @@ def _write(self, data, fp, **kwargs):

162162163163164164

class _Toml(_Json):

165-

suffixes = (".toml",)

166-

module_names = "tomlkit", "tomllib", "tomli"

165+

suffixes = ('.toml',)

166+

module_names = 'tomlkit', 'tomllib', 'tomli'

167167168168

def _check_data(self, data):

169169

if 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

173173

def mode(self) -> str:

174-

return (self._module == "tomllib") * "b"

174+

return (self._module == 'tomllib') * 'b'

175175176176177177

class _Yaml(_Json):

178-

suffixes = ".yaml", ".yml"

179-

module_names = ("yaml",)

178+

suffixes = '.yaml', '.yml'

179+

module_names = ('yaml',)

180180181181

@cached_property

182182

def _read(self):

@@ -189,7 +189,7 @@ def _write(self):

189189190190

class _JsonLines(_Json):

191191

use_safer = False

192-

suffixes = ".jl", ".jsonl", ".jsonlines"

192+

suffixes = '.jl', '.jsonl', '.jsonlines'

193193194194

def read(self, p, open=open):

195195

with open(p) as fp:

@@ -198,21 +198,21 @@ def read(self, p, open=open):

198198199199

def _write(self, data, fp, **kwargs):

200200

if 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')

205205206206

for d in data:

207207

print(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)):

211211

try:

212-

return iter(d)

212+

return iter(data)

213213

except TypeError:

214214

pass

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')

216216217217218218

CLASSES = _Json(), _JsonLines(), _Toml(), _Txt(), _Yaml()

@@ -223,4 +223,4 @@ def _get_class(p):

223223

try:

224224

return SUFFIX_TO_CLASS[p.suffix]

225225

except KeyError:

226-

raise ValueError("Do not understand file {p}")

226+

raise ValueError('Do not understand file {p}') from None