TYP: try out TypeGuard
State: closed

mypy throws a bunch of strange complaints locally (also on main), so let's see what the CI says...

Labels:
name: Typing
description: type annotations, mypy/pyright type checking

Comments:
This is a great idea. What kind of errors were you getting?
Somehow it doesn't think typing has a TypeGuard attr.  I'm not sure about the CI, but locally i was definitely running on py310

```
mypy.....................................................................Failed
- hook id: mypy
- exit code: 1

pandas/_libs/lib.pyi:5: error: Module "typing" has no attribute "TypeGuard"  [attr-defined]
pandas/core/dtypes/inference.py:38: error: Module "typing" has no attribute "TypeGuard"  [attr-defined]
pandas/core/interchange/column.py:84: error: Signature of "size" incompatible with supertype "Column"  [override]
pandas/core/series.py:4411: error: Missing return statement  [empty-body]
pandas/core/frame.py:9278: error: Missing return statement  [empty-body]
```

Locally, the mypy output on main is:

```
pandas/util/_decorators.py:401: error: Item "str" of "Union[str, Callable[..., Any]]" has no attribute "_docstring_components"  [union-attr]
pandas/util/_decorators.py:401: error: Item "function" of "Union[str, Callable[..., Any]]" has no attribute "_docstring_components"  [union-attr]
pandas/io/common.py:1056: error: "BaseBuffer" has no attribute "readable"  [attr-defined]
pandas/io/common.py:1066: error: "BaseBuffer" has no attribute "writable"  [attr-defined]
pandas/core/arrays/base.py:1649: error: Unused "type: ignore" comment
pandas/core/arrays/base.py:1653: error: Unused "type: ignore" comment
pandas/core/interchange/column.py:84: error: Unused "type: ignore" comment
pandas/core/base.py:1110: error: "ExtensionArray" has no attribute "memory_usage"  [attr-defined]
pandas/plotting/_core.py:1817: error: "Dict[str, List[EntryPoint]]" has no attribute "select"  [attr-defined]
pandas/core/series.py:1656: error: Item "str" of "Union[str, PathLike[str], WriteBuffer[str]]" has no attribute "write"  [union-attr]
pandas/core/series.py:1656: error: Item "PathLike[str]" of "Union[str, PathLike[str], WriteBuffer[str]]" has no attribute "write"  [union-attr]
pandas/core/series.py:1658: error: Argument 1 to "open" has incompatible type "Union[str, PathLike[str], WriteBuffer[str]]"; expected "Union[Union[str, bytes, PathLike[str], PathLike[bytes]], int]"  [arg-type]
pandas/core/series.py:4412: error: Unused "type: ignore" comment
pandas/core/series.py:5021: error: Signature of "fillna" incompatible with supertype "NDFrame"  [override]
pandas/core/series.py:5021: note:      Superclass:
pandas/core/series.py:5021: note:          @overload
pandas/core/series.py:5021: note:          def fillna(self, value: Union[Hashable, Series, DataFrame] = ..., *, method: Optional[Literal['backfill', 'bfill', 'ffill', 'pad']] = ..., axis: Union[int, None, Literal['index', 'columns', 'rows']] = ..., inplace: Literal[False] = ..., limit: Optional[int] = ..., downcast: Optional[Dict[Any, Any]] = ...) -> Series
pandas/core/series.py:5021: note:          @overload
pandas/core/series.py:5021: note:          def fillna(self, value: Union[Hashable, Mapping[Any, Any], Series, DataFrame] = ..., *, method: Optional[Literal['backfill', 'bfill', 'ffill', 'pad']] = ..., axis: Optional[Union[int, Literal['index', 'columns', 'rows']]] = ..., inplace: Literal[True], limit: Optional[int] = ..., downcast: Optional[Dict[Any, Any]] = ...) -> None
pandas/core/series.py:5021: note:          @overload
pandas/core/series.py:5021: note:          def fillna(self, value: Union[Hashable, Series, DataFrame] = ..., *, method: Optional[Literal['backfill', 'bfill', 'ffill', 'pad']] = ..., axis: Union[int, None, Literal['index', 'columns', 'rows']] = ..., inplace: bool = ..., limit: Optional[int] = ..., downcast: Optional[Dict[Any, Any]] = ...) -> Optional[Series]
pandas/core/series.py:5021: note:      Subclass:
pandas/core/series.py:5021: note:          def fillna(self, value: Union[Hashable, Mapping[Any, Any], Series, DataFrame] = ..., *, method: Optional[Literal['backfill', 'bfill', 'ffill', 'pad']] = ..., axis: Optional[Union[int, Literal['index', 'columns', 'rows']]] = ..., inplace: bool = ..., limit: Optional[int] = ..., downcast: Optional[Dict[Any, Any]] = ...) -> Optional[Series]
pandas/core/series.py:5100: error: Signature of "replace" incompatible with supertype "NDFrame"  [override]
pandas/core/series.py:5100: note:      Superclass:
pandas/core/series.py:5100: note:          @overload
pandas/core/series.py:5100: note:          def replace(self, to_replace: Any = ..., value: Any = ..., *, inplace: Literal[False] = ..., limit: Optional[int] = ..., regex: bool = ..., method: Union[_NoDefault, Literal['pad', 'ffill', 'bfill']] = ...) -> Series
pandas/core/series.py:5100: note:          @overload
pandas/core/series.py:5100: note:          def replace(self, to_replace: Any = ..., value: Any = ..., *, inplace: Literal[True], limit: Optional[int] = ..., regex: bool = ..., method: Literal['pad', 'ffill', 'bfill', _NoDefault.no_default] = ...) -> None
pandas/core/series.py:5100: note:          @overload
pandas/core/series.py:5100: note:          def replace(self, to_replace: Any = ..., value: Any = ..., *, inplace: bool = ..., limit: Optional[int] = ..., regex: bool = ..., method: Union[_NoDefault, Literal['pad', 'ffill', 'bfill']] = ...) -> Optional[Series]
pandas/core/series.py:5100: note:      Subclass:
pandas/core/series.py:5100: note:          def replace(self, to_replace: Any = ..., value: Any = ..., *, inplace: bool = ..., limit: Optional[int] = ..., regex: bool = ..., method: Literal['pad', 'ffill', 'bfill', _NoDefault.no_default] = ...) -> Optional[Series]
pandas/core/frame.py:5527: error: Signature of "fillna" incompatible with supertype "NDFrame"  [override]
pandas/core/frame.py:5527: note:      Superclass:
pandas/core/frame.py:5527: note:          @overload
pandas/core/frame.py:5527: note:          def fillna(self, value: Union[Hashable, Series, DataFrame] = ..., *, method: Optional[Literal['backfill', 'bfill', 'ffill', 'pad']] = ..., axis: Union[int, None, Literal['index', 'columns', 'rows']] = ..., inplace: Literal[False] = ..., limit: Optional[int] = ..., downcast: Optional[Dict[Any, Any]] = ...) -> DataFrame
pandas/core/frame.py:5527: note:          @overload
pandas/core/frame.py:5527: note:          def fillna(self, value: Union[Hashable, Mapping[Any, Any], Series, DataFrame] = ..., *, method: Optional[Literal['backfill', 'bfill', 'ffill', 'pad']] = ..., axis: Optional[Union[int, Literal['index', 'columns', 'rows']]] = ..., inplace: Literal[True], limit: Optional[int] = ..., downcast: Optional[Dict[Any, Any]] = ...) -> None
pandas/core/frame.py:5527: note:          @overload
pandas/core/frame.py:5527: note:          def fillna(self, value: Union[Hashable, Series, DataFrame] = ..., *, method: Optional[Literal['backfill', 'bfill', 'ffill', 'pad']] = ..., axis: Union[int, None, Literal['index', 'columns', 'rows']] = ..., inplace: bool = ..., limit: Optional[int] = ..., downcast: Optional[Dict[Any, Any]] = ...) -> Optional[DataFrame]
pandas/core/frame.py:5527: note:      Subclass:
pandas/core/frame.py:5527: note:          def fillna(self, value: Union[Hashable, Mapping[Any, Any], Series, DataFrame] = ..., *, method: Optional[Literal['backfill', 'bfill', 'ffill', 'pad']] = ..., axis: Optional[Union[int, Literal['index', 'columns', 'rows']]] = ..., inplace: bool = ..., limit: Optional[int] = ..., downcast: Optional[Dict[Any, Any]] = ...) -> Optional[DataFrame]
pandas/core/frame.py:5617: error: Signature of "replace" incompatible with supertype "NDFrame"  [override]
pandas/core/frame.py:5617: note:      Superclass:
pandas/core/frame.py:5617: note:          @overload
pandas/core/frame.py:5617: note:          def replace(self, to_replace: Any = ..., value: Any = ..., *, inplace: Literal[False] = ..., limit: Optional[int] = ..., regex: bool = ..., method: Union[_NoDefault, Literal['pad', 'ffill', 'bfill']] = ...) -> DataFrame
pandas/core/frame.py:5617: note:          @overload
pandas/core/frame.py:5617: note:          def replace(self, to_replace: Any = ..., value: Any = ..., *, inplace: Literal[True], limit: Optional[int] = ..., regex: bool = ..., method: Literal['pad', 'ffill', 'bfill', _NoDefault.no_default] = ...) -> None
pandas/core/frame.py:5617: note:          @overload
pandas/core/frame.py:5617: note:          def replace(self, to_replace: Any = ..., value: Any = ..., *, inplace: bool = ..., limit: Optional[int] = ..., regex: bool = ..., method: Union[_NoDefault, Literal['pad', 'ffill', 'bfill']] = ...) -> Optional[DataFrame]
pandas/core/frame.py:5617: note:      Subclass:
pandas/core/frame.py:5617: note:          def replace(self, to_replace: Any = ..., value: Any = ..., *, inplace: bool = ..., limit: Optional[int] = ..., regex: bool = ..., method: Literal['pad', 'ffill', 'bfill', _NoDefault.no_default] = ...) -> Optional[DataFrame]
pandas/core/frame.py:6757: error: Signature of "sort_values" incompatible with supertype "NDFrame"  [override]
pandas/core/frame.py:6757: note:      Superclass:
pandas/core/frame.py:6757: note:          @overload
pandas/core/frame.py:6757: note:          def sort_values(self, *, axis: Union[int, Literal['index', 'columns', 'rows']] = ..., ascending: Union[bool, Sequence[bool]] = ..., inplace: Literal[False] = ..., kind: str = ..., na_position: str = ..., ignore_index: bool = ..., key: Optional[Callable[[Series], Union[Series, Union[Union[ExtensionArray, ndarray[Any, Any]], Index, Series]]]] = ...) -> DataFrame
pandas/core/frame.py:6757: note:          @overload
pandas/core/frame.py:6757: note:          def sort_values(self, *, axis: Union[int, Literal['index', 'columns', 'rows']] = ..., ascending: Union[bool, Sequence[bool]] = ..., inplace: Literal[True], kind: str = ..., na_position: str = ..., ignore_index: bool = ..., key: Optional[Callable[[Series], Union[Series, Union[Union[ExtensionArray, ndarray[Any, Any]], Index, Series]]]] = ...) -> None
pandas/core/frame.py:6757: note:          @overload
pandas/core/frame.py:6757: note:          def sort_values(self, *, axis: Union[int, Literal['index', 'columns', 'rows']] = ..., ascending: Union[bool, Sequence[bool]] = ..., inplace: bool = ..., kind: str = ..., na_position: str = ..., ignore_index: bool = ..., key: Optional[Callable[[Series], Union[Series, Union[Union[ExtensionArray, ndarray[Any, Any]], Index, Series]]]] = ...) -> Optional[DataFrame]
pandas/core/frame.py:6757: note:      Subclass:
pandas/core/frame.py:6757: note:          def sort_values(self, by: Union[Hashable, Sequence[Hashable]], *, axis: Union[int, Literal['index', 'columns', 'rows']] = ..., ascending: Union[bool, List[bool], Tuple[bool, ...]] = ..., inplace: bool = ..., kind: str = ..., na_position: str = ..., ignore_index: bool = ..., key: Optional[Callable[[Series], Union[Series, Union[Union[ExtensionArray, ndarray[Any, Any]], Index, Series]]]] = ...) -> Optional[DataFrame]
pandas/core/frame.py:9279: error: Unused "type: ignore" comment
```