Skip to content

Blacken the doctest code in docstrings #3857

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 3 commits into from
Mar 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions xarray/backends/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -1196,8 +1196,8 @@ def save_mfdataset(

Save a dataset into one netCDF per year of data:

>>> years, datasets = zip(*ds.groupby('time.year'))
>>> paths = ['%s.nc' % y for y in years]
>>> years, datasets = zip(*ds.groupby("time.year"))
>>> paths = ["%s.nc" % y for y in years]
>>> xr.save_mfdataset(datasets, paths)
"""
if mode == "w" and len(set(paths)) < len(paths):
Expand Down
2 changes: 1 addition & 1 deletion xarray/coding/cftime_offsets.py
Original file line number Diff line number Diff line change
Expand Up @@ -938,7 +938,7 @@ def cftime_range(
This function returns a ``CFTimeIndex``, populated with ``cftime.datetime``
objects associated with the specified calendar type, e.g.

>>> xr.cftime_range(start='2000', periods=6, freq='2MS', calendar='noleap')
>>> xr.cftime_range(start="2000", periods=6, freq="2MS", calendar="noleap")
CFTimeIndex([2000-01-01 00:00:00, 2000-03-01 00:00:00, 2000-05-01 00:00:00,
2000-07-01 00:00:00, 2000-09-01 00:00:00, 2000-11-01 00:00:00],
dtype='object')
Expand Down
46 changes: 25 additions & 21 deletions xarray/coding/cftimeindex.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,29 +268,32 @@ def _partial_date_slice(self, resolution, parsed):
>>> from cftime import DatetimeNoLeap
>>> import pandas as pd
>>> import xarray as xr
>>> da = xr.DataArray([1, 2],
coords=[[DatetimeNoLeap(2001, 1, 1),
DatetimeNoLeap(2001, 2, 1)]],
dims=['time'])
>>> da.sel(time='2001-01-01')
>>> da = xr.DataArray(
... [1, 2],
... coords=[[DatetimeNoLeap(2001, 1, 1), DatetimeNoLeap(2001, 2, 1)]],
... dims=["time"],
... )
>>> da.sel(time="2001-01-01")
<xarray.DataArray (time: 1)>
array([1])
Coordinates:
* time (time) object 2001-01-01 00:00:00
>>> da = xr.DataArray([1, 2],
coords=[[pd.Timestamp(2001, 1, 1),
pd.Timestamp(2001, 2, 1)]],
dims=['time'])
>>> da.sel(time='2001-01-01')
>>> da = xr.DataArray(
Copy link
Collaborator

Choose a reason for hiding this comment

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

I had thought the standard was a blank line after each result; is that a general standard or was I holding that incorrectly?
(no need to do it this round though)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I had the same impression, but the docs say otherwise:

Expected output cannot contain an all-whitespace line, since such a line is taken to signal the end of expected output. If expected output does contain a blank line, put <BLANKLINE> in your doctest example each place a blank line is expected.

I think that means that we only need the newline if we want to continue with normal text.

Copy link
Collaborator

Choose a reason for hiding this comment

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

OK. IIUC we can still have blank lines, but it becomes a preference rather than enforced.
FWIW I do find they make it much easier to read the examples!

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

sounds good. Let's do that in a new PR, though.

... [1, 2],
... coords=[[pd.Timestamp(2001, 1, 1), pd.Timestamp(2001, 2, 1)]],
... dims=["time"],
... )
>>> da.sel(time="2001-01-01")
<xarray.DataArray ()>
array(1)
Coordinates:
time datetime64[ns] 2001-01-01
>>> da = xr.DataArray([1, 2],
coords=[[pd.Timestamp(2001, 1, 1, 1),
pd.Timestamp(2001, 2, 1)]],
dims=['time'])
>>> da.sel(time='2001-01-01')
>>> da = xr.DataArray(
... [1, 2],
... coords=[[pd.Timestamp(2001, 1, 1, 1), pd.Timestamp(2001, 2, 1)]],
... dims=["time"],
... )
>>> da.sel(time="2001-01-01")
<xarray.DataArray (time: 1)>
array([1])
Coordinates:
Expand Down Expand Up @@ -396,10 +399,10 @@ def shift(self, n, freq):

Examples
--------
>>> index = xr.cftime_range('2000', periods=1, freq='M')
>>> index = xr.cftime_range("2000", periods=1, freq="M")
>>> index
CFTimeIndex([2000-01-31 00:00:00], dtype='object')
>>> index.shift(1, 'M')
>>> index.shift(1, "M")
CFTimeIndex([2000-02-29 00:00:00], dtype='object')
"""
from .cftime_offsets import to_offset
Expand Down Expand Up @@ -479,7 +482,7 @@ def to_datetimeindex(self, unsafe=False):
Examples
--------
>>> import xarray as xr
>>> times = xr.cftime_range('2000', periods=2, calendar='gregorian')
>>> times = xr.cftime_range("2000", periods=2, calendar="gregorian")
>>> times
CFTimeIndex([2000-01-01 00:00:00, 2000-01-02 00:00:00], dtype='object')
>>> times.to_datetimeindex()
Expand Down Expand Up @@ -518,9 +521,10 @@ def strftime(self, date_format):

Examples
--------
>>> rng = xr.cftime_range(start='2000', periods=5, freq='2MS',
... calendar='noleap')
>>> rng.strftime('%B %d, %Y, %r')
>>> rng = xr.cftime_range(
... start="2000", periods=5, freq="2MS", calendar="noleap"
... )
>>> rng.strftime("%B %d, %Y, %r")
Index(['January 01, 2000, 12:00:00 AM', 'March 01, 2000, 12:00:00 AM',
'May 01, 2000, 12:00:00 AM', 'July 01, 2000, 12:00:00 AM',
'September 01, 2000, 12:00:00 AM'],
Expand Down
2 changes: 1 addition & 1 deletion xarray/coding/strings.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ class StackedBytesArray(indexing.ExplicitlyIndexedNDArrayMixin):
"""Wrapper around array-like objects to create a new indexable object where
values, when accessed, are automatically stacked along the last dimension.

>>> StackedBytesArray(np.array(['a', 'b', 'c']))[:]
>>> StackedBytesArray(np.array(["a", "b", "c"]))[:]
array('abc',
dtype='|S3')
"""
Expand Down
4 changes: 2 additions & 2 deletions xarray/conventions.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class NativeEndiannessArray(indexing.ExplicitlyIndexedNDArrayMixin):
big endian) into native endianness, so they can be used with Cython
functions, such as those found in bottleneck and pandas.

>>> x = np.arange(5, dtype='>i2')
>>> x = np.arange(5, dtype=">i2")

>>> x.dtype
dtype('>i2')
Expand Down Expand Up @@ -50,7 +50,7 @@ class BoolTypeArray(indexing.ExplicitlyIndexedNDArrayMixin):
This is useful for decoding boolean arrays from integer typed netCDF
variables.

>>> x = np.array([1, 0, 1, 1, 0], dtype='i1')
>>> x = np.array([1, 0, 1, 1, 0], dtype="i1")

>>> x.dtype
dtype('>i2')
Expand Down
10 changes: 5 additions & 5 deletions xarray/core/accessor_dt.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,8 +250,8 @@ class DatetimeAccessor(Properties):
---------
>>> import xarray as xr
>>> import pandas as pd
>>> dates = pd.date_range(start='2000/01/01', freq='D', periods=10)
>>> ts = xr.DataArray(dates, dims=('time'))
>>> dates = pd.date_range(start="2000/01/01", freq="D", periods=10)
>>> ts = xr.DataArray(dates, dims=("time"))
>>> ts
<xarray.DataArray (time: 10)>
array(['2000-01-01T00:00:00.000000000', '2000-01-02T00:00:00.000000000',
Expand Down Expand Up @@ -296,8 +296,8 @@ def strftime(self, date_format):

Examples
--------
>>> rng = xr.Dataset({'time': datetime.datetime(2000, 1, 1)})
>>> rng['time'].dt.strftime('%B %d, %Y, %r')
>>> rng = xr.Dataset({"time": datetime.datetime(2000, 1, 1)})
>>> rng["time"].dt.strftime("%B %d, %Y, %r")
<xarray.DataArray 'strftime' ()>
array('January 01, 2000, 12:00:00 AM', dtype=object)
"""
Expand Down Expand Up @@ -400,7 +400,7 @@ class TimedeltaAccessor(Properties):
>>> import pandas as pd
>>> import xarray as xr
>>> dates = pd.timedelta_range(start="1 day", freq="6H", periods=20)
>>> ts = xr.DataArray(dates, dims=('time'))
>>> ts = xr.DataArray(dates, dims=("time"))
>>> ts
<xarray.DataArray (time: 20)>
array([ 86400000000000, 108000000000000, 129600000000000, 151200000000000,
Expand Down
2 changes: 1 addition & 1 deletion xarray/core/accessor_str.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class StringAccessor:
Similar to pandas, fields can be accessed through the `.str` attribute
for applicable DataArrays.

>>> da = xr.DataArray(['some', 'text', 'in', 'an', 'array'])
>>> da = xr.DataArray(["some", "text", "in", "an", "array"])
>>> ds.str.len()
<xarray.DataArray (dim_0: 5)>
array([4, 4, 2, 2, 5])
Expand Down
34 changes: 20 additions & 14 deletions xarray/core/alignment.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,16 @@ def align(
--------

>>> import xarray as xr
>>> x = xr.DataArray([[25, 35], [10, 24]], dims=('lat', 'lon'),
... coords={'lat': [35., 40.], 'lon': [100., 120.]})
>>> y = xr.DataArray([[20, 5], [7, 13]], dims=('lat', 'lon'),
... coords={'lat': [35., 42.], 'lon': [100., 120.]})
>>> x = xr.DataArray(
... [[25, 35], [10, 24]],
... dims=("lat", "lon"),
... coords={"lat": [35.0, 40.0], "lon": [100.0, 120.0]},
... )
>>> y = xr.DataArray(
... [[20, 5], [7, 13]],
... dims=("lat", "lon"),
... coords={"lat": [35.0, 42.0], "lon": [100.0, 120.0]},
... )

>>> x
<xarray.DataArray (lat: 2, lon: 2)>
Expand Down Expand Up @@ -156,7 +162,7 @@ def align(
* lat (lat) float64 35.0
* lon (lon) float64 100.0 120.0

>>> a, b = xr.align(x, y, join='outer')
>>> a, b = xr.align(x, y, join="outer")
>>> a
<xarray.DataArray (lat: 3, lon: 2)>
array([[25., 35.],
Expand All @@ -174,7 +180,7 @@ def align(
* lat (lat) float64 35.0 40.0 42.0
* lon (lon) float64 100.0 120.0

>>> a, b = xr.align(x, y, join='outer', fill_value=-999)
>>> a, b = xr.align(x, y, join="outer", fill_value=-999)
>>> a
<xarray.DataArray (lat: 3, lon: 2)>
array([[ 25, 35],
Expand All @@ -192,7 +198,7 @@ def align(
* lat (lat) float64 35.0 40.0 42.0
* lon (lon) float64 100.0 120.0

>>> a, b = xr.align(x, y, join='left')
>>> a, b = xr.align(x, y, join="left")
>>> a
<xarray.DataArray (lat: 2, lon: 2)>
array([[25, 35],
Expand All @@ -208,7 +214,7 @@ def align(
* lat (lat) float64 35.0 40.0
* lon (lon) float64 100.0 120.0

>>> a, b = xr.align(x, y, join='right')
>>> a, b = xr.align(x, y, join="right")
>>> a
<xarray.DataArray (lat: 2, lon: 2)>
array([[25., 35.],
Expand All @@ -224,13 +230,13 @@ def align(
* lat (lat) float64 35.0 42.0
* lon (lon) float64 100.0 120.0

>>> a, b = xr.align(x, y, join='exact')
>>> a, b = xr.align(x, y, join="exact")
Traceback (most recent call last):
...
"indexes along dimension {!r} are not equal".format(dim)
ValueError: indexes along dimension 'lat' are not equal

>>> a, b = xr.align(x, y, join='override')
>>> a, b = xr.align(x, y, join="override")
>>> a
<xarray.DataArray (lat: 2, lon: 2)>
array([[25, 35],
Expand Down Expand Up @@ -674,8 +680,8 @@ def broadcast(*args, exclude=None):

Broadcast two data arrays against one another to fill out their dimensions:

>>> a = xr.DataArray([1, 2, 3], dims='x')
>>> b = xr.DataArray([5, 6], dims='y')
>>> a = xr.DataArray([1, 2, 3], dims="x")
>>> b = xr.DataArray([5, 6], dims="y")
>>> a
<xarray.DataArray (x: 3)>
array([1, 2, 3])
Expand Down Expand Up @@ -706,8 +712,8 @@ def broadcast(*args, exclude=None):

Fill out the dimensions of all data variables in a dataset:

>>> ds = xr.Dataset({'a': a, 'b': b})
>>> ds2, = xr.broadcast(ds) # use tuple unpacking to extract one dataset
>>> ds = xr.Dataset({"a": a, "b": b})
>>> (ds2,) = xr.broadcast(ds) # use tuple unpacking to extract one dataset
>>> ds2
<xarray.Dataset>
Dimensions: (x: 3, y: 2)
Expand Down
6 changes: 3 additions & 3 deletions xarray/core/combine.py
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,7 @@ def combine_nested(
precipitation (x, y) float64 5.904 2.453 3.404 ...

>>> ds_grid = [[x1y1, x1y2], [x2y1, x2y2]]
>>> combined = xr.combine_nested(ds_grid, concat_dim=['x', 'y'])
>>> combined = xr.combine_nested(ds_grid, concat_dim=["x", "y"])
<xarray.Dataset>
Dimensions: (x: 4, y: 4)
Dimensions without coordinates: x, y
Expand Down Expand Up @@ -441,7 +441,7 @@ def combine_nested(
precipitation (t) float64 5.904 2.453 3.404 ...

>>> ds_grid = [[t1temp, t1precip], [t2temp, t2precip]]
>>> combined = xr.combine_nested(ds_grid, concat_dim=['t', None])
>>> combined = xr.combine_nested(ds_grid, concat_dim=["t", None])
<xarray.Dataset>
Dimensions: (t: 10)
Dimensions without coordinates: t
Expand Down Expand Up @@ -650,7 +650,7 @@ def combine_by_coords(
temperature (y, x) float64 1.654 10.63 7.015 nan ... nan 12.46 2.22 15.96
precipitation (y, x) float64 0.2136 0.9974 0.7603 ... 0.6125 0.4654 0.5953

>>> xr.combine_by_coords([x3, x1], join='override')
>>> xr.combine_by_coords([x3, x1], join="override")
<xarray.Dataset>
Dimensions: (x: 3, y: 4)
Coordinates:
Expand Down
Loading