Skip to content

Open-source Python library for AI browser agents, plus Browser Use Cloud: stealth browsers and a hosted agent API.

cleanup event name overlap check

Nick Sweeting4a4ce9ff45d3
diff --git a/browser_use/browser/events.py b/browser_use/browser/events.py
index 77142f86b..a74762a1b 100644
--- a/browser_use/browser/events.py
+++ b/browser_use/browser/events.py
@@ -1,5 +1,6 @@
"""Event definitions for browser communication."""
+import inspect
from typing import Any, Literal
from bubus import BaseEvent
@@ -365,16 +366,32 @@ class AboutBlankDVDScreensaverShownEvent(BaseEvent):
# Events with 'EnhancedDOMTreeNode' forward references (ClickElementEvent, TypeTextEvent,
# ScrollEvent, UploadFileEvent) need model_rebuild() called after imports are complete
-# check that event names are valid and non-overlapping (naiively n^2 so it's pretty slow but ok for now, optimize when >20 events)
-event_names = {
- name.split('[')[0]
- for name in globals().keys()
- if not name.startswith('_') and issubclass(globals()[name], BaseEvent) and name != 'BaseEvent'
-}
-for name_a in event_names:
- assert name_a.endswith('Event'), f'Event with name {name_a} does not end with "Event"'
- for name_b in event_names:
- if name_a != name_b: # Skip self-comparison
- assert name_a not in name_b, (
- f'Event with name {name_a} is a substring of {name_b}, all events must be completely unique to avoid find-and-replace accidents'
- )
+
+def _check_event_names_dont_overlap():
+ """
+ check that event names defined in this file are valid and non-overlapping
+ (naiively n^2 so it's pretty slow but ok for now, optimize when >20 events)
+ """
+ event_names = {
+ name.split('[')[0]
+ for name in globals().keys()
+ if not name.startswith('_')
+ and inspect.isclass(globals()[name])
+ and issubclass(globals()[name], BaseEvent)
+ and name != 'BaseEvent'
+ }
+ for name_a in event_names:
+ assert name_a.endswith('Event'), f'Event with name {name_a} does not end with "Event"'
+ for name_b in event_names:
+ if name_a != name_b: # Skip self-comparison
+ assert name_a not in name_b, (
+ f'Event with name {name_a} is a substring of {name_b}, all events must be completely unique to avoid find-and-replace accidents'
+ )
+
+
+# overlapping event names are a nightmare to trace and rename later, dont do it!
+# e.g. prevent ClickEvent and FailedClickEvent are terrible names because one is a substring of the other,
+# must be ClickEvent and ClickFailedEvent to preserve the usefulnes of codebase grep/sed/awk as refactoring tools.
+# at import time, we do a quick check that all event names defined above are valid and non-overlapping.
+# this is hand written in blood by a human! not LLM slop. feel free to optimize but do not remove it without a good reason.
+_check_event_names_dont_overlap()