From 5d24561fefe2ac52456aa10bc4cb4192aa46d815 Mon Sep 17 00:00:00 2001 From: rbpatt2019 Date: Tue, 5 Jan 2021 09:21:21 +0000 Subject: [PATCH 1/6] TYP:Add typing for pandas.core.frame.dropna Fixes GH38948. --- pandas/core/frame.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 1abbe37e67b09..09006f9567b4a 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -5074,10 +5074,10 @@ def dropna( self, axis: Axis = 0, how: str = "any", - thresh=None, - subset=None, + thresh Optional[int] = None, + subset: Optional[Union[Hashable, Sequence[Hashable]]] = None, inplace: bool = False, - ): + ) -> Optional[DataFrame]: """ Remove missing values. From f2733e35bd2ea31760122384ec15c2fe81ecdb8e Mon Sep 17 00:00:00 2001 From: rbpatt2019 Date: Tue, 5 Jan 2021 09:45:56 +0000 Subject: [PATCH 2/6] BUG:Add missing `return None` statement for pandas.core.frame.dropna mypy justifiably requires an explicit `return None` when a function return is typed as `Optional` --- pandas/core/frame.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 09006f9567b4a..51b60ce127933 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -5074,7 +5074,7 @@ def dropna( self, axis: Axis = 0, how: str = "any", - thresh Optional[int] = None, + thresh: Optional[int] = None, subset: Optional[Union[Hashable, Sequence[Hashable]]] = None, inplace: bool = False, ) -> Optional[DataFrame]: @@ -5216,6 +5216,7 @@ def dropna( if inplace: self._update_inplace(result) + return None else: return result From f2e2bcf8be84b3b97e47ff501af554826ae58428 Mon Sep 17 00:00:00 2001 From: rbpatt2019 Date: Tue, 5 Jan 2021 13:23:24 +0000 Subject: [PATCH 3/6] TYP:Overload pandas.core.frame.dropna function signature As the `Optional[DataFrame]` return type creates a union of incompatible types (DataFrame and None), we overload the signature on the value of the `inplace` parameter, to force the return of a DataFrame when inplace is false. Fixes GH38948 --- pandas/core/frame.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 51b60ce127933..577daa358c66b 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -5069,6 +5069,28 @@ def notna(self) -> DataFrame: @doc(NDFrame.notna, klass=_shared_doc_kwargs["klass"]) def notnull(self) -> DataFrame: return ~self.isna() + + # Overload function signature to prevent union of conflicting types + # As Optional[DataFrame] is really Union[DataFrame, None] + @overload + def dropna( + self, + axis: Axis = ..., + how: str = ..., + thresh: Optional[int] = ..., + subset: Optional[Union[Hashable, Sequence[Hashable]]] = ..., + inplace: Literal[False] = False, + ) -> DataFrame: ... + + @overload + def dropna( + self, + axis: Axis = ..., + how: str = ..., + thresh: Optional[int] = ..., + subset: Optional[Union[Hashable, Sequence[Hashable]]] = ..., + inplace: Literal[True] = True, + ) -> None: ... def dropna( self, From 63d0410ccaf7f7b816d7a43885510c74ab208dea Mon Sep 17 00:00:00 2001 From: rbpatt2019 Date: Tue, 5 Jan 2021 13:33:29 +0000 Subject: [PATCH 4/6] CLN:Blacken code For GH38948 --- pandas/core/frame.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 577daa358c66b..a9884fab44104 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -5069,7 +5069,7 @@ def notna(self) -> DataFrame: @doc(NDFrame.notna, klass=_shared_doc_kwargs["klass"]) def notnull(self) -> DataFrame: return ~self.isna() - + # Overload function signature to prevent union of conflicting types # As Optional[DataFrame] is really Union[DataFrame, None] @overload @@ -5080,7 +5080,8 @@ def dropna( thresh: Optional[int] = ..., subset: Optional[Union[Hashable, Sequence[Hashable]]] = ..., inplace: Literal[False] = False, - ) -> DataFrame: ... + ) -> DataFrame: + ... @overload def dropna( @@ -5090,7 +5091,8 @@ def dropna( thresh: Optional[int] = ..., subset: Optional[Union[Hashable, Sequence[Hashable]]] = ..., inplace: Literal[True] = True, - ) -> None: ... + ) -> None: + ... def dropna( self, From bc0c5d24bd6450a52ddd2e331ff1cf019b47f911 Mon Sep 17 00:00:00 2001 From: rbpatt2019 Date: Tue, 5 Jan 2021 14:43:27 +0000 Subject: [PATCH 5/6] CLN:Remove superfluous comment GH38948 pull request review --- pandas/core/frame.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index a9884fab44104..21e64bac9cf05 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -5070,8 +5070,6 @@ def notna(self) -> DataFrame: def notnull(self) -> DataFrame: return ~self.isna() - # Overload function signature to prevent union of conflicting types - # As Optional[DataFrame] is really Union[DataFrame, None] @overload def dropna( self, From 55ef7f90c53c0cb7a3532e0486281feabcd77472 Mon Sep 17 00:00:00 2001 From: rbpatt2019 Date: Tue, 5 Jan 2021 14:58:58 +0000 Subject: [PATCH 6/6] TYP:Remove default literals in signature overloads To pass mypy checks for GH38948 --- pandas/core/frame.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 21e64bac9cf05..8b4cc4f1b8fb3 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -5071,13 +5071,15 @@ def notnull(self) -> DataFrame: return ~self.isna() @overload - def dropna( + # https://github.com/python/mypy/issues/6580 + # Overloaded function signatures 1 and 2 overlap with incompatible return types + def dropna( # type: ignore[misc] self, axis: Axis = ..., how: str = ..., thresh: Optional[int] = ..., subset: Optional[Union[Hashable, Sequence[Hashable]]] = ..., - inplace: Literal[False] = False, + inplace: Literal[False] = ..., ) -> DataFrame: ... @@ -5088,7 +5090,7 @@ def dropna( how: str = ..., thresh: Optional[int] = ..., subset: Optional[Union[Hashable, Sequence[Hashable]]] = ..., - inplace: Literal[True] = True, + inplace: Literal[True] = ..., ) -> None: ...