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

Setting bool values to lineno argument of warnings.filterwarnings() and warnings.simplefilter() works #125899

Closed
hyperkai opened this issue Oct 23, 2024 · 2 comments
Labels
pending The issue will be closed if no feedback is provided type-bug An unexpected behavior, bug, or error

Comments

@hyperkai
Copy link

hyperkai commented Oct 23, 2024

Bug report

Bug description:

Setting the float value 3.14 to lineno argument of warnings.filterwarnings() and warnings.simplefilter() gets the error message as shown below:

import warnings
                                       # ↓ ↓ ↓ ↓ ↓ ↓
warnings.filterwarnings(action="ignore", lineno=3.14) # Error
warnings.simplefilter(action="ignore", lineno=3.14) # Error
                                     # ↑ ↑ ↑ ↑ ↑ ↑
print(warnings.filters)

AssertionError: lineno must be an int >= 0

But setting the bool values True and False to lineno argument of warnings.filterwarnings() and warnings.simplefilter() works as shown below:

import warnings
                                       # ↓ ↓ ↓ ↓ ↓ ↓
warnings.filterwarnings(action="ignore", lineno=True)
warnings.filterwarnings(action="ignore", lineno=False)
                                        # ↑ ↑ ↑ ↑ ↑ ↑
                                     # ↓ ↓ ↓ ↓ ↓ ↓
warnings.simplefilter(action="ignore", lineno=True)
warnings.simplefilter(action="ignore", lineno=False)
                                      # ↑ ↑ ↑ ↑ ↑ ↑
print(warnings.filters)
# [('ignore', None, <class 'Warning'>, None, False),
#  ('ignore', None, <class 'Warning'>, None, True)]

CPython versions tested on:

3.12

Operating systems tested on:

Windows

@hyperkai hyperkai added the type-bug An unexpected behavior, bug, or error label Oct 23, 2024
@picnixz
Copy link
Contributor

picnixz commented Oct 23, 2024

bool is a subclass of int so this is not really an issue per se IMO. They can be considered as 0 or 1. I don't think we should have a dedicated if isinstance(x, bool) just to raise an appropriate error.

I don't think we need to make those functions entirely fool-proof for any kind of int-subclasses. There is a trade-off between practicability and purity IMO.

cc @ncoghlan

@picnixz picnixz added the pending The issue will be closed if no feedback is provided label Oct 23, 2024
@brettcannon
Copy link
Member

I'm going to close this as this is just part of the cost of bool being a subclass of int and there's no way we can (or will) sprinkle type checks for bool everywhere int is acceptable. Your best option is to have your linter detect this sort of thing.

@brettcannon brettcannon closed this as not planned Won't fix, can't repro, duplicate, stale Oct 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pending The issue will be closed if no feedback is provided type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

3 participants