Skip to content
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

TYP: Use Self instead of class-bound TypeVar in pyi files (remove Y019) #51480

Merged
merged 2 commits into from
Mar 13, 2023

Conversation

topper-123
Copy link
Contributor

Progress towards #51233. Next up will be to use Self in the pandas code base.

xref #51309.

CC: @twoertwein

@@ -117,3 +117,4 @@ dependencies:

- pip:
- sphinx-toggleprompt
- typing_extensions; python_version<"3.11"
Copy link
Member

Choose a reason for hiding this comment

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

I think we don't need to add it, as it is only used during type checking (and typing_extensions is, I think, a dependency of mypy).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, typing_extension is a dependency of mypy, I just checked for that. We do not want to be explicit, now that we're importing fro typing_extensions?

Copy link
Member

Choose a reason for hiding this comment

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

I think even the stubs packages from typeshed (for example https://pypi.org/project/types-psutil/) do not have typing_extensions as a dependency (neither does pandas-stubs).

If it is a new dependency, it probably should be added to the whatsnew (there might be other places where it needs to be added as well).

Copy link
Member

Choose a reason for hiding this comment

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

we have had discussions about this before and the outcome was that we do not use typing_extensions.

IIUC we don't need it with the latest mypy (1.0.0) when checking with python 3.11

@@ -86,4 +86,5 @@ feedparser
pyyaml
requests
sphinx-toggleprompt
typing_extensions; python_version<"3.11"
Copy link
Member

Choose a reason for hiding this comment

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

I don't think we need this

Copy link
Member

@twoertwein twoertwein left a comment

Choose a reason for hiding this comment

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

The Self changes look good to me. Whether typing_extensions should be a new dependency or not, should be judged by someone else.

@topper-123
Copy link
Contributor Author

The Self changes look good to me. Whether typing_extensions should be a new dependency or not, should be judged by someone else.

+1 from me, I'm also not sure what is normal.

I looked at typeshed, and the project itself has typing_extensions as a test requirement, se the requirements-tests.txt. OTOH, individual type stubs don't have it as a requirement AFAIKS, e.g. types-protobuf uses typing_extensions but has no dependencies...

@@ -117,3 +117,4 @@ dependencies:

- pip:
- sphinx-toggleprompt
- typing_extensions; python_version<"3.11"
Copy link
Member

Choose a reason for hiding this comment

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

we have had discussions about this before and the outcome was that we do not use typing_extensions.

IIUC we don't need it with the latest mypy (1.0.0) when checking with python 3.11

if sys.version_info >= (3, 11):
from typing import Self
else:
from typing_extensions import Self # pyright: reportUnusedImport = false
Copy link
Member

Choose a reason for hiding this comment

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

I guess you could make Self equivalent to Any here. IIRC we did something similar before to enable the latest typing features

Copy link
Member

Choose a reason for hiding this comment

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

We type check using a single version of Python. There are capabilities in mypy to set the Python version to check against regardless of the version used. We don't do that.

So i'm not sure when this import is actually needed.

@topper-123
Copy link
Contributor Author

topper-123 commented Feb 22, 2023

This PR was made as a bit of a follow-up to #51309, where @jbrockmendel had a suggestion to use TypeGuard for a more precise typing experience. TypeGuard is only available from python 3.10, so he added type_extensions to _typing module. #51309 hasn't actually been pulled in yet, but I didn't sense that using TypeGuard/typing_extensions as controversial.

I was not not aware of the decision to not use typing_extensions (which is totally on me, I had a break from pandas so have not been aware of some thing happening in the community). Would it be possible to reconsider the decision to not use typing_extensions?

IMO typing_extensions will be very useful to use in Pandas, because:

  • typings_extensions is just a backport of functionality from newer python version to allow mypy to type check using older python versions,
  • it is a dependency of mypy itself already (so is already installed, if mypy is installed), is developed by Guido & company (so has good support), so it doesn't seem controversial to me to use it, and
  • Pandas is a very large code base, so more/better typing is IMO always helpful to our project.
    ?

@simonjayhawkins
Copy link
Member

simonjayhawkins commented Feb 22, 2023

Would it be possible to reconsider the decision to not use typing_extensions?

When we had the discussion before I was arguing for including it. Now that we have pandas-stubs and typing in the codebase could be considered as a glorified linter there are probably less arguments for. Also, it has not always ended up been a blocker for using features supported in the latest mypy by implementing some trivial workarounds in some cases.

cc @TomAugspurger @jorisvandenbossche @jreback @WillAyd from previous discussions.

@simonjayhawkins
Copy link
Member

The previous decision was for a run-time dependency.

As the import is inside a TYPE_CHECKING block this implementation only makes it a dev dependency (just like the use of npt)?

@topper-123
Copy link
Contributor Author

The previous decision was for a run-time dependency.

As the import is inside a TYPE_CHECKING block this implementation only makes it a dev dependency (just like the use of npt)?

Yes, this will only be active when type checking and it will not be a run time requirement.

@simonjayhawkins
Copy link
Member

scanning back over the historic typing_extensions discussions, the issue with having it as a dev-only dependency was the the need to wrap in TYPE_CHECKING and was also before typing_extensions was an required dependency of mypy in #27646

@simonjayhawkins
Copy link
Member

simonjayhawkins commented Feb 24, 2023

IMO typing_extensions will be very useful to use in Pandas, because:

  • typings_extensions is just a backport of functionality from newer python version to allow mypy to type check using older python versions,
  • it is a dependency of mypy itself already (so is already installed, if mypy is installed), is developed by Guido & company (so has good support), so it doesn't seem controversial to me to use it, and
  • Pandas is a very large code base, so more/better typing is IMO always helpful to our project.

adding to this list...

  • pandas-stubs is not restricted by the minimum Python version support. It is desirable to keep the pandas-stubs and pandas type annotations in sync where possible/appropriate.

@jorisvandenbossche
Copy link
Member

From looking at the linked issues, #37119 might be the most recent-ish complete discussion issue about this (+ #34869 as a precursor of it). We can maybe have the actual discussion about adding as dependency or not there?

From skimming that issue, what is still unclear to me is what the exact consequences are? Is this only a dev dependency (only contributors have to have it installed in their environment) and not a required runtime dependency? Or would it only be useful if it's actually a runtime dependency?
(the issue has various conflicting messages about this, some say it's possible, others not, or that it limits its usefulness, ..)
While from this PR it seems it is only a dev dependency, since it's not added to pyproject.toml.

Can someone summarize this over at #37119?

@topper-123 topper-123 mentioned this pull request Mar 1, 2023
@topper-123
Copy link
Contributor Author

topper-123 commented Mar 1, 2023

I've answered in #37119 regarding my experience using typing_extensions inside the if TYPE_CHECKING block.

Wrt. whether typing_extensions is/should be a run time dependency or not, I've made if NOT a run time dependency in this PR (i.e. only imported when TYPE_CHECKING is True).

If there's an argument for making this available at run time i'm open to arguments, but I just didn't see the need myself. and it makes sense to always limit run time dependencies when possible.

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 @topper-123

doesn't appear there are any objections from anyone this time round.

if part of the argument to use typing_extensions is that it is a required dependency of mypy, do we need to explicitly include it in environment.yml

requires merge main

lgtm pending green

@topper-123
Copy link
Contributor Author

I've merged main.

@WillAyd WillAyd merged commit b07a388 into pandas-dev:main Mar 13, 2023
@WillAyd
Copy link
Member

WillAyd commented Mar 13, 2023

Wow great work @topper-123

@jbrockmendel
Copy link
Member

nice!

@jorisvandenbossche
Copy link
Member

jorisvandenbossche commented Mar 13, 2023

Can someone update #37119 with the outcome? (and probably close it)

@topper-123 topper-123 deleted the remove_flake8_Y019_ignore branch March 13, 2023 19:36
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.

6 participants