Skip to content

Commit 618cb92

Browse files
[py] Add explicit stacklevel=2 to remaining warnings.warn calls (#18244)
--------- Co-authored-by: Corey Goldberg <[email protected]>
1 parent 505d93c commit 618cb92

6 files changed

Lines changed: 43 additions & 3 deletions

File tree

‎py/pyproject.toml‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,6 @@ fixable = ["ALL"]
177177
extend-ignore = [
178178
"B007", # Loop control variable not used within loop body
179179
"B024", # Abstract base class without abstract methods
180-
"B028", # No explicit `stacklevel` keyword argument found
181180
"D100", # Missing docstring in public module
182181
"D101", # Missing docstring in public class
183182
"D102", # Missing docstring in public method

‎py/selenium/webdriver/firefox/firefox_profile.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,5 +106,6 @@ def _read_existing_userjs(self, userjs):
106106
self._desired_preferences[matches.group(1)] = json.loads(matches.group(2))
107107
except Exception:
108108
warnings.warn(
109-
f"(skipping) failed to json.loads existing preference: {matches.group(1) + matches.group(2)}"
109+
f"(skipping) failed to json.loads existing preference: {matches.group(1) + matches.group(2)}",
110+
stacklevel=2,
110111
)

‎py/selenium/webdriver/firefox/webdriver.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ def get_full_page_screenshot_as_file(self, filename) -> bool:
183183
warnings.warn(
184184
"name used for saved screenshot does not match file type. It should end with a `.png` extension",
185185
UserWarning,
186+
stacklevel=2,
186187
)
187188
png = self.get_full_page_screenshot_as_png()
188189
try:

‎py/selenium/webdriver/remote/webelement.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -447,6 +447,7 @@ def screenshot(self, filename) -> bool:
447447
warnings.warn(
448448
"name used for saved screenshot does not match file type. It should end with a `.png` extension",
449449
UserWarning,
450+
stacklevel=2,
450451
)
451452
png = self.screenshot_as_png
452453
try:

‎py/selenium/webdriver/support/relative_locator.py‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,11 @@ def with_tag_name(tag_name: str) -> "RelativeBy":
3838
This method is deprecated and may be removed in future versions.
3939
Please use `locate_with` instead.
4040
"""
41-
warnings.warn("This method is deprecated and may be removed in future versions. Please use `locate_with` instead.")
41+
warnings.warn(
42+
"This method is deprecated and may be removed in future versions. Please use `locate_with` instead.",
43+
DeprecationWarning,
44+
stacklevel=2,
45+
)
4246
if not tag_name:
4347
raise WebDriverException("tag_name can not be null")
4448
return RelativeBy({By.CSS_SELECTOR: tag_name})
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Licensed to the Software Freedom Conservancy (SFC) under one
2+
# or more contributor license agreements. See the NOTICE file
3+
# distributed with this work for additional information
4+
# regarding copyright ownership. The SFC licenses this file
5+
# to you under the Apache License, Version 2.0 (the
6+
# "License"); you may not use this file except in compliance
7+
# with the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
18+
import pytest
19+
20+
from selenium.webdriver.common.by import By
21+
from selenium.webdriver.support.relative_locator import locate_with, with_tag_name
22+
23+
24+
def test_locate_with():
25+
locator = locate_with(By.TAG_NAME, "div")
26+
assert locator.to_dict() == {"relative": {"root": {"tag name": "div"}, "filters": []}}
27+
28+
29+
def test_with_tag_name_warning():
30+
with pytest.deprecated_call() as warning_info:
31+
locator = with_tag_name("div")
32+
assert len(warning_info) == 1
33+
assert "locate_with" in str(warning_info[0].message)
34+
assert locator.to_dict() == {"relative": {"root": {"css selector": "div"}, "filters": []}}

0 commit comments

Comments
 (0)