Skip to content

Support Ellipsis as a type per se #684

Description

@max-sixty

Referenced from python/mypy#7818

We use an Ellipsis as an easy-to-understand, easy-to-import sentinel value to mean "everything else".

But it's not possible to use fully with type checking, since mypy doesn't exclude it from a type when it's been excluded with an if, as it does for None, or Enums per #240). I've moved the issue here since it's apparently a python typing issue rather than a mypy implementation.

I've included below a MCVE of the behavior below, and here's an example of how we use it, given @gvanrossum has already asked whether it's important to use an Ellipsis:

Details

For example, to transpose an array, these are equivalent:

In [1]: importxarrayasxrIn [2]: ds=xr.tutorial.scatter_example_dataset()
In [3]: dsOut[3]:
<xarray.Dataset>Dimensions: (w: 4, x: 3, y: 11, z: 4)
Coordinates:
*x (x) int64012*y (y) float640.00.10.20.30.40.50.60.70.80.91.0*z (z) int640123*w (w) <U5'one''two''three''five'Datavariables:
A (x, y, z, w) float640.020740.04807-0.1059 ... -0.1809-0.04862B (x, y, z, w) float640.00.00.00.0 ... 1.4061.4141.3681.408In [4]: ds.transpose('w','x','y','z')
Out[4]:
<xarray.Dataset>Dimensions: (w: 4, x: 3, y: 11, z: 4)
Coordinates:
*x (x) int64012*y (y) float640.00.10.20.30.40.50.60.70.80.91.0*z (z) int640123*w (w) <U5'one''two''three''five'Datavariables:
A (w, x, y, z) float640.020740.020740.02074 ... -0.03076-0.04862B (w, x, y, z) float640.00.0020740.004147 ... 1.4031.4051.408In [5]: ds.transpose('w',...) # use an Ellipsis to indicate 'all other dimensions'Out[5]:
<xarray.Dataset>Dimensions: (w: 4, x: 3, y: 11, z: 4)
Coordinates:
*x (x) int64012*y (y) float640.00.10.20.30.40.50.60.70.80.91.0*z (z) int640123*w (w) <U5'one''two''three''five'Datavariables:
A (w, x, y, z) float640.020740.020740.02074 ... -0.03076-0.04862B (w, x, y, z) float640.00.0020740.004147 ... 1.4031.4051.408

Code example with existing mypy:

importbuiltinsfromtypingimportList, Uniondeffun(x: Union[builtins.ellipsis, List], y: List):
ifxisnotEllipsis:
y=x# error: Incompatible types in assignment (expression has type "Union[ellipsis, List[Any]]", variable has type "List[Any]")returny# another attempt:deffun2(x: Union[Ellipsis, List], y: List): # error: Variable "builtins.Ellipsis" is not valid as a typeifxisnotEllipsis:
y=xreturny

Thank you!

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    topic: featureDiscussions about new features for Python's type annotations

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions