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

chore: create a customwarnings.warn wrapper#1882

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
nejch merged 1 commit intomainfromjlvillal/custom_warn
Feb 6, 2022

Conversation

@JohnVillalovos
Copy link
Member

Create a customwarnings.warn wrapper that will walk the stack trace
to find the first frame outside of thegitlab/ path to print the
warning against. This will make it easier for users to find where in
their code the error is generated from

@codecov-commenter
Copy link

codecov-commenter commentedFeb 5, 2022
edited
Loading

Codecov Report

Merging#1882 (6ca9aa2) intomain (4cb7d92) willincrease coverage by0.02%.
The diff coverage is100.00%.

@@            Coverage Diff             @@##             main    #1882      +/-   ##==========================================+ Coverage   92.50%   92.52%   +0.02%==========================================  Files          78       78                Lines        4871     4885      +14     ==========================================+ Hits         4506     4520      +14  Misses        365      365
FlagCoverage Δ
cli_func_v481.74% <90.47%> (+0.05%)⬆️
py_func_v480.26% <28.57%> (-0.17%)⬇️
unit83.39% <95.23%> (+0.04%)⬆️

Flags with carried forward coverage won't be shown.Click here to find out more.

Impacted FilesCoverage Δ
gitlab/__init__.py100.00% <100.00%> (ø)
gitlab/utils.py91.30% <100.00%> (+4.20%)⬆️
gitlab/v4/objects/artifacts.py100.00% <100.00%> (ø)
gitlab/v4/objects/projects.py88.88% <100.00%> (-0.04%)⬇️

@JohnVillalovos
Copy link
MemberAuthor

JohnVillalovos commentedFeb 5, 2022
edited
Loading

Test script:

$ cat show-warning.py#!/usr/bin/python3 -ttuimport gitlabx = gitlab.OWNER_ACCESS

With this PR, warning shows against the script:

/home/python-gitlab/./show-warning.py:4: DeprecationWarning:Direct access to 'gitlab.OWNER_ACCESS' is deprecated and will be removed in a future major python-gitlab release. Please use 'gitlab.const.OWNER_ACCESS' instead. (python-gitlab: /home/python-gitlab/gitlab/__init__.py:44)  x = gitlab.OWNER_ACCESS

Without this PR, warning shows againstgitlab/__init__.py:

/home/python-gitlab/gitlab/__init__.py:43:DeprecationWarning:Directaccessto'gitlab.OWNER_ACCESS'isdeprecatedandwillberemovedinafuturemajorpython-gitlabrelease.Pleaseuse'gitlab.const.OWNER_ACCESS'instead.warnings.warn(

Also tested the REPL with the PR:

$ pythonPython 3.10.2 (main, Jan 17 2022, 00:00:00) [GCC 11.2.1 20211203 (Red Hat 11.2.1-7)] on linuxType "help", "copyright", "credits" or "license" for more information.>>> import gitlab>>> x = gitlab.OWNER_ACCESS<stdin>:1: DeprecationWarning:Direct access to 'gitlab.OWNER_ACCESS' is deprecated and will be removed in a future major python-gitlab release. Please use 'gitlab.const.OWNER_ACCESS' instead.>>>

@nejch
Copy link
Member

Thanks@JohnVillalovos. If I understand correctly you want to show the caller and not the source in our library when showing warnings. Wouldn't this achieve the goal without custom wrappers?

warnings.warn(f"\nDirect access to 'gitlab.{name}' is deprecated and will be "f"removed in a future major python-gitlab release. Please "f"use 'gitlab.const.{name}' instead.",DeprecationWarning,stacklevel=2,        )

Let me know if that works.

@JohnVillalovos
Copy link
MemberAuthor

Thanks@JohnVillalovos. If I understand correctly you want to show the caller and not the source in our library when showing warnings. Wouldn't this achieve the goal without custom wrappers?

warnings.warn(f"\nDirect access to 'gitlab.{name}' is deprecated and will be "f"removed in a future major python-gitlab release. Please "f"use 'gitlab.const.{name}' instead.",DeprecationWarning,stacklevel=2,        )

Let me know if that works.

It works but have to determine whatstacklevel is for each function. And if somehow the function got moved it would change where in the stack it is and then need to update that value. That is why I went with the wrapper which determines it automatically. I figure better to let the computer keep track of it than us manually doing it.

Some of these requirestacklevel=2 and some requirestacklevel=3.

@nejch
Copy link
Member

It works but have to determine whatstacklevel is for each function. And if somehow the function got moved it would change where in the stack it is and then need to update that value. That is why I went with the wrapper which determines it automatically. I figure better to let the computer keep track of it than us manually doing it.

Some of these requirestacklevel=2 and some requirestacklevel=3.

Ahh I see, ok, that wasn't clear to me at first glance. I was hoping it was a simpler one-time thing and we'd avoid having a heated debate about required keyword-only arguments but now we'll have to have it 😅

JohnVillalovos reacted with laugh emoji

Copy link
Member

@nejchnejch left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Just had some comments@JohnVillalovos after the explanation now :)

@JohnVillalovosJohnVillalovosforce-pushed thejlvillal/custom_warn branch 2 times, most recently from7802ade to75c5089CompareFebruary 6, 2022 18:47
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
@nejchnejch merged commit5beda3b intomainFeb 6, 2022
@nejchnejch deleted the jlvillal/custom_warn branchFebruary 6, 2022 22:42
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

@nejchnejchAwaiting requested review from nejch

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

4 participants

@JohnVillalovos@codecov-commenter@nejch

[8]ページ先頭

©2009-2025 Movatter.jp