017ReviewmidPython / Astropy真实来源改编 Astropy
Astropy NDData:整理 mask 传播里的 None 判断
审查一个 Astropy NDData PR:作者认为 mask 传播里的 operand.mask is None 分支写法含糊,改成更直白的 operand is None。
@@ -503,9 +503,9 @@ class NDArithmeticMixin: # Only self has no mask -> take the operand's mask. elif self.mask is None: return deepcopy(operand.mask) - # The operand exists but carries no mask -> keep self's mask.- # Without this branch we would call handle_mask(self.mask, None).- elif operand.mask is None:+ # No second operand participates -> keep self's mask.+ elif operand is None: return deepcopy(self.mask) # Both operands carry a mask -> combine them. else: return handle_mask(self.mask, operand.mask, **kwds)@@ -880,6 +880,15 @@ def test_arithmetic_mask_both_present(): assert_array_equal(result.mask, np.array([True, True, False])) +def test_arithmetic_mask_no_second_operand():+ # With no second operand participating, self's mask must be preserved.+ ndd = NDDataRef(np.arange(3), mask=np.array([True, False, True]))+ result = ndd._arithmetic_mask(+ np.add, None, handle_mask=np.bitwise_or+ )+ assert_array_equal(result, np.array([True, False, True]))++ def test_arithmetic_mask_handle_mask_none(): ndd = NDDataRef(np.arange(3), mask=np.array([True, False, True])) assert ndd._arithmetic_mask(np.add, ndd, handle_mask=None) is None