Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TYP/CI: Demonstrate possible integration of VirtusLab's type tests #45561

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file.
822 changes: 822 additions & 0 deletions pandas/tests/typing/valid/test_frame.py

Large diffs are not rendered by default.

31 changes: 31 additions & 0 deletions pandas/tests/typing/valid/test_interval.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# flake8: noqa: F841
# pyright: reportGeneralTypeIssues = true

import pandas as pd


def test_interval_init() -> None:
i1: pd.Interval = pd.Interval(1, 2, closed="both")
i2: pd.Interval = pd.Interval(1, right=2, closed="right")
i3: pd.Interval = pd.Interval(left=1, right=2, closed="left")


def test_interval_arithmetic() -> None:
i1: pd.Interval = pd.Interval(1, 2, closed="both")
i2: pd.Interval = pd.Interval(1, right=2, closed="right")

i3: pd.Interval = i1 + 1
i4: pd.Interval = i1 - 1
i5: pd.Interval = i1 * 2
i6: pd.Interval = i1 / 2
i7: pd.Interval = i1 // 2


def test_max_intervals() -> None:
i1 = pd.Interval(
pd.Timestamp("2000-01-01"), pd.Timestamp("2000-01-02"), closed="both"
)
i2 = pd.Interval(
pd.Timestamp("2000-01-01T12:00:00"), pd.Timestamp("2000-01-02"), closed="both"
)
print(max(i1.left, i2.left))
168 changes: 168 additions & 0 deletions pandas/tests/typing/valid/test_pandas.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
# flake8: noqa: F841
# TODO: many functions need return types annotations for pyright
# to run with reportGeneralTypeIssues = true
import tempfile
from typing import (
Any,
Dict,
List,
Union,
)

import pandas as pd

from pandas.io.parsers import TextFileReader


def test_types_to_datetime() -> None:
df = pd.DataFrame({"year": [2015, 2016], "month": [2, 3], "day": [4, 5]})
# error: No overload variant of "to_datetime" matches argument type "DataFrame"
pd.to_datetime(df) # type: ignore[call-overload]
# error: No overload variant of "to_datetime" matches argument types "DataFrame",
# "str", "str", "bool"
pd.to_datetime( # type: ignore[call-overload]
df, unit="s", origin="unix", infer_datetime_format=True
)
# error: No overload variant of "to_datetime" matches argument types "DataFrame",
# "str", "bool", "None", "str", "bool"
pd.to_datetime( # type: ignore[call-overload]
df, unit="ns", dayfirst=True, utc=None, format="%M:%D", exact=False
)
pd.to_datetime([1, 2], unit="D", origin=pd.Timestamp("01/01/2000"))
pd.to_datetime([1, 2], unit="D", origin=3)


def test_types_concat() -> None:
s = pd.Series([0, 1, -10])
s2 = pd.Series([7, -5, 10])

pd.concat([s, s2])
pd.concat([s, s2], axis=1)
pd.concat([s, s2], keys=["first", "second"], sort=True)
pd.concat([s, s2], keys=["first", "second"], names=["source", "row"])

# Depends on the axis
# error: Argument 1 to "concat" has incompatible type "Dict[str, Series]"; expected
# "Union[Iterable[DataFrame], Mapping[Hashable, DataFrame]]"
rs1: Union[pd.Series, pd.DataFrame] = pd.concat(
{"a": s, "b": s2} # type:ignore[arg-type]
)
# error: Argument 1 to "concat" has incompatible type "Dict[str, Series]"; expected
# "Union[Iterable[NDFrame], Mapping[Hashable, NDFrame]]"
rs1a: Union[pd.Series, pd.DataFrame] = pd.concat(
{"a": s, "b": s2}, axis=1 # type:ignore[arg-type]
)
# error: Argument 1 to "concat" has incompatible type "Dict[int, Series]"; expected
# "Union[Iterable[DataFrame], Mapping[Hashable, DataFrame]]"
rs2: Union[pd.Series, pd.DataFrame] = pd.concat(
{1: s, 2: s2} # type:ignore[arg-type]
)
# error: Argument 1 to "concat" has incompatible type "Dict[int, Series]"; expected
# "Union[Iterable[NDFrame], Mapping[Hashable, NDFrame]]"
rs2a: Union[pd.Series, pd.DataFrame] = pd.concat(
{1: s, 2: s2}, axis=1 # type:ignore[arg-type]
)
# error: Argument 1 to "concat" has incompatible type "Dict[Optional[int], Series]";
# expected "Union[Iterable[DataFrame], Mapping[Hashable, DataFrame]]"
rs3: Union[pd.Series, pd.DataFrame] = pd.concat(
{1: s, None: s2} # type:ignore[arg-type]
)
# error: Argument 1 to "concat" has incompatible type "Dict[Optional[int], Series]";
# expected "Union[Iterable[NDFrame], Mapping[Hashable, NDFrame]]"
rs3a: Union[pd.Series, pd.DataFrame] = pd.concat(
{1: s, None: s2}, axis=1 # type:ignore[arg-type]
)

