Skip to content

Commit

Permalink
Allow simple container literals as default values (#3703)
Browse files Browse the repository at this point in the history
  • Loading branch information
JonathanPlasse committed Mar 24, 2023
1 parent 0f95056 commit 7da06b9
Show file tree
Hide file tree
Showing 7 changed files with 461 additions and 162 deletions.
198 changes: 140 additions & 58 deletions crates/ruff/resources/test/fixtures/flake8_pyi/PYI011.py
Original file line number Diff line number Diff line change
@@ -1,79 +1,161 @@
import math
import os
import sys
from math import inf

import numpy as np

def f12(
x,
y: str = os.pathsep, # OK
) -> None:
...


def f11(*, x: str = "x") -> None: # OK
...


) -> None: ...
def f11(*, x: str = "x") -> None: ... # OK
def f13(
x: list[str] = [
x: list[str] = [ # OK
"foo",
"bar",
"baz",
] # OK
) -> None:
...


]
) -> None: ...
def f14(
x: tuple[str, ...] = (
x: tuple[str, ...] = ( # OK
"foo",
"bar",
"baz",
) # OK
) -> None:
...


)
) -> None: ...
def f15(
x: set[str] = {
x: set[str] = { # OK
"foo",
"bar",
"baz",
} # OK
) -> None:
...


def f16(x: frozenset[bytes] = frozenset({b"foo", b"bar", b"baz"})) -> None: # OK
...


}
) -> None: ...
def f151(x: dict[int, int] = {1: 2}) -> None: ... # Ok
def f152(
x: dict[
int, int
] = { # OK
1: 2,
**{3: 4},
}
) -> None: ...
def f153(
x: list[
int
] = [ # OK
1,
2,
3,
4,
5,
6,
7,
8,
9,
10,
11,
]
) -> None: ...
def f154(
x: tuple[
str, tuple[str, ...]
] = ( # OK
"foo",
("bar", "baz"),
)
) -> None: ...
def f141(
x: list[
int
] = [ # OK
*range(10)
],
) -> None: ...
def f142(
x: list[
int
] = list( # OK
range(10)
),
) -> None: ...
def f16(
x: frozenset[
bytes
] = frozenset( # OK
{b"foo", b"bar", b"baz"}
)
) -> None: ...
def f17(
x: str = "foo" + "bar", # OK
) -> None:
...


x: str = "foo" # OK
+ "bar",
) -> None: ...
def f18(
x: str = b"foo" + b"bar", # OK
) -> None:
...


x: str = b"foo" # OK
+ b"bar",
) -> None: ...
def f19(
x: object = "foo" + 4, # OK
) -> None:
...


x: object = "foo" # OK
+ 4,
) -> None: ...
def f20(
x: int = 5 + 5, # OK
) -> None:
...


x: int = 5
+ 5, # OK
) -> None: ...
def f21(
x: complex = 3j - 3j, # OK
) -> None:
...


x: complex = 3j
- 3j, # OK
) -> None: ...
def f22(
x: complex = -42.5j + 4.3j, # OK
) -> None:
...
x: complex = -42.5j # OK
+ 4.3j,
) -> None: ...
def f23(
x: bool = True, # OK
) -> None: ...
def f24(
x: float = 3.14, # OK
) -> None: ...
def f25(
x: float = -3.14, # OK
) -> None: ...
def f26(
x: complex = -3.14j, # OK
) -> None: ...
def f27(
x: complex = -3 - 3.14j, # OK
) -> None: ...
def f28(
x: float = math.tau, # OK
) -> None: ...
def f29(
x: float = math.inf, # OK
) -> None: ...
def f30(
x: float = -math.inf, # OK
) -> None: ...
def f31(
x: float = inf, # OK
) -> None: ...
def f32(
x: float = np.inf, # OK
) -> None: ...
def f33(
x: float = math.nan, # OK
) -> None: ...
def f34(
x: float = -math.nan, # OK
) -> None: ...
def f35(
x: complex = math.inf # OK
+ 1j,
) -> None: ...
def f36(
*,
x: str = sys.version, # OK
) -> None: ...
def f37(
*,
x: str = "" # OK
+ "",
) -> None: ...
67 changes: 56 additions & 11 deletions crates/ruff/resources/test/fixtures/flake8_pyi/PYI011.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -11,32 +11,74 @@ def f12(
) -> None: ...
def f11(*, x: str = "x") -> None: ... # OK
def f13(
x: list[
str
] = [ # Error PYI011 Only simple default values allowed for typed arguments
x: list[str] = [ # OK
"foo",
"bar",
"baz",
]
) -> None: ...
def f14(
x: tuple[
str, ...
] = ( # Error PYI011 Only simple default values allowed for typed arguments
x: tuple[str, ...] = ( # OK
"foo",
"bar",
"baz",
)
) -> None: ...
def f15(
x: set[
str
] = { # Error PYI011 Only simple default values allowed for typed arguments
x: set[str] = { # OK
"foo",
"bar",
"baz",
}
) -> None: ...
def f151(x: dict[int, int] = {1: 2}) -> None: ... # Ok
def f152(
x: dict[
int, int
] = { # Error PYI011 Only simple default values allowed for typed arguments
1: 2,
**{3: 4},
}
) -> None: ...
def f153(
x: list[
int
] = [ # Error PYI011 Only simple default values allowed for typed arguments
1,
2,
3,
4,
5,
6,
7,
8,
9,
10,
11,
]
) -> None: ...
def f154(
x: tuple[
str, tuple[str, ...]
] = ( # Error PYI011 Only simple default values allowed for typed arguments
"foo",
("bar", "baz"),
)
) -> None: ...
def f141(
x: list[
int
] = [ # Error PYI011 Only simple default values allowed for typed arguments
*range(10)
],
) -> None: ...
def f142(
x: list[
int
] = list( # Error PYI011 Only simple default values allowed for typed arguments
range(10)
),
) -> None: ...
def f16(
x: frozenset[
bytes
Expand Down Expand Up @@ -109,8 +151,11 @@ def f35(
+ 1j,
) -> None: ...
def f36(
*, x: str = sys.version, # OK
*,
x: str = sys.version, # OK
) -> None: ...
def f37(
*, x: str = "" + "", # Error PYI011 Only simple default values allowed for typed arguments
*,
x: str = "" # Error PYI011 Only simple default values allowed for typed arguments
+ "",
) -> None: ...
52 changes: 52 additions & 0 deletions crates/ruff/resources/test/fixtures/flake8_pyi/PYI014.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,58 @@ def f15(
...


def f151(x={1: 2}) -> None: # Ok
...


def f152(
x={ # OK
1: 2,
**{3: 4},
}
) -> None:
...


def f153(
x=[ # OK
1,
2,
3,
4,
5,
6,
7,
8,
9,
10,
11,
]
) -> None:
...


def f154(
x=( # OK
"foo",
("bar", "baz"),
)
) -> None:
...


def f141(
x=[*range(10)], # OK
) -> None:
...


def f142(
x=list(range(10)), # OK
) -> None:
...


def f16(x=frozenset({b"foo", b"bar", b"baz"})) -> None:
... # OK

Expand Down
Loading

0 comments on commit 7da06b9

Please sign in to comment.