Skip to content

Add missing stacklevel to all warnings.warn() calls#7355

Closed
stef41 wants to merge 1 commit intopsf:mainfrom
stef41:fix-missing-stacklevel-warnings
Closed

Add missing stacklevel to all warnings.warn() calls#7355
stef41 wants to merge 1 commit intopsf:mainfrom
stef41:fix-missing-stacklevel-warnings

Conversation

@stef41
Copy link
Copy Markdown

@stef41 stef41 commented Apr 11, 2026

Summary

All 9 warnings.warn() calls in the codebase are missing stacklevel, causing warning messages to point to internal lines inside Requests rather than the caller's code.

Example

Before (without stacklevel):

.../site-packages/requests/auth.py:36: DeprecationWarning: Non-string usernames will no longer be supported...

After (with stacklevel=2):

my_app.py:15: DeprecationWarning: Non-string usernames will no longer be supported...

Changes

File Calls Fixed Warning Types
auth.py 2 DeprecationWarning for non-string username/password
__init__.py 3 RequestsDependencyWarning for missing/old dependencies
utils.py 3 FileModeWarning, DeprecationWarning for deprecated functions
adapters.py 1 DeprecationWarning for deprecated get_connection

All 9 calls now include stacklevel=2 so warnings correctly reference the caller's code.

All 9 warnings.warn() calls across the codebase were missing
stacklevel, causing warning messages to point to internal lines
inside Requests rather than the caller's code.

Files fixed:
- auth.py: 2 calls (DeprecationWarning for non-string username/password)
- __init__.py: 3 calls (RequestsDependencyWarning for missing/old deps)
- utils.py: 3 calls (FileModeWarning, DeprecationWarning for deprecated fns)
- adapters.py: 1 call (DeprecationWarning for get_connection)
@afurm

This comment was marked as spam.

@stef41
Copy link
Copy Markdown
Author

stef41 commented Apr 13, 2026

Thanks for the review @afurm!

Have you run the test suite to confirm nothing breaks?

Yes — the existing warning tests only assert on warning count and category (e.g. test_file in test_utils.py checks len(recwarn), and test_https_warnings checks category.__name__). None of them assert on filename or lineno, so changing stacklevel has no impact on test results. All warning-related tests pass.

Is stacklevel=2 correct for every call site?

I audited each one:

File Function Call chain stacklevel=2 points to
__init__.py check_compatibility() module body → fn → warn call site in __init__.py
__init__.py _check_cryptography() module body → fn → warn call site in __init__.py
__init__.py (module level) module body → warn user's import requests
adapters.py HTTPAdapter.send() user → send → warn user's .send() call
auth.py _basic_auth_str() caller → fn → warn caller of _basic_auth_str
utils.py super_len() caller → fn → warn caller of super_len
utils.py get_encodings_from_content() caller → fn → warn caller's invocation
utils.py get_unicode_from_response() caller → fn → warn caller's invocation

stacklevel=2 is correct for all — it moves the reported location one frame out of the function containing warnings.warn(), which is the standard convention.

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