Most warnings don't provide a useful stacklevel

Bug #1978744 reported by Jakub Kuczys
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
Beautiful Soup
Fix Released
Undecided
Unassigned

Bug Description

Most of the warnings in beautifulsoup do not declare a `stacklevel`. This is quite problematic because it makes it hard to pinpoint the location of the code (user code) that is actually the issue here as we instead get the information about the code that warns us about the problem, which isn't really useful in any way since beatifulsoup's code is not the culprit here.

Additionally, such a setup makes it impossible to suppress specific occurrences of the warning as all of the warnings point at the line in beautifulsoup's code and that line is always going to be the same for a specific type of warning. This somewhat relates to #1873787 as I would like to suppress this same warning but I would like to suppress it in such a way that it only is suppressed for the code I know is meant to work like that, not all of the code. From what I understand, this is supposed to help when someone uses bs4 incorrectly and because not all of the code in the application is going to be owned by me (this is especially true for library developers who use bs4), I don't want to hide this warning from others.

summary: - Warnings use wrong stacklevel
+ Most warnings don't provide a useful stacklevel
Changed in beautifulsoup:
status: New → Fix Committed
Revision history for this message
Leonard Richardson (leonardr) wrote :

Revisions 98756d3c97f553f7abf22e3e16a357dac60f0b02 and 159acad64e01010cded01c9c6dba849178e929e5 add appropriate stacklevel values for all warnings. I also removed some warnings altogether that pertain to versions of Python that Beautiful Soup no longer supports.

I tested this with the attached script, which triggers every warning currently in Beautiful Soup. The output should include a warning for every line of code from 5 to 25, making it easy to scan and verify the fix.

python -X dev test_warning_stacklevel.py
# /home/user/beautifulsoup/test_warning_stacklevel.py:5
# /home/user/beautifulsoup/test_warning_stacklevel.py:6
# ...
# /home/user/beautifulsoup/test_warning_stacklevel.py:25

I don't see a way to automatically test this without hard-coding the names of the test files into the tests, but I'll give it some more thought before closing this issue.

Changed in beautifulsoup:
status: Fix Committed → In Progress
Revision history for this message
Leonard Richardson (leonardr) wrote :

I improved all relevant unit tests to do a basic verification of the file that triggered the warning (if not the exact line number), and I think that's about the best I can do.

Changed in beautifulsoup:
status: In Progress → Fix Committed
Revision history for this message
Leonard Richardson (leonardr) wrote :

Fix released in version 4.11.2.

Changed in beautifulsoup:
status: Fix Committed → Fix Released
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.