Skip to content

TYP: Return annotations for io/{formats,json} #47516

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 4 commits into from
Jun 27, 2022
Merged

TYP: Return annotations for io/{formats,json} #47516

merged 4 commits into from
Jun 27, 2022

Conversation

twoertwein
Copy link
Member

No description provided.

@datapythonista datapythonista added the Typing type annotations, mypy/pyright type checking label Jun 27, 2022
@datapythonista
Copy link
Member

Looks good. It'd be good to have an issue about this for more context, and to somehow group the different issues in this effort. Also to add to the CI the validation you're using to find those. Does this make sense to you?

@twoertwein
Copy link
Member Author

twoertwein commented Jun 27, 2022

It'd be good to have an issue about this for more context

I will create an issue later today about it (many of these annotations could be easy first contributions). Inspired by @MarcoGorelli's #47497 I'm using pyright to find missing return annotations that it can infer. While this doesn't help pyright much (it infers them already), it helps mypy, and in some cases it also helps pyright (when pyright infers a Union (might be an overload) or infers Any/Unknown (might be able to find a narrower type).

Also to add to the CI the validation you're using to find those.

At the moment I'm just doing:

pyright --createstub pandas
grep -R "# -> " typings/pandas/

and then see which cases I feel comfortable annotating.

In terms of integrating something like that into the CI:

@@ -1036,7 +1036,8 @@ def _repr_fits_horizontal_(self, ignore_width: bool = False) -> bool:
value = buf.getvalue()
repr_width = max(len(line) for line in value.split("\n"))

return repr_width < width
# error: Unsupported operand types for < ("int" and "None")
Copy link
Member

Choose a reason for hiding this comment

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

Probably to be safe, this typing infraction should be address with an if width is not None or similar.

Copy link
Member Author

Choose a reason for hiding this comment

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

As far as I understand (not sure), width can only be None, when pandas is running in a non-interactive way (and I think it also requires that the user put None to set_options() for the default width). The function _repr_fits_horizontal_ exits early if console.in_interactive_session() is true which should ensure that the above line will never be reached with None. Probably makes sense to or this if condition with width is None:

if ignore_width or not console.in_interactive_session():

Copy link
Member

Choose a reason for hiding this comment

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

Thanks, makes sense to me

@mroeschke mroeschke added this to the 1.5 milestone Jun 27, 2022
@MarcoGorelli
Copy link
Member

Inspired by @MarcoGorelli's #47497 I'm using pyright to find missing return annotations that it can infer.

Nice! Does this obliviate the need for #47497 and associated tool, or does pyright not find some obvious -> None statements?

@twoertwein
Copy link
Member Author

Does this obliviate the need for #47497 and associated tool, or does pyright not find some obvious -> None statements?

Pyright is really good at inferring types :) but --createstubs does not care about private functions. Your hook is not only helpful to cover private functions but it is also helpful to enforce obvious -> None annotations. I believe that even #39813 (--verifytypes) does not enforce unannotated private functions.

@mroeschke mroeschke merged commit 734db4f into pandas-dev:main Jun 27, 2022
@mroeschke
Copy link
Member

Thanks @twoertwein As @datapythonista mentioned, would be good to have a general issue about this pyright specific check.

@twoertwein
Copy link
Member Author

Opened #47521

yehoshuadimarsky pushed a commit to yehoshuadimarsky/pandas that referenced this pull request Jul 13, 2022
* TYP: Return annotations for io/{formats,json}

* flake8

* explicitly check whether width is None
@twoertwein twoertwein deleted the formats_json branch September 21, 2022 15:28
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.

4 participants