diff --git a/sdks/python/apache_beam/ml/anomaly/base_test.py b/sdks/python/apache_beam/ml/anomaly/base_test.py index 1894601624ae..8e8efc72330f 100644 --- a/sdks/python/apache_beam/ml/anomaly/base_test.py +++ b/sdks/python/apache_beam/ml/anomaly/base_test.py @@ -57,7 +57,8 @@ def score_one(self): ... def __eq__(self, value: "TestAnomalyDetector.Dummy") -> bool: - return self._my_arg == value._my_arg + return isinstance(value, TestAnomalyDetector.Dummy) and \ + self._my_arg == value._my_arg def test_unknown_detector(self): self.assertRaises(ValueError, Specifiable.from_spec, Spec(type="unknown")) @@ -141,9 +142,9 @@ def learn_one(self): def score_one(self): ... - def __eq__( - self, value: 'TestEnsembleAnomalyDetector.DummyEnsemble') -> bool: - return self._my_ensemble_arg == value._my_ensemble_arg + def __eq__(self, value) -> bool: + return isinstance(value, TestEnsembleAnomalyDetector.DummyEnsemble) and \ + self._my_ensemble_arg == value._my_ensemble_arg @specifiable(on_demand_init=False) class DummyWeakLearner(AnomalyDetector): @@ -157,9 +158,9 @@ def learn_one(self): def score_one(self): ... - def __eq__( - self, value: 'TestEnsembleAnomalyDetector.DummyWeakLearner') -> bool: - return self._my_arg == value._my_arg + def __eq__(self, value) -> bool: + return isinstance(value, TestEnsembleAnomalyDetector.DummyWeakLearner) \ + and self._my_arg == value._my_arg def test_model_id_on_known_detector(self): a = self.DummyEnsemble() diff --git a/sdks/python/apache_beam/ml/anomaly/specifiable.py b/sdks/python/apache_beam/ml/anomaly/specifiable.py index 5db63721a33e..1c53d62ec6e8 100644 --- a/sdks/python/apache_beam/ml/anomaly/specifiable.py +++ b/sdks/python/apache_beam/ml/anomaly/specifiable.py @@ -205,7 +205,7 @@ def new_getattr(self, name): cls._run_init = run_init cls.to_spec = Specifiable.to_spec cls._to_spec_helper = staticmethod(Specifiable._to_spec_helper) - cls.from_spec = classmethod(Specifiable.from_spec) # type: ignore + cls.from_spec = classmethod(Specifiable.from_spec) cls._from_spec_helper = staticmethod(Specifiable._from_spec_helper) return cls diff --git a/sdks/python/apache_beam/ml/anomaly/specifiable_test.py b/sdks/python/apache_beam/ml/anomaly/specifiable_test.py index 28e2632933fa..47888022e386 100644 --- a/sdks/python/apache_beam/ml/anomaly/specifiable_test.py +++ b/sdks/python/apache_beam/ml/anomaly/specifiable_test.py @@ -124,8 +124,9 @@ def __init__(self, product: Product, quantity: int = 1): self._product = product self._quantity = quantity - def __eq__(self, value: 'Entry') -> bool: - return self._product == value._product and \ + def __eq__(self, value) -> bool: + return isinstance(value, Entry) and \ + self._product == value._product and \ self._quantity == value._quantity @specifiable( @@ -199,10 +200,10 @@ def __init__(self, arg): self.assertRaises(AttributeError, getattr, foo, "my_arg") self.assertRaises(AttributeError, lambda: foo.my_arg) self.assertRaises(AttributeError, getattr, foo, "unknown_arg") - self.assertRaises(AttributeError, lambda: foo.unknown_arg) # type: ignore + self.assertRaises(AttributeError, lambda: foo.unknown_arg) self.assertEqual(FooOnDemand.counter, 0) - foo_2 = FooOnDemand(456, _run_init=True) # type: ignore + foo_2 = FooOnDemand(456, _run_init=True) self.assertEqual(FooOnDemand.counter, 1) self.assertIn("_init_params", foo_2.__dict__) self.assertEqual(foo_2.__dict__["_init_params"], {"arg": 456}) @@ -231,7 +232,7 @@ def __init__(self, arg): # __init__ is called when trying to accessing an attribute self.assertEqual(foo.my_arg, 3210) self.assertEqual(FooJustInTime.counter, 1) - self.assertRaises(AttributeError, lambda: foo.unknown_arg) # type: ignore + self.assertRaises(AttributeError, lambda: foo.unknown_arg) self.assertEqual(FooJustInTime.counter, 1) def test_on_demand_and_just_in_time_init(self): @@ -255,7 +256,7 @@ def __init__(self, arg): self.assertEqual(FooOnDemandAndJustInTime.counter, 1) # __init__ is called - foo_2 = FooOnDemandAndJustInTime(789, _run_init=True) # type: ignore + foo_2 = FooOnDemandAndJustInTime(789, _run_init=True) self.assertEqual(FooOnDemandAndJustInTime.counter, 2) self.assertIn("_init_params", foo_2.__dict__) self.assertEqual(foo_2.__dict__["_init_params"], {"arg": 789}) @@ -355,7 +356,7 @@ class Child_Error_1(Parent): child_class_var = 2001 def __init__(self, c): - self.child_inst_var += 1 # type: ignore + self.child_inst_var += 1 super().__init__(c) Child_2.counter += 1 @@ -366,7 +367,7 @@ class Child_Error_2(Parent): child_class_var = 2001 def __init__(self, c): - self.parent_inst_var += 1 # type: ignore + self.parent_inst_var += 1 Child_2.counter += 1 @@ -413,7 +414,7 @@ def test_error_in_child(self): self.assertEqual(child_1.child_class_var, 2001) # error during child initialization - self.assertRaises(AttributeError, lambda: child_1.child_inst_var) # type: ignore + self.assertRaises(AttributeError, lambda: child_1.child_inst_var) self.assertEqual(Parent.counter, 0) self.assertEqual(Child_1.counter, 0) @@ -421,7 +422,7 @@ def test_error_in_child(self): self.assertEqual(child_2.child_class_var, 2001) # error during child initialization - self.assertRaises(AttributeError, lambda: child_2.parent_inst_var) # type: ignore + self.assertRaises(AttributeError, lambda: child_2.parent_inst_var) self.assertEqual(Parent.counter, 0) self.assertEqual(Child_2.counter, 0)