File tree 2 files changed +25
-1
lines changed
2 files changed +25
-1
lines changed Original file line number Diff line number Diff line change @@ -177,6 +177,7 @@ def warn(
177
177
* ,
178
178
category : Optional [Type [Warning ]] = None ,
179
179
source : Optional [Any ] = None ,
180
+ show_caller : bool = True ,
180
181
) -> None :
181
182
"""This `warnings.warn` wrapper function attempts to show the location causing the
182
183
warning in the user code that called the library.
@@ -196,8 +197,10 @@ def warn(
196
197
frame_dir = str (pathlib .Path (frame .filename ).parent .resolve ())
197
198
if not frame_dir .startswith (str (pg_dir )):
198
199
break
200
+ if show_caller :
201
+ message += warning_from
199
202
warnings .warn (
200
- message = message + warning_from ,
203
+ message = message ,
201
204
category = category ,
202
205
stacklevel = stacklevel ,
203
206
source = source ,
Original file line number Diff line number Diff line change @@ -123,6 +123,27 @@ def test_warn(self):
123
123
assert __file__ in str (warning .message )
124
124
assert warn_source == warning .source
125
125
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
+
126
147
127
148
@pytest .mark .parametrize (
128
149
"source,expected" ,
You can’t perform that action at this time.
0 commit comments