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

Make typing-extensions optional #5624

Merged
merged 3 commits into from
Jul 22, 2021

Conversation

shoyer
Copy link
Member

@shoyer shoyer commented Jul 20, 2021

Type checking may be a little worse if typing-extensions are not installed, but I don't think it's worth the trouble of adding another hard dependency just for one use for TypeGuard.

Note: sadly this doesn't work yet. Mypy (and pylance) don't like the type alias defined with try/except. Any ideas? In the worst case, we could revert the TypeGuard entirely, but that would be a shame...

Fixes pydataGH-5495

Type checking may be a little worse if typing-extensions are not
installed, but I don't think it's worth the trouble of adding another
hard dependency just for one use for TypeGuard.
@github-actions
Copy link
Contributor

github-actions bot commented Jul 20, 2021

Unit Test Results

         6 files  ±0           6 suites  ±0   56m 9s ⏱️ ±0s
16 204 tests ±0  14 469 ✔️ ±0  1 735 💤 ±0  0 ❌ ±0 
90 420 runs  ±0  82 245 ✔️ ±0  8 175 💤 ±0  0 ❌ ±0 

Results for commit deaca14. ± Comparison against base commit deaca14.

♻️ This comment has been updated with latest results.

@max-sixty
Copy link
Collaborator

max-sixty commented Jul 20, 2021

Sorry to be late to the party here — I've been less in the flow of xarray for the past week.

How strongly do you feel about not having this as a dependency @shoyer ? I hadn't thought of it as controversial given it's a "official" python package, but without much confidence so happy to defer.

A few options:

  • Revert TypeGuard (when would we add it back — when typing-extensions has more track record? When 3.10 is our minimum version?)
  • Have typing-extensions be optional and add # type: ignore comments. Not sure how many would be required though.
  • Have typing-extensions be optional but raise an error on if TYPE_CHECKING if it's not installed — i.e. type checking will fail without it, but running xarray would be fine.
  • Make it a required dependency

If we can get a good error message, I would vote for requiring it for type-checking. If it silently swallows the error and doesn't type check, then I'd probably vote for reverting TypeGuard or making typing-extensions required.

@shoyer
Copy link
Member Author

shoyer commented Jul 20, 2021

I think requiring typing-extensions for type checks to pass (especially internal to Xarray) would be totally reasonable. In general, it's not a big deal to install a dependency like this with pip/conda but every new dependency can be a marginal annoyance for those who maintain a custom build system of some sort.

To be honest, this only came up because I asked a co-worker to try using the development version of Xarray. Then they discovered that Apache-Beam currently puts on upper bound on the supported typing-extensions version (<= 3.7), so it would not be possible to install the latest versions of both Beam and Xarray :(. Beam should definitely relax this requirement, too, but it seemed like we do could the same, too.

@max-sixty
Copy link
Collaborator

It's not pretty, but this seems to work, on top of the most recent commit 2191fbc:

diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml
index 232dbec3..3b490dcc 100644
--- a/.pre-commit-config.yaml
+++ b/.pre-commit-config.yaml
@@ -43,6 +43,7 @@ repos:
             types-pytz,
             # Dependencies that are typed
             numpy,
+            typing-extensions==3.10.0.0,
           ]
   # run this occasionally, ref discussion https://github.com/pydata/xarray/pull/3194
   # - repo: https://github.com/asottile/pyupgrade
diff --git a/xarray/core/utils.py b/xarray/core/utils.py
index 1f2dfb5c..a139d2ef 100644
--- a/xarray/core/utils.py
+++ b/xarray/core/utils.py
@@ -10,6 +10,7 @@
 import warnings
 from enum import Enum
 from typing import (
+    TYPE_CHECKING,
     Any,
     Callable,
     Collection,
@@ -32,7 +33,6 @@
 import numpy as np
 import pandas as pd
 
-
 K = TypeVar("K")
 V = TypeVar("V")
 T = TypeVar("T")
@@ -307,19 +307,29 @@ def _is_scalar(value, include_0d):
     )
 
 
+# See GH5624, this is a convoluted way to allow type-checking to use `TypeGuard` without
+# requiring typing_extensions as a required dependency to _run_ the code (it is required
+# to type-check).
 try:
     if sys.version_info >= (3, 10):
         from typing import TypeGuard
     else:
         from typing_extensions import TypeGuard
 except ImportError:
-    def is_scalar(value: Any, include_0d: bool = True) -> bool:
-        """Whether to treat a value as a scalar.
+    if TYPE_CHECKING:
+        raise
+    else:
+
+        def is_scalar(value: Any, include_0d: bool = True) -> bool:
+            """Whether to treat a value as a scalar.
+
+            Any non-iterable, string, or 0-D array
+            """
+            return _is_scalar(value, include_0d)
+
 
-        Any non-iterable, string, or 0-D array
-        """
-        return _is_scalar(value, include_0d)
 else:
+
     def is_scalar(value: Any, include_0d: bool = True) -> TypeGuard[Hashable]:
         """Whether to treat a value as a scalar.
 

It works with or without the edit to .pre-commit-config.yaml

@shoyer want me to add it on and merge?

@shoyer shoyer marked this pull request as ready for review July 22, 2021 22:31
@shoyer
Copy link
Member Author

shoyer commented Jul 22, 2021

@max-sixty I just pushed a commit with that change... let's verify that it works in CI and then ship it!

@dcherian
Copy link
Contributor

nice work! shipping...

@dcherian dcherian merged commit deaca14 into pydata:main Jul 22, 2021
@shoyer shoyer deleted the typing-extensions-optional branch July 22, 2021 23:02
st-bender added a commit to st-bender/xarray that referenced this pull request Aug 10, 2021
* main: (31 commits)
  Refactor index vs. coordinate variable(s) (pydata#5636)
  pre-commit: autoupdate hook versions (pydata#5685)
  Flexible Indexes: Avoid len(index) in map_blocks (pydata#5670)
  Speed up _mapping_repr (pydata#5661)
  update the link to `scipy`'s intersphinx file (pydata#5665)
  Bump styfle/cancel-workflow-action from 0.9.0 to 0.9.1 (pydata#5663)
  pre-commit: autoupdate hook versions (pydata#5660)
  fix the binder environment (pydata#5650)
  Update api.rst (pydata#5639)
  Kwargs to rasterio open (pydata#5609)
  Bump codecov/codecov-action from 1 to 2.0.2 (pydata#5633)
  new blank whats-new for v0.19.1
  v0.19.0 release notes (pydata#5632)
  remove deprecations scheduled for 0.19 (pydata#5630)
  Make typing-extensions optional (pydata#5624)
  Plots get labels from pint arrays (pydata#5561)
  Add to_numpy() and as_numpy() methods (pydata#5568)
  pin fsspec (pydata#5627)
  pre-commit: autoupdate hook versions (pydata#5617)
  Add dataarray scatter with 3d support (pydata#4909)
  ...
dcherian added a commit to kmsquire/xarray that referenced this pull request Aug 11, 2021
* upstream/main: (31 commits)
  Refactor index vs. coordinate variable(s) (pydata#5636)
  pre-commit: autoupdate hook versions (pydata#5685)
  Flexible Indexes: Avoid len(index) in map_blocks (pydata#5670)
  Speed up _mapping_repr (pydata#5661)
  update the link to `scipy`'s intersphinx file (pydata#5665)
  Bump styfle/cancel-workflow-action from 0.9.0 to 0.9.1 (pydata#5663)
  pre-commit: autoupdate hook versions (pydata#5660)
  fix the binder environment (pydata#5650)
  Update api.rst (pydata#5639)
  Kwargs to rasterio open (pydata#5609)
  Bump codecov/codecov-action from 1 to 2.0.2 (pydata#5633)
  new blank whats-new for v0.19.1
  v0.19.0 release notes (pydata#5632)
  remove deprecations scheduled for 0.19 (pydata#5630)
  Make typing-extensions optional (pydata#5624)
  Plots get labels from pint arrays (pydata#5561)
  Add to_numpy() and as_numpy() methods (pydata#5568)
  pin fsspec (pydata#5627)
  pre-commit: autoupdate hook versions (pydata#5617)
  Add dataarray scatter with 3d support (pydata#4909)
  ...
dcherian added a commit to dcherian/xarray that referenced this pull request Aug 13, 2021
* upstream/main: (34 commits)
  Use same bool validator as other inputs (pydata#5703)
  conditionally disable bottleneck (pydata#5560)
  Refactor index vs. coordinate variable(s) (pydata#5636)
  pre-commit: autoupdate hook versions (pydata#5685)
  Flexible Indexes: Avoid len(index) in map_blocks (pydata#5670)
  Speed up _mapping_repr (pydata#5661)
  update the link to `scipy`'s intersphinx file (pydata#5665)
  Bump styfle/cancel-workflow-action from 0.9.0 to 0.9.1 (pydata#5663)
  pre-commit: autoupdate hook versions (pydata#5660)
  fix the binder environment (pydata#5650)
  Update api.rst (pydata#5639)
  Kwargs to rasterio open (pydata#5609)
  Bump codecov/codecov-action from 1 to 2.0.2 (pydata#5633)
  new blank whats-new for v0.19.1
  v0.19.0 release notes (pydata#5632)
  remove deprecations scheduled for 0.19 (pydata#5630)
  Make typing-extensions optional (pydata#5624)
  Plots get labels from pint arrays (pydata#5561)
  Add to_numpy() and as_numpy() methods (pydata#5568)
  pin fsspec (pydata#5627)
  ...
dcherian added a commit to dcherian/xarray that referenced this pull request Aug 13, 2021
* upstream/main: (307 commits)
  Use same bool validator as other inputs (pydata#5703)
  conditionally disable bottleneck (pydata#5560)
  Refactor index vs. coordinate variable(s) (pydata#5636)
  pre-commit: autoupdate hook versions (pydata#5685)
  Flexible Indexes: Avoid len(index) in map_blocks (pydata#5670)
  Speed up _mapping_repr (pydata#5661)
  update the link to `scipy`'s intersphinx file (pydata#5665)
  Bump styfle/cancel-workflow-action from 0.9.0 to 0.9.1 (pydata#5663)
  pre-commit: autoupdate hook versions (pydata#5660)
  fix the binder environment (pydata#5650)
  Update api.rst (pydata#5639)
  Kwargs to rasterio open (pydata#5609)
  Bump codecov/codecov-action from 1 to 2.0.2 (pydata#5633)
  new blank whats-new for v0.19.1
  v0.19.0 release notes (pydata#5632)
  remove deprecations scheduled for 0.19 (pydata#5630)
  Make typing-extensions optional (pydata#5624)
  Plots get labels from pint arrays (pydata#5561)
  Add to_numpy() and as_numpy() methods (pydata#5568)
  pin fsspec (pydata#5627)
  ...
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.

Add typing-extensions to the list of dependencies?
3 participants