From dc5b805612df12ab82be65dfa0fcfbad85960d93 Mon Sep 17 00:00:00 2001 From: Prajjwal Nijhara Date: Fri, 24 Apr 2020 03:56:36 +0530 Subject: [PATCH] Fix some code quality and bug-risk issues --- .deepsource.toml | 18 ++++++++++++++++++ MANIFEST.in | 1 + xarray/coding/cftime_offsets.py | 4 ++-- xarray/coding/cftimeindex.py | 2 +- xarray/convert.py | 6 +++--- xarray/core/computation.py | 4 ++-- xarray/core/formatting.py | 12 +++++------- xarray/core/groupby.py | 4 +++- xarray/plot/plot.py | 2 +- 9 files changed, 36 insertions(+), 17 deletions(-) create mode 100644 .deepsource.toml diff --git a/.deepsource.toml b/.deepsource.toml new file mode 100644 index 00000000000..e37b41de303 --- /dev/null +++ b/.deepsource.toml @@ -0,0 +1,18 @@ +version = 1 + +test_patterns = [ + "*/tests/**", + "*/test_*.py" +] + +exclude_patterns = [ + "doc/**", + "ci/**" +] + +[[analyzers]] +name = "python" +enabled = true + + [analyzers.meta] + runtime_version = "3.x.x" \ No newline at end of file diff --git a/MANIFEST.in b/MANIFEST.in index cbfb8c8cdca..3dffb4bde77 100644 --- a/MANIFEST.in +++ b/MANIFEST.in @@ -1,4 +1,5 @@ include LICENSE +include *.toml recursive-include licenses * recursive-include doc * prune doc/_build diff --git a/xarray/coding/cftime_offsets.py b/xarray/coding/cftime_offsets.py index a2306331ca7..9f8825a0213 100644 --- a/xarray/coding/cftime_offsets.py +++ b/xarray/coding/cftime_offsets.py @@ -987,9 +987,9 @@ def cftime_range( else: raise ValueError("Closed must be either 'left', 'right' or None") - if not left_closed and len(dates) and start is not None and dates[0] == start: + if not left_closed and dates and start is not None and dates[0] == start: dates = dates[1:] - if not right_closed and len(dates) and end is not None and dates[-1] == end: + if not right_closed and dates and end is not None and dates[-1] == end: dates = dates[:-1] return CFTimeIndex(dates, name=name) diff --git a/xarray/coding/cftimeindex.py b/xarray/coding/cftimeindex.py index 6fc28d213dd..f90367ba4c2 100644 --- a/xarray/coding/cftimeindex.py +++ b/xarray/coding/cftimeindex.py @@ -309,7 +309,7 @@ def _partial_date_slice(self, resolution, parsed): times = self._data if self.is_monotonic: - if len(times) and ( + if times and ( (start < times[0] and end < times[0]) or (start > times[-1] and end > times[-1]) ): diff --git a/xarray/convert.py b/xarray/convert.py index 4974a55d8e2..0c86b090f34 100644 --- a/xarray/convert.py +++ b/xarray/convert.py @@ -229,11 +229,11 @@ def _iris_cell_methods_to_str(cell_methods_obj): """ cell_methods = [] for cell_method in cell_methods_obj: - names = "".join([f"{n}: " for n in cell_method.coord_names]) + names = "".join(f"{n}: " for n in cell_method.coord_names) intervals = " ".join( - [f"interval: {interval}" for interval in cell_method.intervals] + f"interval: {interval}" for interval in cell_method.intervals ) - comments = " ".join([f"comment: {comment}" for comment in cell_method.comments]) + comments = " ".join(f"comment: {comment}" for comment in cell_method.comments) extra = " ".join([intervals, comments]).strip() if extra: extra = f" ({extra})" diff --git a/xarray/core/computation.py b/xarray/core/computation.py index 6cf4178b5bf..a3723ea9db9 100644 --- a/xarray/core/computation.py +++ b/xarray/core/computation.py @@ -1192,10 +1192,10 @@ def dot(*arrays, dims=None, **kwargs): # construct einsum subscripts, such as '...abc,...ab->...c' # Note: input_core_dims are always moved to the last position subscripts_list = [ - "..." + "".join([dim_map[d] for d in ds]) for ds in input_core_dims + "..." + "".join(dim_map[d] for d in ds) for ds in input_core_dims ] subscripts = ",".join(subscripts_list) - subscripts += "->..." + "".join([dim_map[d] for d in output_core_dims[0]]) + subscripts += "->..." + "".join(dim_map[d] for d in output_core_dims[0]) join = OPTIONS["arithmetic_join"] # using "inner" emulates `(a * b).sum()` for all joins (except "exact") diff --git a/xarray/core/formatting.py b/xarray/core/formatting.py index 534d253ecc8..d6732fc182e 100644 --- a/xarray/core/formatting.py +++ b/xarray/core/formatting.py @@ -298,12 +298,10 @@ def _summarize_coord_multiindex(coord, col_width, marker): def _summarize_coord_levels(coord, col_width, marker="-"): return "\n".join( - [ - summarize_variable( - lname, coord.get_level_variable(lname), col_width, marker=marker - ) - for lname in coord.level_names - ] + summarize_variable( + lname, coord.get_level_variable(lname), col_width, marker=marker + ) + for lname in coord.level_names ) @@ -562,7 +560,7 @@ def extra_items_repr(extra_keys, mapping, ab_side): for m in (a_mapping, b_mapping): attr_s = "\n".join( - [summarize_attr(ak, av) for ak, av in m[k].attrs.items()] + summarize_attr(ak, av) for ak, av in m[k].attrs.items() ) attrs_summary.append(attr_s) diff --git a/xarray/core/groupby.py b/xarray/core/groupby.py index 5a5f4c0d296..148e16863d1 100644 --- a/xarray/core/groupby.py +++ b/xarray/core/groupby.py @@ -273,7 +273,7 @@ def __init__( grouper=None, bins=None, restore_coord_dims=None, - cut_kwargs={}, + cut_kwargs=None, ): """Create a GroupBy object @@ -299,6 +299,8 @@ def __init__( Extra keyword arguments to pass to `pandas.cut` """ + if cut_kwargs is None: + cut_kwargs = {} from .dataarray import DataArray if grouper is not None and bins is not None: diff --git a/xarray/plot/plot.py b/xarray/plot/plot.py index 4657bee9415..4d6033bf00d 100644 --- a/xarray/plot/plot.py +++ b/xarray/plot/plot.py @@ -30,7 +30,7 @@ def _infer_line_data(darray, x, y, hue): error_msg = "must be either None or one of ({:s})".format( - ", ".join([repr(dd) for dd in darray.dims]) + ", ".join(repr(dd) for dd in darray.dims) ) ndims = len(darray.dims)