Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit7d04315

Browse files
JohnVillalovosnejch
authored andcommitted
chore: addshow_caller argument toutils.warn()
This allows us to not add the caller's location to the UserWarningmessage.
1 parent9358640 commit7d04315

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

‎gitlab/utils.py‎

Lines changed: 4 additions & 1 deletion
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
ifnotframe_dir.startswith(str(pg_dir)):
198199
break
200+
ifshow_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‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,27 @@ def test_warn(self):
123123
assert__file__instr(warning.message)
124124
assertwarn_source==warning.source
125125

126+
deftest_warn_no_show_caller(self):
127+
warn_message="short and stout"
128+
warn_source="teapot"
129+
130+
withwarnings.catch_warnings(record=True)ascaught_warnings:
131+
utils.warn(
132+
message=warn_message,
133+
category=UserWarning,
134+
source=warn_source,
135+
show_caller=False,
136+
)
137+
assertlen(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+
assertwarning.category==UserWarning
142+
assertisinstance(warning.message,UserWarning)
143+
assertwarn_messageinstr(warning.message)
144+
assert__file__notinstr(warning.message)
145+
assertwarn_source==warning.source
146+
126147

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

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp