Skip to content

Commit a21b419

Browse files
committed
Test dataframe pipe typing
1 parent a370cab commit a21b419

File tree

1 file changed

+107
-5
lines changed

1 file changed

+107
-5
lines changed

tests/test_frame.py

Lines changed: 107 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1436,21 +1436,123 @@ def foo(df: pd.DataFrame) -> pd.DataFrame:
14361436
.pipe(foo)
14371437
)
14381438

1439+
df = pd.DataFrame({"a": [1], "b": [2]})
14391440
check(assert_type(val, pd.DataFrame), pd.DataFrame)
14401441

1441-
check(assert_type(pd.DataFrame({"a": [1]}).pipe(foo), pd.DataFrame), pd.DataFrame)
1442+
check(assert_type(df.pipe(foo), pd.DataFrame), pd.DataFrame)
14421443

14431444
def bar(val: Styler) -> Styler:
14441445
return val
14451446

1446-
check(
1447-
assert_type(pd.DataFrame({"a": [1], "b": [1]}).style.pipe(bar), Styler), Styler
1448-
)
1447+
check(assert_type(df.style.pipe(bar), Styler), Styler)
14491448

14501449
def baz(val: Styler) -> str:
14511450
return val.to_latex()
14521451

1453-
check(assert_type(pd.DataFrame({"a": [1], "b": [1]}).style.pipe(baz), str), str)
1452+
check(assert_type(df.style.pipe(baz), str), str)
1453+
1454+
def qux(
1455+
df: pd.DataFrame,
1456+
positional_only: int,
1457+
/,
1458+
argument_1: list[float],
1459+
argument_2: str,
1460+
*,
1461+
keyword_only: tuple[int, int],
1462+
) -> pd.DataFrame:
1463+
return pd.DataFrame(df)
1464+
1465+
check(
1466+
assert_type(
1467+
df.pipe(qux, 1, [1.0, 2.0], argument_2="hi", keyword_only=(1, 2)),
1468+
pd.DataFrame,
1469+
),
1470+
pd.DataFrame,
1471+
)
1472+
1473+
if TYPE_CHECKING_INVALID_USAGE:
1474+
df.pipe(
1475+
qux,
1476+
"a", # type: ignore[arg-type] # pyright: ignore[reportGeneralTypeIssues]
1477+
[1.0, 2.0],
1478+
argument_2="hi",
1479+
keyword_only=(1, 2),
1480+
)
1481+
df.pipe(
1482+
qux,
1483+
1,
1484+
[1.0, "b"], # type: ignore[list-item] # pyright: ignore[reportGeneralTypeIssues]
1485+
argument_2="hi",
1486+
keyword_only=(1, 2),
1487+
)
1488+
df.pipe(
1489+
qux,
1490+
1,
1491+
[1.0, 2.0],
1492+
argument_2=11, # type: ignore[arg-type] # pyright: ignore[reportGeneralTypeIssues]
1493+
keyword_only=(1, 2),
1494+
)
1495+
df.pipe(
1496+
qux,
1497+
1,
1498+
[1.0, 2.0],
1499+
argument_2="hi",
1500+
keyword_only=(1,), # type: ignore[arg-type] # pyright: ignore[reportGeneralTypeIssues]
1501+
)
1502+
df.pipe( # type: ignore[call-arg]
1503+
qux,
1504+
1,
1505+
[1.0, 2.0],
1506+
argument_3="hi", # pyright: ignore[reportGeneralTypeIssues]
1507+
keyword_only=(1, 2),
1508+
)
1509+
df.pipe( # type: ignore[misc]
1510+
qux,
1511+
1,
1512+
[1.0, 2.0],
1513+
11, # type: ignore[arg-type]
1514+
(1, 2), # pyright: ignore[reportGeneralTypeIssues]
1515+
)
1516+
df.pipe( # type: ignore[call-arg]
1517+
qux,
1518+
positional_only=1, # pyright: ignore[reportGeneralTypeIssues]
1519+
argument_1=[1.0, 2.0],
1520+
argument_2=11, # type: ignore[arg-type]
1521+
keyword_only=(1, 2),
1522+
)
1523+
1524+
def dataframe_not_first_arg(x: int, df: pd.DataFrame) -> pd.DataFrame:
1525+
return df
1526+
1527+
check(
1528+
assert_type(
1529+
df.pipe(
1530+
(
1531+
dataframe_not_first_arg,
1532+
"df",
1533+
),
1534+
1,
1535+
),
1536+
pd.DataFrame,
1537+
),
1538+
pd.DataFrame,
1539+
)
1540+
1541+
if TYPE_CHECKING_INVALID_USAGE:
1542+
df.pipe(
1543+
(
1544+
dataframe_not_first_arg, # type: ignore[arg-type]
1545+
1, # pyright: ignore[reportGeneralTypeIssues]
1546+
),
1547+
1,
1548+
)
1549+
df.pipe(
1550+
( # pyright: ignore[reportGeneralTypeIssues]
1551+
1, # type: ignore[arg-type] # pyright: ignore[reportGeneralTypeIssues]
1552+
"df",
1553+
),
1554+
1,
1555+
)
14541556

14551557

14561558
# set_flags() method added in 1.2.0 https://pandas.pydata.org/docs/whatsnew/v1.2.0.html

0 commit comments

Comments
 (0)