df = pd.DataFrame(data={"col1": [1, 2], "col2": [3, 4]})
df2 = pd.DataFrame(data={"col1": [10, 20], "col2": [30, 40]})

pd.concat([df, df2])
pd.concat([df, df2], axis=1)
pd.concat([df, df2], keys=["first", "second"], sort=True)
pd.concat([df, df2], keys=["first", "second"], names=["source", "row"])

# error: Incompatible types in assignment (expression has type "Union[DataFrame,
# Series]", variable has type "DataFrame")
# error: Argument 1 to "concat" has incompatible type "Dict[str, DataFrame]";
# expected "Union[Iterable[NDFrame], Mapping[Hashable, NDFrame]]"
result: pd.DataFrame = pd.concat( # type: ignore[assignment]
{
"a": pd.DataFrame([1, 2, 3]),
"b": pd.DataFrame([4, 5, 6]),
}, # type:ignore[arg-type]
axis=1,
)
# error: Argument 1 to "concat" has incompatible type "Dict[str, Series]"; expected
# "Union[Iterable[NDFrame], Mapping[Hashable, NDFrame]]"
result2: Union[pd.DataFrame, pd.Series] = pd.concat(
{
"a": pd.Series([1, 2, 3]),
"b": pd.Series([4, 5, 6]),
}, # type:ignore[arg-type]
axis=1,
)

# error: Argument 1 to "concat" has incompatible type "Dict[str, DataFrame]";
# expected "Union[Iterable[DataFrame], Mapping[Hashable, DataFrame]]"
rdf1: pd.DataFrame = pd.concat({"a": df, "b": df2}) # type:ignore[arg-type]
# error: Argument 1 to "concat" has incompatible type "Dict[int, DataFrame]";
# expected "Union[Iterable[DataFrame], Mapping[Hashable, DataFrame]]"
rdf2: pd.DataFrame = pd.concat({1: df, 2: df2}) # type:ignore[arg-type]
# error: Argument 1 to "concat" has incompatible type "Dict[Optional[int],
# DataFrame]"; expected "Union[Iterable[DataFrame], Mapping[Hashable, DataFrame]]"
rdf3: pd.DataFrame = pd.concat({1: df, None: df2}) # type:ignore[arg-type]

rdf4: pd.DataFrame = pd.concat(list(map(lambda x: s2, ["some_value", 3])), axis=1)


def test_types_json_normalize() -> None:
data1: List[Dict[str, Any]] = [
{"id": 1, "name": {"first": "Coleen", "last": "Volk"}},
{"name": {"given": "More", "family": "Regner"}},
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to make codespell happy

{"id": 2, "name": "Faye Raker"},
]
df1: pd.DataFrame = pd.json_normalize(data=data1)
df2: pd.DataFrame = pd.json_normalize(data=data1, max_level=0, sep=";")
df3: pd.DataFrame = pd.json_normalize(
data=data1, meta_prefix="id", record_prefix="name", errors="raise"
)
df4: pd.DataFrame = pd.json_normalize(data=data1, record_path=None, meta="id")
data2: Dict[str, Any] = {"name": {"given": "More", "family": "Regner"}}
df5: pd.DataFrame = pd.json_normalize(data=data2)


def test_types_read_csv() -> None:
df = pd.DataFrame(data={"col1": [1, 2], "col2": [3, 4]})
# error: Incompatible types in assignment (expression has type "Optional[str]",
# variable has type "str")
csv_df: str = df.to_csv() # type: ignore[assignment]

with tempfile.NamedTemporaryFile() as file:
df.to_csv(file.name)
df2: pd.DataFrame = pd.read_csv(file.name)
df3: pd.DataFrame = pd.read_csv(file.name, sep="a", squeeze=False)
df4: pd.DataFrame = pd.read_csv(
file.name,
header=None,
prefix="b",
mangle_dupe_cols=True,
keep_default_na=False,
)
df5: pd.DataFrame = pd.read_csv(
file.name, engine="python", true_values=[0, 1, 3], na_filter=False
)
df6: pd.DataFrame = pd.read_csv(
file.name,
skiprows=lambda x: x in [0, 2],
skip_blank_lines=True,
dayfirst=False,
)
df7: pd.DataFrame = pd.read_csv(file.name, nrows=2)
tfr1: TextFileReader = pd.read_csv(
file.name, nrows=2, iterator=True, chunksize=3
)
tfr2: TextFileReader = pd.read_csv(file.name, nrows=2, chunksize=1)
tfr3: TextFileReader = pd.read_csv(
file.name, nrows=2, iterator=False, chunksize=1
)
tfr4: TextFileReader = pd.read_csv(file.name, nrows=2, iterator=True)
Loading