Skip to content

Is there some obvious reason why api.typing is not present in the api interface stub? #1221

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

Closed
Dr-Irv opened this issue May 19, 2025 · 6 comments · Fixed by #1223
Closed
Assignees

Comments

@Dr-Irv
Copy link
Collaborator

Dr-Irv commented May 19, 2025

Is there some obvious reason why api.typing is not present in the api interface stub?
https://github.com/pandas-dev/pandas-stubs/blob/86073150f5e1b607c37a536ec99204ed21bad0cb/pandas-stubs/api/__init__.pyi
Was it missed in #1058?
I see types there but not typing, both are public interfaces according to docs.

I just wanted to use pandas.api.typing.NaTType in my code but my static type checker complained that typing is not part of pandas.api.

Originally posted by @Molkree in #1220

@Dr-Irv
Copy link
Collaborator Author

Dr-Irv commented May 19, 2025

@loicdiridollou need to add typing to pandas-stubs\api\__init__.pyi

@loicdiridollou
Copy link
Member

loicdiridollou commented May 19, 2025 via email

@Molkree
Copy link

Molkree commented May 20, 2025

I just stumbled on a similar problem with pandas.io module. __init__.pyi is completely empty here, so none of the underlying modules are visible to type checkers.
This goes on deeper as well, because pandas.io.formats.__init__.pyi is also empty
I was interested in pandas.io.formats.style.Styler.
Would you like me to file a separate issue(s)?

@loicdiridollou
Copy link
Member

loicdiridollou commented May 20, 2025

Good catch, the question to @Dr-Irv is should we realign all those __init__ and when we are in the case like pandas.io where all imports are under the TYPE_CHECKING if statement, does it mean that we need it in pandas-stubs?
I will take care of it if we agree on direction.

@Molkree
Copy link

Molkree commented May 20, 2025

when we are in the case like pandas.io where all imports are under the TYPE_CHECKING if statement, does it mean that we need it in pandas-stubs

Hm, I didn't notice this at first, do you know the rationale behind this?

I wanted to write something like this:

def foo(df: pd.DataFrame) -> pandas.io.formats.style.Styler:

but it fails with AttributeError: module 'pandas.io.formats' has no attribute 'style' so I have to wrap import and enable future annotations:

from __future__ import annotations

from typing import TYPE_CHECKING

if TYPE_CHECKING:
    from pandas.io.formats.style import Styler

def colour_platform_column(df: pd.DataFrame) -> Styler:

This is a bit cumbersome, maybe I'm missing something here? Docs don't mention the fact that you have to do such hacks.

@Dr-Irv
Copy link
Collaborator Author

Dr-Irv commented May 20, 2025

Good catch, the question to @Dr-Irv is should we realign all those __init__ and when we are in the case like pandas.io where all imports are under the TYPE_CHECKING if statement, does it mean that we need it in pandas-stubs?

My take on this is that if the pandas source has an __init__.py in some sub-package that imports other sub-packages/modules, then the stubs should do the same in the corresponding __init__.pyi, but without the if TYPE_CHECKING line.

I think they would all need to be of the from from sub_package import xyz as xyz for it to work in the stubs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants