[py] Use lazy imports in webdriver __init__.py#16993
Conversation
Replace eager imports of all browser modules with lazy loading via __getattr__. This enables future modularization of the Bazel build and improves import performance for users who only use a subset of browsers. The public API is fully backwards compatible - all existing import patterns continue to work identically. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
PR Compliance Guide 🔍Below is a summary of compliance checks for this PR:
Compliance status legend🟢 - Fully Compliant🟡 - Partial Compliant 🔴 - Not Compliant ⚪ - Requires Further Human Verification 🏷️ - Compliance label |
||||||||||||||||||||||||
PR Code Suggestions ✨Latest suggestions up to d3c78b6
Previous suggestions✅ Suggestions up to commit b98c7a6
Suggestions up to commit 81e764b
✅ Suggestions up to commit d758f36
✅ Suggestions up to commit acfa8ca
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Pull request overview
This PR replaces eager imports with lazy loading in the selenium/webdriver/__init__.py module to improve startup performance and enable future Bazel build modularization. The implementation uses Python's PEP 562 module-level __getattr__ to defer imports until attributes are accessed.
Changes:
- Removed 24 eager import statements for browser modules and utilities
- Added lazy import mechanism using
__getattr__with a_LAZY_IMPORTSdictionary mapping - Maintained full backwards compatibility with the public API
2f21aac to
d758f36
Compare
|
Persistent suggestions updated to latest commit d3c78b6 |
cgoldberg
left a comment
There was a problem hiding this comment.
LGTM.
FWIW, Python 3.15 is adding direct support for lazy imports, so someday we can replace this with the new syntax: https://peps.python.org/pep-0810/
User description
Step 1 in getting Bazel Python to work for us instead of against us
💥 What does this PR do?
Replace eager imports of all browser modules in
py/selenium/webdriver/__init__.pywith lazy loading via__getattr__. This:The public API is fully backwards compatible - all existing import patterns continue to work identically.
🔧 Implementation Notes
Uses Python's
__getattr__module-level function (PEP 562) to defer imports until attributes are actually accessed. This is the standard Python approach for lazy module loading.💡 Additional Considerations
None - this is a drop-in replacement with no behavioral changes to the public API.
🔄 Types of changes
PR Type
Enhancement
Description
Replace eager imports with lazy loading via
__getattr__Improves import performance for subset browser usage
Enables future Bazel build modularization
Maintains full backwards compatibility with public API
Diagram Walkthrough
File Walkthrough
__init__.py
Convert eager imports to lazy loading mechanismpy/selenium/webdriver/init.py
importlibimport for dynamic module loading_LAZY_IMPORTSdictionary mapping public names to modulepaths and attributes
__getattr__function to handle lazy loading on attributeaccess
__dir__function to support introspection of availableattributes
__all__list for explicit public API exports