From 34bcaa835a13ea62d3c79e5a537748b59e782fbc Mon Sep 17 00:00:00 2001 From: tp Date: Mon, 30 Dec 2019 20:45:05 +0000 Subject: [PATCH 1/6] Add types to some top-level funcs --- pandas/core/indexes/datetimes.py | 4 ++-- pandas/core/reshape/merge.py | 6 +++--- pandas/core/reshape/pivot.py | 6 +++--- pandas/io/json/_normalize.py | 2 +- pandas/util/_tester.py | 3 ++- 5 files changed, 11 insertions(+), 10 deletions(-) diff --git a/pandas/core/indexes/datetimes.py b/pandas/core/indexes/datetimes.py index 108e24ffee820..9ff968bc554e4 100644 --- a/pandas/core/indexes/datetimes.py +++ b/pandas/core/indexes/datetimes.py @@ -1356,7 +1356,7 @@ def date_range( name=None, closed=None, **kwargs, -): +) -> DatetimeIndex: """ Return a fixed frequency DatetimeIndex. @@ -1522,7 +1522,7 @@ def bdate_range( holidays=None, closed=None, **kwargs, -): +) -> DatetimeIndex: """ Return a fixed frequency DatetimeIndex, with business day as the default frequency. diff --git a/pandas/core/reshape/merge.py b/pandas/core/reshape/merge.py index 37ec05c40940e..7a22a6c846240 100644 --- a/pandas/core/reshape/merge.py +++ b/pandas/core/reshape/merge.py @@ -68,7 +68,7 @@ def merge( copy: bool = True, indicator: bool = False, validate=None, -): +) -> "DataFrame": op = _MergeOperation( left, right, @@ -183,7 +183,7 @@ def merge_ordered( fill_method=None, suffixes=("_x", "_y"), how: str = "outer", -): +) -> "DataFrame": """ Perform merge with optional filling/interpolation. @@ -317,7 +317,7 @@ def merge_asof( tolerance=None, allow_exact_matches: bool = True, direction: str = "backward", -): +) -> "DataFrame": """ Perform an asof merge. This is similar to a left-join except that we match on nearest key rather than equal keys. diff --git a/pandas/core/reshape/pivot.py b/pandas/core/reshape/pivot.py index 4b21045cd0217..7127ba5a6402c 100644 --- a/pandas/core/reshape/pivot.py +++ b/pandas/core/reshape/pivot.py @@ -35,7 +35,7 @@ def pivot_table( dropna=True, margins_name="All", observed=False, -): +) -> "DataFrame": index = _convert_by(index) columns = _convert_by(columns) @@ -148,7 +148,7 @@ def pivot_table( table = table.sort_index(axis=1) if fill_value is not None: - table = table.fillna(value=fill_value, downcast="infer") + table: "DataFrame" = table.fillna(value=fill_value, downcast="infer") if margins: if dropna: @@ -426,7 +426,7 @@ def _convert_by(by): @Substitution("\ndata : DataFrame") @Appender(_shared_docs["pivot"], indents=1) -def pivot(data: "DataFrame", index=None, columns=None, values=None): +def pivot(data: "DataFrame", index=None, columns=None, values=None) -> "DataFrame": if values is None: cols = [columns] if index is None else [index, columns] append = index is None diff --git a/pandas/io/json/_normalize.py b/pandas/io/json/_normalize.py index aa14c3f3a63f3..6f2e6e844f8e5 100644 --- a/pandas/io/json/_normalize.py +++ b/pandas/io/json/_normalize.py @@ -118,7 +118,7 @@ def _json_normalize( errors: Optional[str] = "raise", sep: str = ".", max_level: Optional[int] = None, -): +) -> "DataFrame": """ Normalize semi-structured JSON data into a flat table. diff --git a/pandas/util/_tester.py b/pandas/util/_tester.py index b299f3790ab22..4e60285c9c7e0 100644 --- a/pandas/util/_tester.py +++ b/pandas/util/_tester.py @@ -3,11 +3,12 @@ """ import os import sys +from typing import NoReturn PKG = os.path.dirname(os.path.dirname(__file__)) -def test(extra_args=None): +def test(extra_args=None) -> NoReturn: try: import pytest except ImportError: From 4ef2528ee5dc7322af8f061580fbce9f842cd096 Mon Sep 17 00:00:00 2001 From: tp Date: Mon, 30 Dec 2019 21:13:14 +0000 Subject: [PATCH 2/6] Remove NoReturn --- pandas/util/_tester.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/pandas/util/_tester.py b/pandas/util/_tester.py index 4e60285c9c7e0..b299f3790ab22 100644 --- a/pandas/util/_tester.py +++ b/pandas/util/_tester.py @@ -3,12 +3,11 @@ """ import os import sys -from typing import NoReturn PKG = os.path.dirname(os.path.dirname(__file__)) -def test(extra_args=None) -> NoReturn: +def test(extra_args=None): try: import pytest except ImportError: From 635d94583c1bf1959a42d64c06e228193c57df4a Mon Sep 17 00:00:00 2001 From: tp Date: Mon, 30 Dec 2019 21:58:46 +0000 Subject: [PATCH 3/6] mypy casting --- pandas/core/reshape/pivot.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/pandas/core/reshape/pivot.py b/pandas/core/reshape/pivot.py index 7127ba5a6402c..b405f204d8a2b 100644 --- a/pandas/core/reshape/pivot.py +++ b/pandas/core/reshape/pivot.py @@ -1,4 +1,4 @@ -from typing import TYPE_CHECKING, Callable, Dict, Tuple, Union +from typing import cast, TYPE_CHECKING, Callable, Dict, Tuple, Union import numpy as np @@ -148,7 +148,8 @@ def pivot_table( table = table.sort_index(axis=1) if fill_value is not None: - table: "DataFrame" = table.fillna(value=fill_value, downcast="infer") + filled = table.fillna(value=fill_value, downcast="infer") + table = cast(DataFrame, filled) if margins: if dropna: From 08078283795cdbe3611651cc342946769898f185 Mon Sep 17 00:00:00 2001 From: tp Date: Mon, 30 Dec 2019 22:13:55 +0000 Subject: [PATCH 4/6] mypy part II --- pandas/core/reshape/pivot.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/pandas/core/reshape/pivot.py b/pandas/core/reshape/pivot.py index b405f204d8a2b..2eb2990bd58c4 100644 --- a/pandas/core/reshape/pivot.py +++ b/pandas/core/reshape/pivot.py @@ -1,4 +1,4 @@ -from typing import cast, TYPE_CHECKING, Callable, Dict, Tuple, Union +from typing import TYPE_CHECKING, Callable, Dict, Tuple, Union import numpy as np @@ -149,7 +149,8 @@ def pivot_table( if fill_value is not None: filled = table.fillna(value=fill_value, downcast="infer") - table = cast(DataFrame, filled) + assert filled is not None # needed for mypy + table = filled if margins: if dropna: From 648c1134768e4630809c2a1c877c664fec1f9e9c Mon Sep 17 00:00:00 2001 From: tp Date: Tue, 31 Dec 2019 00:12:32 +0000 Subject: [PATCH 5/6] mypy add type: ignore[assignment] --- pandas/core/reshape/pivot.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/pandas/core/reshape/pivot.py b/pandas/core/reshape/pivot.py index 2eb2990bd58c4..978167cefce2d 100644 --- a/pandas/core/reshape/pivot.py +++ b/pandas/core/reshape/pivot.py @@ -148,9 +148,8 @@ def pivot_table( table = table.sort_index(axis=1) if fill_value is not None: - filled = table.fillna(value=fill_value, downcast="infer") - assert filled is not None # needed for mypy - table = filled + table = table.fillna(fill_value, downcast="infer") # type: ignore[assignment] + assert isinstance(table, DataFrame) if margins: if dropna: From 5a85a327b0ff834d7e68e5993500ff824b7d772c Mon Sep 17 00:00:00 2001 From: tp Date: Tue, 31 Dec 2019 00:47:22 +0000 Subject: [PATCH 6/6] Revert "mypy add type: ignore[assignment]" This reverts commit 648c1134768e4630809c2a1c877c664fec1f9e9c. --- pandas/core/reshape/pivot.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/pandas/core/reshape/pivot.py b/pandas/core/reshape/pivot.py index 978167cefce2d..2eb2990bd58c4 100644 --- a/pandas/core/reshape/pivot.py +++ b/pandas/core/reshape/pivot.py @@ -148,8 +148,9 @@ def pivot_table( table = table.sort_index(axis=1) if fill_value is not None: - table = table.fillna(fill_value, downcast="infer") # type: ignore[assignment] - assert isinstance(table, DataFrame) + filled = table.fillna(value=fill_value, downcast="infer") + assert filled is not None # needed for mypy + table = filled if margins: if dropna: