Skip to content

TYP: future annotations #151

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 14 commits into from
Jul 21, 2022
Merged

TYP: future annotations #151

merged 14 commits into from
Jul 21, 2022

Conversation

twoertwein
Copy link
Member

@twoertwein twoertwein marked this pull request as ready for review July 18, 2022 22:39
@twoertwein
Copy link
Member Author

I will try to separate it in two commits: automatic (sed+pre-commit) and then manual changes to make it easier to review

@Dr-Irv
Copy link
Collaborator

Dr-Irv commented Jul 18, 2022

I will try to separate it in two commits: automatic (sed+pre-commit) and then manual changes to make it easier to review

Thanks. I'll review when you do that.

Did you just do sed to convert everything? Or use some other tool?

@twoertwein
Copy link
Member Author

The new PR will:

  • add future to all files using sed and run pre-commit
  • manually remove unused imports (and workaround mypy bugs: def type() causes issues)

This will not satisfy flake8-pyi as pre-commit will not rewrite Union/... outside of annotations (TypeAliases), I had many ugly sed's in this PR but I think I will skip this and try to address that later.

@twoertwein
Copy link
Member Author

twoertwein commented Jul 19, 2022

  • first commit: sed -i '1s;^;from __future__ import annotations\n;' + pre-commit + isort + black
  • second commit: removing unused imports and getting mypy/pyright to pass
  • thrid commit: manually chaning offending (flake8-pyi Y022) Union/List/Dict/Tuple/Set cases
  • fourth commit: remove unused flake8 ignore comments

pyi files do not need the future import but without it pre-commit while not re-write the annotations.

Copy link
Contributor

@bashtage bashtage left a comment

Choose a reason for hiding this comment

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

2, 3, and 4 all look good. 1 is harder to check, but probably can be trusted.

@@ -19,7 +19,7 @@ class BooleanDtype(ExtensionDtype):
@property
def kind(self) -> str: ...
@classmethod
def construct_array_type(cls) -> type[BooleanArray]: ...
def construct_array_type(cls) -> _type_BooleanArray: ...
Copy link
Contributor

Choose a reason for hiding this comment

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

Same

Copy link
Collaborator

@Dr-Irv Dr-Irv left a comment

Choose a reason for hiding this comment

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

I somehow stared at all of these. Not sure I caught everything. General comments:

  1. In pandas we have type_t in pandas/_typing.py, so we can then do things like type_t[Dtype], etc. So I think we should use that convention here rather than type[Dtype]
  2. There are places where a parameter has one line of its type, but parentheses got inserted and are unnecessary. I caught many of them, but may have missed a few
  3. There are places where we have None | str | int (or similar pattern), where we should use the convention of str | int | None

There was one place where a type was dropped. Otherwise OK.

@twoertwein
Copy link
Member Author

But neither Dict nor List is at those lines? (locally poe test_all works for me - on 3.10)

pandas-stubs/core/frame.pyi:194: error: Name "Dict" is not defined [name-defined]
pandas-stubs/core/frame.pyi:194: note: Did you forget to import it from "typing"? (Suggestion: "from typing import Dict")
pandas-stubs/core/frame.pyi:197: error: Name "List" is not defined [name-defined]
pandas-stubs/core/frame.pyi:197: note: Did you forget to import it from "typing"? (Suggestion: "from typing import List")

@twoertwein
Copy link
Member Author

Is there a bug in the git checkout? The CI has:

$ /usr/bin/git log -1 --format='%H'
'4a93f11ab848da036b9afe2c33b46839307d85e3'

But locally I have:

$ git log -1 --format='%H'
0433b15f2cf22bc85c5c1920c714212fd3daa919

I'll squash the commits when it started falling (maybe the commit message triggers some weird bug - probably something different is wrong but I cannot see it)

@twoertwein twoertwein marked this pull request as draft July 20, 2022 12:24
@twoertwein
Copy link
Member Author

Since I cannot reproduce the error locally, I will revert until the latest working commit and then add slowly make changes to see what is going on.

This reverts commit 188c4d9.
@twoertwein twoertwein marked this pull request as ready for review July 20, 2022 14:36
@Dr-Irv
Copy link
Collaborator

Dr-Irv commented Jul 20, 2022

You can retrigger the CI from github. I had a failure due to network connectivity, and just went it and told it to rerun the jobs.

@twoertwein
Copy link
Member Author

You can retrigger the CI from github. I had a failure due to network connectivity, and just went it and told it to rerun the jobs.

Thanks! It seems that github requires that the branch has no conflict with main (that might also explain why Dict and List appeared out of nowhere):
https://github.community/t/run-actions-on-pull-requests-with-merge-conflicts/17104/26

I resolved all conflicts.

@Dr-Irv
Copy link
Collaborator

Dr-Irv commented Jul 20, 2022

Let me know when I should look at this again.

This reverts commit edb4539.
@twoertwein twoertwein marked this pull request as draft July 20, 2022 15:04
@twoertwein twoertwein marked this pull request as ready for review July 20, 2022 15:13
Copy link
Collaborator

@Dr-Irv Dr-Irv left a comment

Choose a reason for hiding this comment

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

So I think there is a better workaround for the type issue, where we define type_t once, export it from _typing.pyi and then import type_t instead of always creating _type

Otherwise, a few small things.

Copy link
Collaborator

@Dr-Irv Dr-Irv left a comment

Choose a reason for hiding this comment

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

I think we should keep using _str in frame.pyi so that the methods that overlap with series.pyi have similar signatures, since we have to use _str in series.pyi

Copy link
Collaborator

@Dr-Irv Dr-Irv 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 . I now have to get used to not using Union, Dict, List, etc. !

@Dr-Irv Dr-Irv merged commit 9383884 into pandas-dev:main Jul 21, 2022
@Dr-Irv Dr-Irv mentioned this pull request Jul 21, 2022
2 tasks
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 this pull request may close these issues.

Use PEP 604 style annotations
3 participants