Skip to content

[py] Add explicit stacklevel=2 to remaining warnings.warn calls - #18244

Merged
cgoldberg merged 3 commits into
SeleniumHQ:trunkfrom
saiyam-jain-dev:fix-py-warning-stacklevel
Oct 27, 2026
Merged

cgoldberg merged 3 commits into
SeleniumHQ:trunkfrom
saiyam-jain-dev:fix-py-warning-stacklevel

Conversation

@saiyam-jain-dev

@saiyam-jain-dev saiyam-jain-dev commented Oct 26, 2026 •

Copy link
Copy Markdown
Contributor

Description

When warnings.warn() is invoked without an explicit stacklevel argument, Python defaults to stacklevel=1. This causes the emitted warning to point inside Selenium's internal implementation rather than pointing to the caller's callsite that invoked the method.

Across most of py/selenium/webdriver/, warnings.warn(..., stacklevel=2) is already used consistently. However, four call sites omitted stacklevel.

This PR:

  • Sets stacklevel=2 on the remaining warning calls so warnings point to the caller's stack frame.
  • Explicitly adds DeprecationWarning to with_tag_name().
  • Adds unit tests in py/test/unit/selenium/webdriver/support/relative_locator_tests.py.
  • Resolves Ruff B028 across the entire Python bindings.

Motivation and Context

Improves developer experience and debugging by correctly attributing warnings to the user's code, adhering to Ruff B028.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist

  • I have read the contributing document.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have added tests to cover my changes.
  • All new and existing tests passed.

@qodo-code-review

Copy link
Copy Markdown
Contributor

Qodo reviews are paused for this user.

Troubleshooting steps vary by plan Learn more →

On a Teams plan?
Reviews resume once this user has a paid seat and their Git account is linked in Qodo.
Link Git account →

Using GitHub Enterprise Server, GitLab Self-Managed, or Bitbucket Data Center?
These require an Enterprise plan - Contact us
Contact us →

@selenium-ci selenium-ci added C-py Python Bindings B-support Issue or PR related to support classes labels Oct 26, 2026
@cgoldberg
cgoldberg self-requested a review October 27, 2026 16:56

@cgoldberg cgoldberg left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM 👍

@cgoldberg

Copy link
Copy Markdown
Member

@saiyam-jain-dev thanks. I just pushed a commit to your branch that removed B028 from the ruff ignore list. I'll merge after CI runs.

Also, do you use some kind of translator? All of your PR descriptions have really weird Unicode characters throughout them in various places. I just cleaned up your PR description for this one.

@SeleniumHQ SeleniumHQ deleted a comment from selenium-ci Oct 27, 2026
@cgoldberg
cgoldberg merged commit 618cb92 into SeleniumHQ:trunk Oct 27, 2026
32 checks passed
@saiyam-jain-dev

Copy link
Copy Markdown
Contributor Author

Thanks for updating the Ruff ignore list and cleaning up the description, Corey!

No translator at all. It was just an issue with my editor and clipboard tool escaping backticks with backslashes and mangling symbols like § and quotes when pasting into GitHub. Thanks for pointing it out, I'll definitely make sure future PR descriptions are clean plain markdown.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

B-support Issue or PR related to support classes C-py Python Bindings

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants