Skip to content

chore: create a custom warnings.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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 6, 2022
Merged

Conversation

JohnVillalovos
Copy link
Member

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

@JohnVillalovos JohnVillalovos requested a review from nejch February 5, 2022 18:42
@codecov-commenter
Copy link

codecov-commenter commented Feb 5, 2022

Codecov Report

Merging #1882 (6ca9aa2) into main (4cb7d92) will increase coverage by 0.02%.
The diff coverage is 100.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              
Flag Coverage Δ
cli_func_v4 81.74% <90.47%> (+0.05%) ⬆️
py_func_v4 80.26% <28.57%> (-0.17%) ⬇️
unit 83.39% <95.23%> (+0.04%) ⬆️

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

Impacted Files Coverage Δ
gitlab/__init__.py 100.00% <100.00%> (ø)
gitlab/utils.py 91.30% <100.00%> (+4.20%) ⬆️
gitlab/v4/objects/artifacts.py 100.00% <100.00%> (ø)
gitlab/v4/objects/projects.py 88.88% <100.00%> (-0.04%) ⬇️

@JohnVillalovos
Copy link
Member Author

JohnVillalovos commented Feb 5, 2022

Test script:

$ cat show-warning.py
#!/usr/bin/python3 -ttu
import gitlab

x = 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 against gitlab/__init__.py:

/home/python-gitlab/gitlab/__init__.py:43: 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.
  warnings.warn(

Also tested the REPL with the PR:

$ python
Python 3.10.2 (main, Jan 17 2022, 00:00:00) [GCC 11.2.1 20211203 (Red Hat 11.2.1-7)] on linux
Type "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

nejch commented Feb 6, 2022

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
Member Author

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 what stacklevel 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 require stacklevel=2 and some require stacklevel=3.

@nejch
Copy link
Member

nejch commented Feb 6, 2022

It works but have to determine what stacklevel 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 require stacklevel=2 and some require stacklevel=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 😅

Copy link
Member

@nejch nejch 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 :)

@JohnVillalovos JohnVillalovos requested a review from nejch February 6, 2022 18:25
@JohnVillalovos JohnVillalovos force-pushed the jlvillal/custom_warn branch 2 times, most recently from 7802ade to 75c5089 Compare February 6, 2022 18:47
Create a custom `warnings.warn` wrapper that will walk the stack trace
to find the first frame outside of the `gitlab/` path to print the
warning against. This will make it easier for users to find where in
their code the error is generated from
@nejch nejch merged commit 5beda3b into main Feb 6, 2022
@nejch nejch deleted the jlvillal/custom_warn branch February 6, 2022 22:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants