Skip to content

Commit

Permalink
Merge branch 'main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
colin99d committed Feb 19, 2024
2 parents 5723298 + df17dfa commit f3a0e4d
Show file tree
Hide file tree
Showing 61 changed files with 1,774 additions and 1,058 deletions.
19 changes: 19 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
name: main

on:
push:
branches: [main, test-me-*]
tags: '*'
pull_request:

jobs:
main-windows:
uses: asottile/workflows/.github/workflows/tox.yml@v1.6.0
with:
env: '["py38"]'
os: windows-latest
main-linux:
uses: asottile/workflows/.github/workflows/tox.yml@v1.6.0
with:
env: '["py38", "py39", "py310", "py311", "py312"]'
os: ubuntu-latest
25 changes: 12 additions & 13 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.4.0
rev: v4.5.0
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
Expand All @@ -10,33 +10,32 @@ repos:
- id: name-tests-test
- id: requirements-txt-fixer
- repo: https://github.com/asottile/setup-cfg-fmt
rev: v2.2.0
rev: v2.5.0
hooks:
- id: setup-cfg-fmt
- repo: https://github.com/asottile/reorder_python_imports
rev: v3.9.0
- repo: https://github.com/asottile/reorder-python-imports
rev: v3.12.0
hooks:
- id: reorder-python-imports
args: [--py37-plus, --add-import, 'from __future__ import annotations']
args: [--py38-plus, --add-import, 'from __future__ import annotations']
- repo: https://github.com/asottile/add-trailing-comma
rev: v2.4.0
rev: v3.1.0
hooks:
- id: add-trailing-comma
args: [--py36-plus]
- repo: https://github.com/asottile/pyupgrade
rev: v3.3.1
rev: v3.15.1
hooks:
- id: pyupgrade
args: [--py37-plus]
- repo: https://github.com/pre-commit/mirrors-autopep8
rev: v2.0.1
args: [--py38-plus]
- repo: https://github.com/hhatto/autopep8
rev: v2.0.4
hooks:
- id: autopep8
- repo: https://github.com/PyCQA/flake8
rev: 6.0.0
rev: 7.0.0
hooks:
- id: flake8
- repo: https://github.com/pre-commit/mirrors-mypy
rev: v0.991
rev: v1.8.0
hooks:
- id: mypy
84 changes: 77 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
[![Build Status](https://dev.azure.com/asottile/asottile/_apis/build/status/asottile.pyupgrade?branchName=main)](https://dev.azure.com/asottile/asottile/_build/latest?definitionId=2&branchName=main)
[![Azure DevOps coverage](https://img.shields.io/azure-devops/coverage/asottile/asottile/2/main.svg)](https://dev.azure.com/asottile/asottile/_build/latest?definitionId=2&branchName=main)
[![build status](https://github.com/asottile/pyupgrade/actions/workflows/main.yml/badge.svg)](https://github.com/asottile/pyupgrade/actions/workflows/main.yml)
[![pre-commit.ci status](https://results.pre-commit.ci/badge/github/asottile/pyupgrade/main.svg)](https://results.pre-commit.ci/latest/github/asottile/pyupgrade/main)

pyupgrade
Expand All @@ -22,7 +21,7 @@ Sample `.pre-commit-config.yaml`:

```yaml
- repo: https://github.com/asottile/pyupgrade
rev: v3.3.1
rev: v3.15.1
hooks:
- id: pyupgrade
```
Expand Down Expand Up @@ -57,6 +56,32 @@ Sample `.pre-commit-config.yaml`:
+{a: b for a, b in y}
```

### Replace unnecessary lambdas in `collections.defaultdict` calls

```diff
-defaultdict(lambda: [])
+defaultdict(list)
-defaultdict(lambda: list())
+defaultdict(list)
-defaultdict(lambda: {})
+defaultdict(dict)
-defaultdict(lambda: dict())
+defaultdict(dict)
-defaultdict(lambda: ())
+defaultdict(tuple)
-defaultdict(lambda: tuple())
+defaultdict(tuple)
-defaultdict(lambda: set())
+defaultdict(set)
-defaultdict(lambda: 0)
+defaultdict(int)
-defaultdict(lambda: 0.0)
+defaultdict(float)
-defaultdict(lambda: 0j)
+defaultdict(complex)
-defaultdict(lambda: '')
+defaultdict(str)
```

### Format Specifiers

Expand Down Expand Up @@ -106,10 +131,6 @@ Availability:
# this fixes a syntax error in python3.3+
-'\N'
+r'\N'

# note: pyupgrade is timid in one case (that's usually a mistake)
# in python2.x `'\u2603'` is the same as `'\\u2603'` without `unicode_literals`
# but in python3.x, that's our friend ☃
```

### `is` / `is not` comparison to constant literals
Expand Down Expand Up @@ -159,6 +180,22 @@ A fix for [python-modernize/python-modernize#178]

[python-modernize/python-modernize#178]: https://github.com/python-modernize/python-modernize/issues/178

### constant fold `isinstance` / `issubclass` / `except`

```diff
-isinstance(x, (int, int))
+isinstance(x, int)

-issubclass(y, (str, str))
+issubclass(y, str)

try:
raises()
-except (Error1, Error1, Error2):
+except (Error1, Error2):
pass
```

### unittest deprecated aliases

Rewrites [deprecated unittest method aliases](https://docs.python.org/3/library/unittest.html#deprecated-aliases) to their non-deprecated forms.
Expand Down Expand Up @@ -514,6 +551,30 @@ Note that `if` blocks without an `else` will not be rewritten as it could introd
handle_error()
```

### `TimeoutError` aliases

Availability:
- `--py310-plus` for `socket.timeout`
- `--py311-plus` for `asyncio.TimeoutError`

```diff

def throw(a):
if a:
- raise asyncio.TimeoutError('boom')
+ raise TimeoutError('boom')
else:
- raise socket.timeout('boom')
+ raise TimeoutError('boom')

def catch(a):
try:
throw(a)
- except (asyncio.TimeoutError, socket.timeout):
+ except TimeoutError:
handle_error()
```

### `typing.Text` str alias

```diff
Expand Down Expand Up @@ -635,6 +696,15 @@ Availability:
...
```

### shlex.join

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

```diff
-' '.join(shlex.quote(arg) for arg in cmd)
+shlex.join(cmd)
```

### replace `@functools.lru_cache(maxsize=None)` with shorthand

Expand Down
23 changes: 0 additions & 23 deletions azure-pipelines.yml

This file was deleted.

10 changes: 10 additions & 0 deletions pyupgrade/_ast_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,13 @@ def is_async_listcomp(node: ast.ListComp) -> bool:
any(gen.is_async for gen in node.generators) or
contains_await(node)
)


def is_type_check(node: ast.AST) -> bool:
return (
isinstance(node, ast.Call) and
isinstance(node.func, ast.Name) and
node.func.id in {'isinstance', 'issubclass'} and
len(node.args) == 2 and
not has_starargs(node)
)
10 changes: 4 additions & 6 deletions pyupgrade/_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,15 @@
from typing import Iterable
from typing import List
from typing import NamedTuple
from typing import Protocol
from typing import Tuple
from typing import TYPE_CHECKING
from typing import TypeVar

from tokenize_rt import Offset
from tokenize_rt import Token

from pyupgrade import _plugins

if TYPE_CHECKING:
from typing import Protocol
else:
Protocol = object

Version = Tuple[int, ...]


Expand All @@ -43,8 +38,11 @@ class State(NamedTuple):

RECORD_FROM_IMPORTS = frozenset((
'__future__',
'asyncio',
'collections',
'functools',
'mmap',
'os',
'select',
'six',
'six.moves',
Expand Down
18 changes: 11 additions & 7 deletions pyupgrade/_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
from pyupgrade._string_helpers import is_codec
from pyupgrade._string_helpers import parse_format
from pyupgrade._string_helpers import unparse_parsed_string
from pyupgrade._token_helpers import CLOSING
from pyupgrade._token_helpers import OPENING
from pyupgrade._token_helpers import is_close
from pyupgrade._token_helpers import is_open
from pyupgrade._token_helpers import remove_brace


Expand Down Expand Up @@ -161,9 +161,9 @@ def _fix_extraneous_parens(tokens: list[Token], i: int) -> None:
# found comma or yield at depth 1: this is a tuple / coroutine
if depth == 1 and tokens[i].src in {',', 'yield'}:
return
elif tokens[i].src in OPENING:
elif is_open(tokens[i]):
depth += 1
elif tokens[i].src in CLOSING:
elif is_close(tokens[i]):
depth -= 1
end = i

Expand Down Expand Up @@ -195,7 +195,7 @@ def _fix_format_literal(tokens: list[Token], end: int) -> None:
for i in parts:
# f'foo {0}'.format(...) would get turned into a SyntaxError
prefix, _ = parse_string_literal(tokens[i].src)
if 'f' in prefix.lower():
if 'f' in prefix.lower(): # pragma: <3.12 cover
return

try:
Expand Down Expand Up @@ -244,7 +244,7 @@ def _fix_encode_to_binary(tokens: list[Token], i: int) -> None:
):
victims = slice(i - 1, i + 4)
prefix, rest = parse_string_literal(tokens[i + 2].src)
if 'f' in prefix.lower():
if 'f' in prefix.lower(): # pragma: <3.12 cover
return
encoding = ast.literal_eval(prefix + rest)
if is_codec(encoding, 'ascii') or is_codec(encoding, 'utf-8'):
Expand Down Expand Up @@ -284,7 +284,7 @@ def _fix_tokens(contents_text: str) -> str:
for i, token in reversed_enumerate(tokens):
if token.name == 'STRING':
tokens[i] = _fix_escape_sequences(_remove_u_prefix(tokens[i]))
elif token.src == '(':
elif token.matches(name='OP', src='('):
_fix_extraneous_parens(tokens, i)
elif token.src == 'format' and i > 0 and tokens[i - 1].src == '.':
_fix_format_literal(tokens, i - 2)
Expand Down Expand Up @@ -374,6 +374,10 @@ def main(argv: Sequence[str] | None = None) -> int:
'--py311-plus',
action='store_const', dest='min_version', const=(3, 11),
)
parser.add_argument(
'--py312-plus',
action='store_const', dest='min_version', const=(3, 12),
)
args = parser.parse_args(argv)

ret = 0
Expand Down

0 comments on commit f3a0e4d

Please sign in to comment.