Skip to content

Commit fb18a3b

Browse files
committed
explain more transformer options
1 parent 9e3ebb9 commit fb18a3b

1 file changed

Lines changed: 64 additions & 26 deletions

File tree

README.MD

Lines changed: 64 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -138,80 +138,80 @@ in a pyproject.toml `[tool.strip-python3]` or setup.cfg `[strip-python3]` sectio
138138
The names are the same as the commandline options, just without a leading "--".
139139
Use "--show" and "--show -v" for details of selected settings.
140140

141-
## define-print-function
141+
## --define-print-function
142142

143143
If `print()` is used then `from __future__ import print_function` is added
144144
if python2 compatibility is requested. (see [doc](doc/define-print-function.md))
145145

146-
## define-float-division
146+
## --define-float-division
147147

148148
If `/` is used then `from __future__ import division` is added
149149
if python2 compatibility is requested. (see [doc](doc/define-float-division.md))
150150

151-
## define-absolute-import
151+
## --define-absolute-import
152152

153153
If `.localtypes` is used then `from __future__ import absolute_import` is added
154154
if python2 compatibility is requested. (see [doc](doc/define-absolute-import.md))
155155

156-
## define-basestring
156+
## --define-basestring
157157

158158
If `isinstance(x, str)` is used then each is replaced by `isinstance(x, basestring)`
159159
if python2 compatibility is requested. It does also import `basestring = str` for
160160
python3 versions. (see [doc](doc/define-basestring.md))
161161

162-
## define-range
162+
## --define-range
163163

164164
If `range(x)` is used then `range = xrange` is defined for python2 versions
165165
if python2 compatibility is required. (see [doc](doc/define-range.md))
166166

167-
## define-callable
167+
## --define-callable
168168

169169
If `callable(x)` is used then a `def callable` boilerplate is added for python3
170170
versions where it was not predefined, which only in 3.0 and 3.1 (see [doc](doc/define-callable.md))
171171

172-
## datetime-fromisoformat
172+
## --datetime-fromisoformat
173173

174174
If `datetime.datetime.fromisoformat` is used then it is replaced by `datetime_fromisodate`
175175
with boilerplate code if python2 or python3 older than 3.7. (see [doc](doc/datetime-fromisoformat.md))
176176

177-
## subprocess-run
177+
## --subprocess-run
178178

179179
If `subprocess.run` is used then it is replaced by `subprocess_run`
180180
with boilerplate code if python2 or python3 older than 3.5. (see [doc](doc/subprocess-run.md))
181181

182-
## time-monotonic
182+
## --time-monotonic
183183

184184
If `time.monotonic` is used then it is replaced by `time_monotonic`
185185
with boilerplate code based on `time.time`. This is needed for
186186
python2 or python3 older than 3.3. (see [doc](doc/time-monotonic.md))
187187

188-
## import-pathlib2
188+
## --import-pathlib2
189189

190190
If `import pathlib` is used then it is replaced by `import pathlib2`
191191
if python2 compatiblity is requested. Pathlib exists since python 3.3
192192
but there is a backport available (to be installed seperately)
193193

194-
## import-backports-zoneinfo
194+
## --import-backports-zoneinfo
195195

196196
If `import zoneinfo` is used then it is replaced by `from backports import zoneinfo`
197197
if python3 compatiblity is requested before python 3.9 (backport requires atleast 3.6)
198198

199-
## import-toml
199+
## --import-toml
200200

201201
If `import tomllib` is used then it is replaced by `import toml`
202202
if compatiblity with python older than 3.11 is requested. The
203203
external toml package (to be installed seperately) did exist
204204
before and was integrated into the standard lib.
205205

206-
## catch-ioerror and catch-select-error
206+
## --catch-ioerror and --catch-select-error
207207

208208
if `except OSError` is used then it gets extended to `except (OSError, IOError)`.
209209
if python3 compatibility is requested before python 3.3 (having PEP3151)
210210

211211
On `import select` it gets extended to `except (OSError, select.error)`.
212212
So for python 3.3 that results in `except (OSError, IOError, select.error)`.
213213

214-
## replace-fstring
214+
## --replace-fstring
215215

216216
If `F"string {part}"` is used then it is replaced by `"string {}".format(part)`
217217
if python2 compatibility requested, as well as python3 older than 3.5.
@@ -223,15 +223,15 @@ you can write `F"string {len(part):4n}: {part}"` which gets expanded to:
223223

224224
See [doc for details](doc/replace-fstring.md) and `--fstring-from-var-locals`.
225225

226-
## replace-namedtuple-class
226+
## --replace-namedtuple-class
227227

228228
If `class Result(NamedTuple)` is used then it is replaced by `Result = namedtuple("Result",[])`
229229
if python2 compatibility requested, as well as python3 older than 3.6. (see [doc](doc/replace-namedtuple-class.md))
230230

231231
They pyi typehints file retains the NamedTuple definition. An exception is thrown if
232232
the NamedTuple class contains non-variable definitions (like method functions).
233233

234-
## replace-walrus-operator
234+
## --replace-walrus-operator
235235

236236
If `if x := fu(): pass` is used then it is replaced by `x = fu()` followed
237237
by `if x: pass` if python2 compatibility requested, as well as python3 older than 3.8.
@@ -248,50 +248,50 @@ When using `while (x:= fu()) != "end": print(x)` it gets expanded to
248248
Currently only if-walrus and while-walrus are supported. Only a direct
249249
assignment or compare/binop is supported. See [doc for details](doc/replace-walrus-operator.md).
250250

251-
## replace-annotated-typing
251+
## --replace-annotated-typing
252252

253253
If `var: Annotated[int, Field(gt=0)]` is used then it is replaced by `var: int`
254254
if python2 compatibility requested, as well as python3 older than 3.9.
255255
(see [doc for details](doc/replace-annotated-typing.md))
256256

257-
## replace-builtin-typing
257+
## --replace-builtin-typing
258258

259259
If `var: list[int]` is used then it is replaced by `var: List[int]`
260260
if python2 compatibility requested, as well as python3 older than 3.9.
261261
(see [doc for details](doc/replace-builtin-typing.md))
262262

263-
## replace-union-typing
263+
## --replace-union-typing
264264

265265
If `var: int|str` is used then it is replaced by `var: Union[int, str]`
266266
if python2 compatibility requested, as well as python3 older than 3.10.
267267
It does also replace `var: int|None` by `var: Optional[int]`
268268
(see [doc for details](doc/replace-builtin-typing.md))
269269

270-
## replace-self-typing
270+
## --replace-self-typing
271271

272272
If `param: Self` is used then it is replaced by `param: SelfB`
273273
if python2 compatibility requested, as well as python3 older than 3.11.
274274
It does declare a `SelfB = TypeVar("SelfB", bound="B")` as suggested
275275
in PEP 673, and it works for the return type of a class method as well.
276276
(see [doc for details](doc/replace-self-typing.md))
277277

278-
## remove-keywordsonly
278+
## --remove-keywordsonly
279279

280280
The keywordsonly syntax became available in python 3.0, so it need to be
281281
removed for python2 (see [doc](doc/remove-positionalonly.md))
282282

283283

284-
## remove-positionalonly
284+
## --remove-positionalonly
285285

286286
The postionalonly syntax became available in python 3.8, so it need to be
287287
removed for python2 and python3 older than 3.8 (see [doc](doc/remove-positionalonly.md))
288288

289-
## remove-var-typehints
289+
## --remove-var-typehints
290290

291291
The var typehints became available in python 3.6, so they need to be
292292
removed for older python3 (or python2)
293293

294-
## remove-typehints
294+
## --remove-typehints
295295

296296
The function annotations became available in python 3.0, so they need to
297297
be removed for python2. Note that the typehints syntax became available
@@ -306,12 +306,12 @@ The `"*.pyi"` file generation needs to be requested with the "--pyi" commandline
306306
The transformations are automatically selected by the minimum target python version
307307
that is given by --pyi-version=3.8 (which is the default).
308308

309-
## remove-positional-pyi
309+
## --remove-positional-pyi
310310

311311
The postionalonly syntax became available in python 3.8, so it need to be
312312
removed for python3 compatiblity older than 3.8.
313313

314-
## remove-typeddict-pyi
314+
## --remove-typeddict-pyi
315315

316316
The TypedDict definition became available in python 3.8, so it need to be
317317
removed for python3 compatiblity older than 3.8.
@@ -344,6 +344,44 @@ after transformations.
344344

345345
If no `*.pyi` file is wanted then disable it with "--no-pyi" again.
346346

347+
# Parser Variants
348+
349+
By default, ast_comments is used to parse/unparse the source tree. This is
350+
actually using the stdlib ast.parse adding comments by line number after
351+
the source code tree has been loaded, and it adds a visitComment to the
352+
stdlib.unparse that was introduced in Python 3.9. In some cases this
353+
scheme generates bad code - specifically, when block heads and expr
354+
statements are on the same line (`if true: return 1`).
355+
356+
## --no-comments
357+
358+
Disables the comment node handling for both parse and unparse. The python
359+
core maintainers ensure the output to be correct. The only comment being
360+
kept (or actually added) is the shebang in the first line of the python
361+
file.
362+
363+
## --remove-comments
364+
365+
Disables loading the comments but still uses the new unparser.
366+
367+
# Upgrade transfomers
368+
369+
Most transformers look for newer python features to be replaced
370+
by older features being the same or similar enough. The transformer
371+
classes are able to upgrade source code to newer python features
372+
as well.
373+
374+
375+
## --fstring-from-locals
376+
377+
Replace `"{x} {y}".format(**locals())` to be `F"{x} {y}"`.
378+
379+
## --fstring-from-var-locals
380+
381+
Adding on `--fstring-from-locals` the format may be not
382+
a literal string. `x='{name}'; x.format(**locals())`
383+
gets replaced here.
384+
347385
# Development
348386

349387
[DEVGUIDE.MD](DEVGUIDE.MD) for more infos.

0 commit comments

Comments
 (0)