Skip to content

TYP/CI: bump mypy&pyright #46905

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 7 commits into from
May 15, 2022
Merged

TYP/CI: bump mypy&pyright #46905

merged 7 commits into from
May 15, 2022

Conversation

twoertwein
Copy link
Member

Mypy aims to have a monthly release.

yield ".".join(normalized_locale)
# error: Argument 1 to "join" of "str" has incompatible type
# "Tuple[Optional[str], Optional[str]]"; expected "Iterable[str]"
yield ".".join(normalized_locale) # type: ignore[arg-type]
Copy link
Member Author

Choose a reason for hiding this comment

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

mypy doesn't seem to understand all(...) to narrow the type.

Copy link
Member

Choose a reason for hiding this comment

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

this works, but let's not do this yet! (until we can use it elsewhere)

        def all_not_none(arg: Iterable[str | None]) -> TypeGuard[Iterable[str]]:
            return all(x is not None for x in arg)

        if all_not_none(normalized_locale):

Copy link
Member

Choose a reason for hiding this comment

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

looking through the code there are a couple of other places this pattern is used that do not use all_not_none from pandas/core/common.py

maybe change this one here, then in the future we only need the TypeGuard in one place.

    try:
        import pandas.core.common as com

        locale.setlocale(lc_var, new_locale)
        normalized_locale = locale.getlocale()
        if com.all_not_none(*normalized_locale):

note need to prevent circular import.

Copy link
Member Author

Choose a reason for hiding this comment

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

Have there been discussions about using typing_extensions? Then we could already use TypeGuard.

Copy link
Member

Choose a reason for hiding this comment

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

no we don't use typing_extensions but we can import from typing in pandas._typing with a guard so that it resolves to Any in older python versions.

Copy link
Member

Choose a reason for hiding this comment

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

Have there been discussions about using typing_extensions?

As long as typing_extensions is not a run-time dependency, I would not be adverse to it being added as a dev dependency.

@twoertwein
Copy link
Member Author

Marking as draft until mypy 0.950 is available through conda

@twoertwein twoertwein marked this pull request as draft May 1, 2022 20:24
@simonjayhawkins simonjayhawkins added the Typing type annotations, mypy/pyright type checking label May 2, 2022
@simonjayhawkins simonjayhawkins added this to the 1.5 milestone May 2, 2022
@twoertwein
Copy link
Member Author

mypy 0.950 is now available on conda-forge :)

@twoertwein twoertwein marked this pull request as ready for review May 13, 2022 23:17
@@ -957,7 +957,7 @@ def equals(self, other) -> bool:
return array_equivalent(left, right, dtype_equal=True)

def _quantile(
self: BaseMaskedArrayT, qs: npt.NDArray[np.float64], interpolation: str
self, qs: npt.NDArray[np.float64], interpolation: str
) -> BaseMaskedArray:
Copy link
Member Author

Choose a reason for hiding this comment

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

Pyright didn't previously find this. I assume the return value can be any BaseMaskedArray.

Copy link
Member

Choose a reason for hiding this comment

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

yep. maybe add type annotations to _maybe_mask_result also.

Copy link
Member

Choose a reason for hiding this comment

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

can be follow-on. I suspect that _maybe_mask_result should probably return the same type, but since we use explicit Subclass constructors this would not be the case if we subclass further.

Copy link
Member Author

Choose a reason for hiding this comment

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

I will leave this out - it would require quite a few ignore comments if we do something like:

    @overload
    def _maybe_mask_result(self, result: ArrayLike, mask: ArrayLike) -> ArrayLike:
        ...

    @overload
    def _maybe_mask_result(
        self, result: tuple[ArrayLike, ArrayLike], mask: ArrayLike
    ) -> tuple[ArrayLike, ArrayLike]:
        ...

    def _maybe_mask_result(
        self, result: ArrayLike | tuple[ArrayLike, ArrayLike], mask: ArrayLike
    ) -> ArrayLike | tuple[ArrayLike, ArrayLike]:

For example FloatingArray and BooleanArray require an ndarray not an ExtensionArray.

@twoertwein
Copy link
Member Author

doc-test failure seems to be unrelated but mypy&pyright passed :)

@simonjayhawkins

Copy link
Member

@simonjayhawkins simonjayhawkins left a comment

Choose a reason for hiding this comment

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

Thanks @twoertwein lgtm pending green ci.

@simonjayhawkins
Copy link
Member

@twoertwein planning to restart the ci here once #47015 is green and merged, so nothing more to do here as far as i'm concerned.

@simonjayhawkins simonjayhawkins merged commit 9222cb0 into pandas-dev:main May 15, 2022
@twoertwein twoertwein deleted the mypy branch May 26, 2022 01:58
yehoshuadimarsky pushed a commit to yehoshuadimarsky/pandas that referenced this pull request Jul 13, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Typing type annotations, mypy/pyright type checking
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants