pw_presubmit: Use AbstractContextManager

Compare objects against contextlib.AbstractContextManager instead of
checking for methods like '__enter__'.

Bug: b/253021172
Change-Id: Ia6135df68e3fa91ba7be004f62b45d5401dcc784
Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/126194
Commit-Queue: Auto-Submit <auto-submit@pigweed.google.com.iam.gserviceaccount.com>
Reviewed-by: Wyatt Hepler <hepler@google.com>
Pigweed-Auto-Submit: Rob Mohr <mohrr@google.com>
diff --git a/pw_presubmit/py/pw_presubmit/build.py b/pw_presubmit/py/pw_presubmit/build.py
index 6280a8c..5feb6fc 100644
--- a/pw_presubmit/py/pw_presubmit/build.py
+++ b/pw_presubmit/py/pw_presubmit/build.py
@@ -594,11 +594,8 @@
     ) -> PresubmitResult:
         with contextlib.ExitStack() as stack:
             for mgr in self.ninja_contexts:
-                # hasattr(x, '__enter__') can return True for types, so
-                # explicitly exclude them. Only non-type objects can be
-                # context managers.
-                if hasattr(mgr, '__enter__') and not isinstance(mgr, type):
-                    stack.enter_context(mgr)  # type: ignore
+                if isinstance(mgr, contextlib.AbstractContextManager):
+                    stack.enter_context(mgr)
                 else:
                     stack.enter_context(mgr(ctx))  # type: ignore
             ninja(ctx, *targets)