Skip to content

[py] Use Self as return type of __enter__ in remote.WebDriver#17170

Merged
cgoldberg merged 1 commit into
SeleniumHQ:trunkfrom
pinterior:use-self-type-for-webdriver-enter
Mar 3, 2026
Merged

[py] Use Self as return type of __enter__ in remote.WebDriver#17170
cgoldberg merged 1 commit into
SeleniumHQ:trunkfrom
pinterior:use-self-type-for-webdriver-enter

Conversation

@pinterior
Copy link
Copy Markdown
Contributor

@pinterior pinterior commented Mar 3, 2026

🔗 Related Issues

Fixes #17169

💥 What does this PR do?

change the return type of s.w.remote.webdriver.WebDriver.__enter__ from s.w.remote.webdriver.WebDriver to Self.
as a result, drv.get_log in the following code can be resolved as expected.

with selenium.webdriver.chrome.webdriver.WebDriver() as drv:
    reveal_type(drv.get_log)

🔧 Implementation Notes

since selenium currently supports Python 3.10, i used typing_extensions.Self instead of typing.Self.

💡 Additional Considerations

🔄 Types of changes

  • Bug fix (backwards compatible)

@qodo-code-review
Copy link
Copy Markdown
Contributor

Review Summary by Qodo

Use Self type for WebDriver __enter__ return type

🐞 Bug fix

Grey Divider

Walkthroughs

Description
• Changes __enter__ return type from WebDriver to Self
• Enables proper type inference for context manager usage
• Imports Self from typing_extensions for Python 3.10 compatibility

Grey Divider

File Changes

1. py/selenium/webdriver/remote/webdriver.py 🐞 Bug fix +3/-1

Update WebDriver __enter__ to use Self type

• Added import of Self from typing_extensions
• Changed __enter__ method return type annotation from "WebDriver" to Self
• Improves type checking for context manager pattern usage

py/selenium/webdriver/remote/webdriver.py


Grey Divider

Qodo Logo

@qodo-code-review
Copy link
Copy Markdown
Contributor

qodo-code-review Bot commented Mar 3, 2026

Code Review by Qodo

🐞 Bugs (0) 📘 Rule violations (1) 📎 Requirement gaps (0)

Grey Divider


Remediation recommended

1. No test for __enter__ typing 📘 Rule violation ⛯ Reliability
Description
This PR is a typing bug fix to WebDriver.__enter__, but it does not add any unit/mypy regression
coverage to prevent the return type from regressing. That increases the risk of future changes
silently reintroducing the type-resolution issue.
Code

py/selenium/webdriver/remote/webdriver.py[R292-293]

+    def __enter__(self) -> Self:
        return self
Evidence
The checklist requires fixes to be accompanied by appropriate tests when feasible (PR Compliance ID
5). The PR description labels this as a bug fix and the changed code shows the updated __enter__
return type, but there are no accompanying test/type-check assertions added in this PR to lock in
the new typing behavior.

AGENTS.md
py/selenium/webdriver/remote/webdriver.py[292-293]

Agent prompt
The issue below was found during a code review. Follow the provided context and guidance below and implement a solution

## Issue description
A typing-focused bug fix changed `WebDriver.__enter__` to return `Self`, but the PR adds no regression coverage to ensure type checkers keep inferring the `with ... as drv` variable as the concrete driver type.

## Issue Context
The PR is intended to improve type resolution (e.g., resolving `drv.get_log` after `with ... as drv:`). Adding a mypy-only assertion (e.g., under `TYPE_CHECKING` using `typing_extensions.assert_type` and `cast`) would provide fast, stable coverage without introducing a browser test.

## Fix Focus Areas
- py/selenium/webdriver/remote/webdriver.py[289-294]

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools


Grey Divider

ⓘ The new review experience is currently in Beta. Learn more

Grey Divider

Qodo Logo

@selenium-ci selenium-ci added the C-py Python Bindings label Mar 3, 2026
@cgoldberg cgoldberg changed the title s.w.remote.webdriver.WebDriver: use Self as return type of __enter__ [py] Use Self as return type of __enter__ in remote.WebDriver Mar 3, 2026
Copy link
Copy Markdown
Member

@cgoldberg cgoldberg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 👍
thanks.

@cgoldberg
Copy link
Copy Markdown
Member

CI is going to fail on this branch because of a bug in Firefox beta.. it's unrelated to this PR.

@cgoldberg cgoldberg merged commit 5eb3fe1 into SeleniumHQ:trunk Mar 3, 2026
27 of 28 checks passed
This was referenced Apr 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

C-py Python Bindings

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[🐛 Bug]: with-as statement and type of WebDriver

3 participants