Skip to content
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
5 changes: 1 addition & 4 deletions lib/iris/_constraints.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,7 @@
from six.moves import (filter, input, map, range, zip) # noqa
import six

try: # Python 3
from collections.abc import Iterable, Mapping
except ImportError: # Python 2.7
from collections import Iterable, Mapping
from collections.abc import Iterable, Mapping
import operator

import numpy as np
Expand Down
5 changes: 1 addition & 4 deletions lib/iris/analysis/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,7 @@
import six

from collections import OrderedDict
try: # Python 3
from collections.abc import Iterable
except ImportError: # Python 2.7
from collections import Iterable
from collections.abc import Iterable
from functools import wraps

import dask.array as da
Expand Down
5 changes: 1 addition & 4 deletions lib/iris/coords.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,7 @@

from abc import ABCMeta
from collections import namedtuple
try: # Python 3
from collections.abc import Iterator
except ImportError: # Python 2.7
from collections import Iterator
from collections.abc import Iterator
import copy
from itertools import chain
from six.moves import zip_longest
Expand Down
9 changes: 1 addition & 8 deletions lib/iris/cube.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,7 @@
import six

from collections import namedtuple, OrderedDict
try: # Python 3
from collections.abc import (Iterable,
Container,
Mapping,
MutableMapping,
Iterator)
except ImportError: # Python 2.7
from collections import (Iterable,
from collections.abc import (Iterable,
Container,
Mapping,
MutableMapping,
Expand Down
5 changes: 1 addition & 4 deletions lib/iris/fileformats/cf.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,7 @@

from abc import ABCMeta, abstractmethod

try: # Python 3
from collections.abc import Iterable, MutableMapping
except ImportError: # Python 2.7
from collections import Iterable, MutableMapping
from collections.abc import Iterable, MutableMapping
import os
import re
import warnings
Expand Down
5 changes: 1 addition & 4 deletions lib/iris/io/format_picker.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,7 @@
from six.moves import (filter, input, map, range, zip) # noqa
import six

try: # Python 3
from collections.abc import Callable
except ImportError: # Python 2.7
from collections import Callable
from collections.abc import Callable
import functools
import os
import struct
Expand Down
5 changes: 1 addition & 4 deletions lib/iris/iterate.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,7 @@
from __future__ import (absolute_import, division, print_function)
from six.moves import (filter, input, map, range, zip) # noqa

try: # Python 3
from collections.abc import Iterator
except ImportError: # Python 2.7
from collections import Iterator
from collections.abc import Iterator
import itertools
import warnings

Expand Down
5 changes: 1 addition & 4 deletions lib/iris/tests/test_merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,7 @@
# import iris tests first so that some things can be initialised before importing anything else
import iris.tests as tests

try: # Python 3
from collections.abc import Iterable
except ImportError: # Python 2.7
from collections import Iterable
from collections.abc import Iterable
import datetime
import itertools
import numpy as np
Expand Down
5 changes: 0 additions & 5 deletions lib/iris/tests/unit/analysis/maths/test__output_dtype.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,6 @@ def setUp(self):
np.multiply,
np.power,
np.floor_divide]
try:
self.same_result_ops.append(operator.div)
except AttributeError:
# operator.div doesn't exist in Python 3
pass

self.unary_same_result_ops = [np.abs]

Expand Down
10 changes: 2 additions & 8 deletions lib/iris/tests/unit/analysis/maths/test_divide.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,7 @@ class TestBroadcasting(tests.IrisTest_nometa,
CubeArithmeticBroadcastingTestMixin):
@property
def data_op(self):
try:
return operator.div
except AttributeError:
return operator.truediv
return operator.truediv
Copy link
Member

Choose a reason for hiding this comment

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

@stephenworsley It seems that the whole point of the data_op property was convenience i.e., to do the right thing across Python2 and Python wrt div and truediv.

Now that we're Python3, I'm just questioning the need for the existence of the data_op property.

Can't we just use operator.truediv or / directly inline for the tests? i.e.,

    def test_unmasked_div_zero(self):
        # Ensure cube behaviour matches numpy operator behaviour for the
        # handling of arrays containing 0.
        dat_a = np.array([0., 0., 0., 0.])
        dat_b = np.array([2., 2., 2., 2.])

        cube_a = Cube(dat_a)
        cube_b = Cube(dat_b)

        com = dat_b / dat_a
        res = self.cube_func(cube_b, cube_a).data

        self.assertArrayEqual(com, res)

Also drop the import operator as well 😉

Copy link
Contributor Author

Choose a reason for hiding this comment

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

So it looks like what's going on here is that these classes are actually inheriting a bunch of tests from https://github.com/SciTools/iris/blob/master/lib/iris/tests/unit/analysis/maths/__init__.py

data_op is an abstract property in the parent class where it is used in other tests

@abstractproperty
def data_op(self):
# Define an operator to be called, I.E. 'operator.xx'.
pass
It seems like the current usage makes sense in this context.

Copy link
Member

@bjlittle bjlittle Nov 4, 2019

Choose a reason for hiding this comment

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

@stephenworsley Cool.

I guess what I'm pushing is that the whole data_op property as a concept is now defunct and should be removed. For me it's simply technical debt that should be purged, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't think it is defunct, it allows code reuse for the tests in iris.tests.unit.maths.__init__ over multiple different operations, see also addition for example https://github.com/SciTools/iris/blob/master/lib/iris/tests/unit/analysis/maths/test_add.py

Copy link
Member

Choose a reason for hiding this comment

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

Okay, so it's generic and not specific, thanks for clarifying 👍


@property
def cube_func(self):
Expand All @@ -42,10 +39,7 @@ def cube_func(self):
class TestMasking(tests.IrisTest_nometa, CubeArithmeticMaskingTestMixin):
@property
def data_op(self):
try:
return operator.div
except AttributeError:
return operator.truediv
return operator.truediv
Copy link
Member

Choose a reason for hiding this comment

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

As the above comment.


@property
def cube_func(self):
Expand Down
5 changes: 1 addition & 4 deletions lib/iris/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,7 @@
from six.moves import (filter, input, map, range, zip) # noqa
import six

try: # Python 3
from collections.abc import Hashable
except ImportError: # Python 2.7
from collections import Hashable
from collections.abc import Hashable
import abc
from contextlib import contextmanager
import copy
Expand Down