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

Commit9cbfd02

Browse files
chore: create a customwarnings.warn wrapper
Create a custom `warnings.warn` wrapper that will walk the stack traceto find the first frame outside of the `gitlab/` path to print thewarning against. This will make it easier for users to find where intheir code the error is generated from
1 parent4cb7d92 commit9cbfd02

File tree

5 files changed

+75
-20
lines changed

5 files changed

+75
-20
lines changed

‎gitlab/__init__.py‎

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
fromtypingimportAny
2121

2222
importgitlab.config# noqa: F401
23+
fromgitlabimportutilsas_utils
2324
fromgitlab._versionimport (# noqa: F401
2425
__author__,
2526
__copyright__,
@@ -40,11 +41,13 @@
4041
def__getattr__(name:str)->Any:
4142
# Deprecate direct access to constants without namespace
4243
ifnameingitlab.const._DEPRECATED:
43-
warnings.warn(
44-
f"\nDirect access to 'gitlab.{name}' is deprecated and will be "
45-
f"removed in a future major python-gitlab release. Please "
46-
f"use 'gitlab.const.{name}' instead.",
47-
DeprecationWarning,
44+
_utils.warn(
45+
message=(
46+
f"\nDirect access to 'gitlab.{name}' is deprecated and will be "
47+
f"removed in a future major python-gitlab release. Please "
48+
f"use 'gitlab.const.{name}' instead."
49+
),
50+
category=DeprecationWarning,
4851
)
4952
returngetattr(gitlab.const,name)
5053
raiseAttributeError(f"module{__name__} has no attribute{name}")

‎gitlab/utils.py‎

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,11 @@
1515
# You should have received a copy of the GNU Lesser General Public License
1616
# along with this program. If not, see <http://www.gnu.org/licenses/>.
1717

18+
importos
19+
importtraceback
1820
importurllib.parse
19-
fromtypingimportAny,Callable,Dict,Optional,Union
21+
importwarnings
22+
fromtypingimportAny,Callable,Dict,Optional,Type,Union
2023

2124
importrequests
2225

@@ -90,3 +93,30 @@ def __new__( # type: ignore
9093

9194
defremove_none_from_dict(data:Dict[str,Any])->Dict[str,Any]:
9295
return {k:vfork,vindata.items()ifvisnotNone}
96+
97+
98+
defwarn(
99+
*,
100+
message:str,
101+
category:Optional[Type]=None,
102+
source:Optional[Any]=None,
103+
)->None:
104+
"""This `warnings.warn` wrapper function attempts to show the location causing the
105+
warning in the user code that called the library.
106+
107+
It does this by walking up the stack trace to find the first frame located outside
108+
the `gitlab/` directory. This is helpful to users as it shows them their code that
109+
is causing the warning.
110+
"""
111+
# Get `stacklevel` for user code so we indicate where issue is in
112+
# their code.
113+
pg_dir=os.path.abspath(os.path.dirname(__file__))
114+
stack=traceback.extract_stack()
115+
stacklevel=1
116+
forstacklevel,frameinenumerate(reversed(stack),start=1):
117+
frame_dir=os.path.abspath(os.path.dirname(frame.filename))
118+
ifnotframe_dir.startswith(pg_dir):
119+
break
120+
warnings.warn(
121+
message=message,category=category,stacklevel=stacklevel,source=source
122+
)

‎gitlab/v4/objects/artifacts.py‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
GitLab API:
33
https://docs.gitlab.com/ee/api/job_artifacts.html
44
"""
5-
importwarnings
65
fromtypingimportAny,Callable,Optional,TYPE_CHECKING
76

87
importrequests
@@ -34,10 +33,12 @@ def __call__(
3433
*args:Any,
3534
**kwargs:Any,
3635
)->Optional[bytes]:
37-
warnings.warn(
38-
"The project.artifacts() method is deprecated and will be "
39-
"removed in a future version. Use project.artifacts.download() instead.\n",
40-
DeprecationWarning,
36+
utils.warn(
37+
message=(
38+
"The project.artifacts() method is deprecated and will be removed in a "
39+
"future version. Use project.artifacts.download() instead.\n"
40+
),
41+
category=DeprecationWarning,
4142
)
4243
returnself.download(
4344
*args,

‎gitlab/v4/objects/projects.py‎

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
importwarnings
21
fromtypingimportAny,Callable,cast,Dict,List,Optional,TYPE_CHECKING,Union
32

43
importrequests
@@ -548,10 +547,12 @@ def transfer(self, to_namespace: Union[int, str], **kwargs: Any) -> None:
548547

549548
@cli.register_custom_action("Project", ("to_namespace",))
550549
deftransfer_project(self,*args:Any,**kwargs:Any)->None:
551-
warnings.warn(
552-
"The project.transfer_project() method is deprecated and will be "
553-
"removed in a future version. Use project.transfer() instead.",
554-
DeprecationWarning,
550+
utils.warn(
551+
message=(
552+
"The project.transfer_project() method is deprecated and will be "
553+
"removed in a future version. Use project.transfer() instead."
554+
),
555+
category=DeprecationWarning,
555556
)
556557
returnself.transfer(*args,**kwargs)
557558

@@ -562,10 +563,12 @@ def artifact(
562563
*args:Any,
563564
**kwargs:Any,
564565
)->Optional[bytes]:
565-
warnings.warn(
566-
"The project.artifact() method is deprecated and will be "
567-
"removed in a future version. Use project.artifacts.raw() instead.",
568-
DeprecationWarning,
566+
utils.warn(
567+
message=(
568+
"The project.artifact() method is deprecated and will be "
569+
"removed in a future version. Use project.artifacts.raw() instead."
570+
),
571+
category=DeprecationWarning,
569572
)
570573
returnself.artifacts.raw(*args,**kwargs)
571574

‎tests/unit/test_utils.py‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
# along with this program. If not, see <http://www.gnu.org/licenses/>.
1717

1818
importjson
19+
importwarnings
1920

2021
fromgitlabimportutils
2122

@@ -76,3 +77,20 @@ def test_json_serializable(self):
7677

7778
obj=utils.EncodedId("we got/a/path")
7879
assert'"we%20got%2Fa%2Fpath"'==json.dumps(obj)
80+
81+
82+
classTestWarningsWrapper:
83+
deftest_warn(self):
84+
warn_message="short and stout"
85+
warn_source="teapot"
86+
87+
withwarnings.catch_warnings(record=True)ascaught_warnings:
88+
utils.warn(message=warn_message,category=UserWarning,source=warn_source)
89+
assertlen(caught_warnings)==1
90+
warning=caught_warnings[0]
91+
# File name is this file as it is the first file outside of the `gitlab/` path.
92+
assert__file__==warning.filename
93+
assertwarning.category==UserWarning
94+
assertisinstance(warning.message,UserWarning)
95+
assertwarn_message==str(warning.message)
96+
assertwarn_source==warning.source

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp