Most warnings don't provide a useful stacklevel
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 |
Revisions 98756d3c97f553f 7abf22e3e16a357 dac60f0b02 and 159acad64e01010 cded01c9c6dba84 9178e929e5 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 beautifulsoup/ test_warning_ stacklevel. py:5 beautifulsoup/ test_warning_ stacklevel. py:6 beautifulsoup/ test_warning_ stacklevel. py:25
# /home/user/
# /home/user/
# ...
# /home/user/
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.