Skip to content

Commit 7d04315

Browse files
JohnVillalovosnejch
authored andcommitted
chore: add show_caller argument to utils.warn()
This allows us to not add the caller's location to the UserWarning message.
1 parent 9358640 commit 7d04315

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

gitlab/utils.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@ def warn(
177177
*,
178178
category: Optional[Type[Warning]] = None,
179179
source: Optional[Any] = None,
180+
show_caller: bool = True,
180181
) -> None:
181182
"""This `warnings.warn` wrapper function attempts to show the location causing the
182183
warning in the user code that called the library.
@@ -196,8 +197,10 @@ def warn(
196197
frame_dir = str(pathlib.Path(frame.filename).parent.resolve())
197198
if not frame_dir.startswith(str(pg_dir)):
198199
break
200+
if show_caller:
201+
message += warning_from
199202
warnings.warn(
200-
message=message + warning_from,
203+
message=message,
201204
category=category,
202205
stacklevel=stacklevel,
203206
source=source,

tests/unit/test_utils.py

+21
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,27 @@ def test_warn(self):
123123
assert __file__ in str(warning.message)
124124
assert warn_source == warning.source
125125

126+
def test_warn_no_show_caller(self):
127+
warn_message = "short and stout"
128+
warn_source = "teapot"
129+
130+
with warnings.catch_warnings(record=True) as caught_warnings:
131+
utils.warn(
132+
message=warn_message,
133+
category=UserWarning,
134+
source=warn_source,
135+
show_caller=False,
136+
)
137+
assert len(caught_warnings) == 1
138+
warning = caught_warnings[0]
139+
# File name is this file as it is the first file outside of the `gitlab/` path.
140+
assert __file__ == warning.filename
141+
assert warning.category == UserWarning
142+
assert isinstance(warning.message, UserWarning)
143+
assert warn_message in str(warning.message)
144+
assert __file__ not in str(warning.message)
145+
assert warn_source == warning.source
146+
126147

127148
@pytest.mark.parametrize(
128149
"source,expected",

0 commit comments

Comments
 (0)