Skip to content

Commit

Permalink
Merge pull request #718 from asottile/py3-plus
Browse files Browse the repository at this point in the history
remove python-2-compatible rewriting mode
  • Loading branch information
asottile authored Oct 4, 2022
2 parents 6dbfc2a + 57eb8ab commit 091f30c
Show file tree
Hide file tree
Showing 62 changed files with 219 additions and 725 deletions.
104 changes: 5 additions & 99 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ Sample `.pre-commit-config.yaml`:
```


### Python2.7+ Format Specifiers
### Format Specifiers

```diff
-'{0} {1}'.format(1, 2)
Expand All @@ -83,10 +83,6 @@ Availability:

### Unicode literals

Availability:
- File imports `from __future__ import unicode_literals`
- `--py3-plus` is passed on the commandline.

```diff
-u'foo'
+'foo'
Expand All @@ -105,9 +101,8 @@ Availability:
# strings with mixed valid / invalid sequences get escaped
-'\n\d'
+'\n\\d'
# `ur` is not a valid string prefix in python3
-u'\d'
+u'\\d'
+r'\d'
# this fixes a syntax error in python3.3+
-'\N'
+r'\N'
Expand All @@ -131,22 +126,6 @@ of those comparisons is implementation specific (due to common object caching).
+x == 'foo'
```

### `ur` string literals

`ur'...'` literals are not valid in python 3.x

```diff
-ur'foo'
+u'foo'
-ur'\s'
+u'\\s'
# unicode escapes are left alone
-ur'\u2603'
+u'\u2603'
-ur'\U0001f643'
+u'\U0001f643'
```

### `.encode()` to bytes literals

```diff
Expand All @@ -162,26 +141,6 @@ of those comparisons is implementation specific (due to common object caching).
+b'\xa0'
```

### Long literals

```diff
-5L
+5
-5l
+5
-123456789123456789123456789L
+123456789123456789123456789
```

### Octal literals

```diff
-0755
+0o755
-05
+5
```

### extraneous parens in `print(...)`

A fix for [python-modernize/python-modernize#178]
Expand All @@ -204,9 +163,6 @@ A fix for [python-modernize/python-modernize#178]

Rewrites [deprecated unittest method aliases](https://docs.python.org/3/library/unittest.html#deprecated-aliases) to their non-deprecated forms.

Availability:
- More deprecated aliases are rewritten with `--py3-plus`

```diff
from unittest import TestCase

Expand All @@ -221,9 +177,6 @@ Availability:

### `super()` calls

Availability:
- `--py3-plus` is passed on the commandline.

```diff
class C(Base):
def f(self):
Expand All @@ -233,9 +186,6 @@ Availability:

### "new style" classes

Availability:
- `--py3-plus` is passed on the commandline.

#### rewrites class declaration

```diff
Expand All @@ -254,9 +204,6 @@ Availability:

### forced `str("native")` literals

Availability:
- `--py3-plus` is passed on the commandline.

```diff
-str()
+''
Expand All @@ -266,19 +213,13 @@ Availability:

### `.encode("utf-8")`

Availability:
- `--py3-plus` is passed on the commandline.

```diff
-"foo".encode("utf-8")
+"foo".encode()
```

### `# coding: ...` comment

Availability:
- `--py3-plus` is passed on the commandline.

as of [PEP 3120], the default encoding for python source is UTF-8

```diff
Expand All @@ -291,9 +232,8 @@ as of [PEP 3120], the default encoding for python source is UTF-8
### `__future__` import removal

Availability:
- by default removes `nested_scopes`, `generators`, `with_statement`
- `--py3-plus` will also remove `absolute_import` / `division` /
`print_function` / `unicode_literals`
- by default removes `nested_scopes`, `generators`, `with_statement`,
`absolute_import`, `division`, `print_function`, `unicode_literals`
- `--py37-plus` will also remove `generator_stop`

```diff
Expand All @@ -302,9 +242,6 @@ Availability:

### Remove unnecessary py3-compat imports

Availability:
- `--py3-plus` is passed on the commandline.

```diff
-from io import open
-from six.moves import map
Expand All @@ -314,7 +251,7 @@ Availability:
### import replacements

Availability:
- `--py3-plus` (and others) will replace imports
- `--py36-plus` (and others) will replace imports

see also [reorder-python-imports](https://github.com/asottile/reorder_python_imports#removing--rewriting-obsolete-six-imports)

Expand All @@ -339,7 +276,6 @@ some examples:
### rewrite `mock` imports

Availability:
- `--py3-plus` is passed on the commandline.
- [Unless `--keep-mock` is passed on the commandline](https://github.com/asottile/pyupgrade/issues/314).

```diff
Expand All @@ -349,9 +285,6 @@ Availability:

### `yield` => `yield from`

Availability:
- `--py3-plus` is passed on the commandline.

```diff
def f():
- for x in y:
Expand All @@ -364,9 +297,6 @@ Availability:

### Python2 and old Python3.x blocks

Availability:
- `--py3-plus` is passed on the commandline.

```diff
import sys
-if sys.version_info < (3,): # also understands `six.PY2` (and `not`), `six.PY3` (and `not`)
Expand Down Expand Up @@ -408,9 +338,6 @@ Note that `if` blocks without an `else` will not be rewritten as it could introd

### remove `six` compatibility code

Availability:
- `--py3-plus` is passed on the commandline.

```diff
-six.text_type
+str
Expand Down Expand Up @@ -536,9 +463,6 @@ Availability:

### `open` alias

Availability:
- `--py3-plus` is passed on the commandline.

```diff
-with io.open('f.txt') as f:
+with open('f.txt') as f:
Expand All @@ -548,9 +472,6 @@ Availability:

### redundant `open` modes

Availability:
- `--py3-plus` is passed on the commandline.

```diff
-open("foo", "U")
+open("foo")
Expand All @@ -571,9 +492,6 @@ Availability:

### `OSError` aliases

Availability:
- `--py3-plus` is passed on the commandline.

```diff
# also understands:
# - IOError
Expand All @@ -596,9 +514,6 @@ Availability:

### `typing.Text` str alias

Availability:
- `--py3-plus` is passed on the commandline.

```diff
-def f(x: Text) -> None:
+def f(x: str) -> None:
Expand All @@ -608,9 +523,6 @@ Availability:

### Unpacking list comprehensions

Availability:
- `--py3-plus` is passed on the commandline.

```diff
-foo, bar, baz = [fn(x) for x in items]
+foo, bar, baz = (fn(x) for x in items)
Expand All @@ -619,9 +531,6 @@ Availability:

### Rewrite `xml.etree.cElementTree` to `xml.etree.ElementTree`

Availability:
- `--py3-plus` is passed on the commandline.

```diff
-import xml.etree.cElementTree as ET
+import xml.etree.ElementTree as ET
Expand All @@ -632,9 +541,6 @@ Availability:

### Rewrite `type` of primitive

Availability:
- `--py3-plus` is passed on the commandline.

```diff
-type('')
+str
Expand Down
2 changes: 1 addition & 1 deletion pyupgrade/_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@


class Settings(NamedTuple):
min_version: Version = (2, 7)
min_version: Version = (3,)
keep_percent_format: bool = False
keep_mock: bool = False
keep_runtime_typing: bool = False
Expand Down
Loading

0 comments on commit 091f30c

Please sign in to comment.