Skip to content
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

Buggy depreciation warining (client_factory will be replaced by gspread.http_client types) #1348

Closed
ArturoGallettiLopez opened this issue Nov 7, 2023 · 4 comments
Assignees
Labels
Bug Need investigation This issue needs to be tested or investigated
Milestone

Comments

@ArturoGallettiLopez
Copy link

ArturoGallettiLopez commented Nov 7, 2023

I am currently cleaning up my code for version 6.0 and ran into a somewhat strange scenario

When calling service_account_from_dict in a subfunction like so:

#imports
import gspread
from gspread import service_account_from_dict
#function declare
def BugTest():
    gc = service_account_from_dict(creds1, scope)

#run
BugTest()

I get the following depreciation warning:
DeprecationWarning: [Deprecated][in version 6.0.0]: client_factory will be replaced by gspread.http_client types BugTest()

If I run the same code as a standalone I do not get any warnings and the rev notes don't seem to indicate any particular issue.

@alifeee
Copy link
Collaborator

alifeee commented Nov 7, 2023

strange indeed...

thanks for the issue :)

I do not know the subtleties of the warnings package, but my guess is that it has something to do with the stacklevel kwarg.

This is how we emit a warning:

gspread/gspread/utils.py

Lines 926 to 930 in 67759c7

warnings.warn(
DEPRECATION_WARNING_TEMPLATE.format(v_deprecated=version, msg_deprecated=msg),
DeprecationWarning,
4, # showd the 4th stack: [1]:current->[2]:deprecation_warning->[3]:<gspread method/function>->[4]:<user's code>
)

The third argument to warnings.warn( is stacklevel (see warnings.warn docs). I'm not fully sure what this means, but it seems that it only warns if the stack level is four or more (or something).

In your case, the stack level increases when you wrap service_account_from_dict in a function, which allows the warning to show. Maybe we need to increase/decrease the stacklevel argument so the warning always shows. maybe @lavigne958 can provide more info, having implemented the warnings.

As an addendum, I have made a PR #1349 which disables this warning if you have not set the client_factory kwarg in service_account_from_dict. In your case, the warning is not very useful, since you do not use the client_factory kwarg. This should hide the warning for you when 5.12.1 releases.

For now, as well, if you want to hide warnings, you can set the SILENCE_WARNINGS_ENV_KEY environment variable to "1" to disable warnings.

@alifeee alifeee added Bug Need investigation This issue needs to be tested or investigated labels Nov 7, 2023
@alifeee alifeee added this to the 5.12.1 milestone Nov 7, 2023
@ArturoGallettiLopez
Copy link
Author

I'm going to create the gc object one level to take care of it in my code. appreciate the prompt response.

@lavigne958
Copy link
Collaborator

lavigne958 commented Nov 7, 2023

Hi thank you for raising this issue.

Yes this is due to the stack level.
The stack level is: the number of function calling the current code.
Stack levels start at 0 from the line of code in the warning package.

Meaning:
in the deprecation warning I set a stack to 4, so when the warning is emitted, it prints as well the code where the user is using some gspread code.
As mentioned in the comment: stack 0 -> is warning package, 1 -> itl function in gspread, up to 3 (or 4 in 1 based notation) which the line of code from the user. so it prints the file/line where someone is calling a gpsread function which triggers the warning..... in most of the case ! sometimes it could be 1 stack above or 1 lower :-(

That should do for now it's only informative, we'll release 6.0.0 soon (:christmas_tree: :question: :upside_down_face: ) so it won't last.

@alifeee
Copy link
Collaborator

alifeee commented Nov 14, 2023

this is fixed by #1349 and will be included in the next release, 5.12.1

@alifeee alifeee closed this as completed Nov 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Need investigation This issue needs to be tested or investigated
Projects
None yet
Development

No branches or pull requests

3 participants