Skip to content

Commit

Permalink
Fix lints
Browse files Browse the repository at this point in the history
  • Loading branch information
shunping committed Feb 4, 2025
1 parent 0e6b9a7 commit acc6448
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 21 deletions.
17 changes: 9 additions & 8 deletions sdks/python/apache_beam/ml/anomaly/base_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,9 @@ def learn_one(self):
def score_one(self):
...

def __eq__(self, value: "TestAnomalyDetector.Dummy") -> bool:
return self._my_arg == value._my_arg
def __eq__(self, value) -> bool:
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"))
Expand Down Expand Up @@ -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):
Expand All @@ -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()
Expand Down
4 changes: 2 additions & 2 deletions sdks/python/apache_beam/ml/anomaly/specifiable.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def get_subspace(cls):
subspace = "*"
for c in cls.mro():
if c in ACCEPTED_SPECIFIABLE_SUBSPACES:
subspace = c.__name__ # type: ignore
subspace = c.__name__
break
return subspace

Expand Down Expand Up @@ -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

Expand Down
23 changes: 12 additions & 11 deletions sdks/python/apache_beam/ml/anomaly/specifiable_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class MyClass():
pass

# class is not decorated/registered
self.assertRaises(AttributeError, lambda: MyClass().to_spec()) # type: ignore
self.assertRaises(AttributeError, lambda: MyClass().to_spec())

self.assertNotIn("MyKey", KNOWN_SPECIFIABLE["*"])

Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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})
Expand Down Expand Up @@ -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):
Expand All @@ -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})
Expand Down Expand Up @@ -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

Expand All @@ -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


Expand Down Expand Up @@ -413,15 +414,15 @@ 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)

child_2 = Child_Error_2(5)
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)

Expand Down

0 comments on commit acc6448

Please sign in to comment.