Skip to content

Replace aliases to builtins with imports #1244

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

Merged
merged 1 commit into from
Jun 7, 2025
Merged
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
9 changes: 5 additions & 4 deletions pandas-stubs/core/frame.pyi
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
from builtins import (
bool as _bool,
str as _str,
)
from collections.abc import (
Callable,
Hashable,
Expand Down Expand Up @@ -161,9 +165,6 @@ from pandas._typing import (
from pandas.io.formats.style import Styler
from pandas.plotting import PlotAccessor

_str = str
_bool = bool

class _iLocIndexerFrame(_iLocIndexer, Generic[_T]):
@overload
def __getitem__(self, idx: tuple[int, int]) -> Scalar: ...
Expand Down Expand Up @@ -2148,7 +2149,7 @@ class DataFrame(NDFrame, OpsMixin, _GetItemHack):
def reindex_like(
self,
other: DataFrame,
method: _str | FillnaOptions | Literal["nearest"] | None = ...,
method: FillnaOptions | Literal["nearest"] | None = ...,
Comment on lines -2151 to +2152
Copy link
Contributor Author

Choose a reason for hiding this comment

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

ruff can now detect that _str shadows the literals we accept so it started complaining here

Copy link
Collaborator

Choose a reason for hiding this comment

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

and this is a better change because the set of strings that are accepted is limited

copy: _bool = ...,
limit: int | None = ...,
tolerance: Scalar | AnyArrayLike | Sequence[Scalar] = ...,
Expand Down
7 changes: 4 additions & 3 deletions pandas-stubs/core/generic.pyi
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
from builtins import (
bool as _bool,
str as _str,
)
from collections.abc import (
Callable,
Hashable,
Expand Down Expand Up @@ -60,9 +64,6 @@ from pandas._typing import (
from pandas.io.pytables import HDFStore
from pandas.io.sql import SQLTable

_bool = bool
_str = str

class NDFrame(indexing.IndexingMixin):
__hash__: ClassVar[None] # type: ignore[assignment] # pyright: ignore[reportIncompatibleMethodOverride]

Expand Down
3 changes: 1 addition & 2 deletions pandas-stubs/core/indexes/base.pyi
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from builtins import str as _str
from collections.abc import (
Callable,
Hashable,
Expand Down Expand Up @@ -66,8 +67,6 @@ from pandas._typing import (

class InvalidIndexError(Exception): ...

_str = str

class Index(IndexOpsMixin[S1]):
__hash__: ClassVar[None] # type: ignore[assignment]
# overloads with additional dtypes
Expand Down
9 changes: 5 additions & 4 deletions pandas-stubs/core/series.pyi
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
from builtins import (
bool as _bool,
str as _str,
)
from collections import dict_keys # type: ignore[attr-defined]
from collections.abc import (
Callable,
Expand Down Expand Up @@ -179,9 +183,6 @@ from pandas.core.dtypes.dtypes import CategoricalDtype

from pandas.plotting import PlotAccessor

_bool: TypeAlias = bool
_str: TypeAlias = str

class _iLocIndexerSeries(_iLocIndexer, Generic[S1]):
# get item
@overload
Expand Down Expand Up @@ -1078,7 +1079,7 @@ class Series(IndexOpsMixin[S1], NDFrame):
def reindex_like(
self,
other: Series[S1],
method: _str | FillnaOptions | Literal["nearest"] | None = ...,
method: FillnaOptions | Literal["nearest"] | None = ...,
copy: _bool = ...,
limit: int | None = ...,
tolerance: Scalar | AnyArrayLike | Sequence[Scalar] = ...,
Expand Down
4 changes: 1 addition & 3 deletions pandas-stubs/core/strings.pyi
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# pyright: strict
from builtins import slice as _slice
from collections.abc import (
Callable,
Sequence,
Expand All @@ -8,7 +9,6 @@ from typing import (
Any,
Generic,
Literal,
TypeAlias,
TypeVar,
overload,
)
Expand Down Expand Up @@ -47,8 +47,6 @@ _T_STR = TypeVar("_T_STR", bound=Series[str] | Index[str])
# Used for the result of str.partition
_T_OBJECT = TypeVar("_T_OBJECT", bound=Series[type[object]] | Index[type[object]])

_slice: TypeAlias = slice

class StringMethods(
NoNewAttributesMixin,
Generic[T, _T_EXPANDING, _T_BOOL, _T_LIST_STR, _T_INT, _T_BYTES, _T_STR, _T_OBJECT],
Expand Down