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

resetwarnings doesn't reset warnings registry #65923

Open
pitrou opened this issue Jun 11, 2014 · 7 comments
Open

resetwarnings doesn't reset warnings registry #65923

pitrou opened this issue Jun 11, 2014 · 7 comments
Labels
3.7 (EOL) end of life 3.8 only security fixes 3.9 only security fixes 3.10 only security fixes docs Documentation in the Doc dir type-bug An unexpected behavior, bug, or error

Comments

@pitrou
Copy link
Member

pitrou commented Jun 11, 2014

BPO 21724
Nosy @ncoghlan, @pitrou, @Julian, @ericsnowcurrently, @berkerpeksag, @nanjekyejoannah
Files
  • reset_warning_registry.py: reset_warning_registry() context manager
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = None
    created_at = <Date 2014-06-11.16:11:44.072>
    labels = ['type-bug', '3.8', '3.9', '3.10', '3.7', 'docs']
    title = "resetwarnings doesn't reset warnings registry"
    updated_at = <Date 2021-10-23.21:12:30.228>
    user = 'https://github.com/pitrou'

    bugs.python.org fields:

    activity = <Date 2021-10-23.21:12:30.228>
    actor = 'yaubi'
    assignee = 'docs@python'
    closed = False
    closed_date = None
    closer = None
    components = ['Documentation']
    creation = <Date 2014-06-11.16:11:44.072>
    creator = 'pitrou'
    dependencies = []
    files = ['40031']
    hgrepos = []
    issue_num = 21724
    keywords = []
    message_count = 7.0
    messages = ['220282', '220328', '220517', '220547', '247432', '369809', '404890']
    nosy_count = 11.0
    nosy_names = ['ncoghlan', 'pitrou', 'Arfrever', 'stefanv', 'docs@python', 'Julian', 'eric.snow', 'eli.collins', 'berker.peksag', 'robertmuil', 'nanjekyejoannah']
    pr_nums = []
    priority = 'normal'
    resolution = None
    stage = None
    status = 'open'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue21724'
    versions = ['Python 3.5', 'Python 3.6', 'Python 3.7', 'Python 3.8', 'Python 3.9', 'Python 3.10']

    @pitrou
    Copy link
    Member Author

    pitrou commented Jun 11, 2014

    There seems to be no (official) way to reset the warnings registry; in particular, resetwarnings() doesn't reset it. It makes it difficult to get repeatable warning messages, e.g. at the command prompt, because the shortcut path will silence messages which were already emitted, even if the filter have been changed to "always" in-between.

    @pitrou pitrou added the type-bug An unexpected behavior, bug, or error label Jun 11, 2014
    @berkerpeksag
    Copy link
    Member

    It makes it difficult to get repeatable warning messages, e.g. at the > command prompt, because the shortcut path will silence messages which > were already emitted, even if the filter have been changed to "always" > in-between.

    This could be related to bpo-4180.

    @pitrou
    Copy link
    Member Author

    pitrou commented Jun 14, 2014

    Related indeed, though not exactly the same issue. What I'd like is to reset (i.e. clear) the registry even when keeping the default warning scheme.

    @stefanv
    Copy link
    Mannequin

    stefanv mannequin commented Jun 14, 2014

    This can be quite painful to work around, since the warning registry is scattered all over. See, e.g.,

    https://github.com/scikit-image/scikit-image/blob/master/skimage/_shared/_warnings.py#L9

    @elicollins
    Copy link
    Mannequin

    elicollins mannequin commented Jul 26, 2015

    I'm not sure how generally applicable this is, but it might be useful as a starting point: Attached is a bit of code I've been using: it's a reset_warnings_registry() context manager, which backs up & clears the registry state for the duration of the context, then restores it afterwards.

    It's particularly useful for unittests, just create & call the __enter__() method during setUp(), and attach __exit__() via addCleanup. Each test is then gets it's own isolated warnings registry.

    @Julian
    Copy link
    Mannequin

    Julian mannequin commented May 24, 2020

    Just ran into this myself -- not sure what the intended fix is (hopefully it's "add a function that restores the warnings configuration to its defaults?" Changing resetwarnings seems likely to be not doable I assume.)

    But in the meanwhile, is a doc patch acceptable? The current documentation for resetwarnings is really deceptive, and makes it look like it does what @pitrou (and I) thought it did:

    Reset the warnings filter. This discards the effect of all previous calls to filterwarnings(), including that of the -W command line options and calls to simplefilter().

    Compare to the docstring of the function (and to what it actually does):

    """Clear the list of warning filters, so that no filters are active."""

    But there are still too many implementation details of the warnings module leaking through here -- for end users it's just "restore the warnings configuration to its defaults" (what it looks like resetwarnings should do) or "unfilter all warnings, even beyond the filters configured by default" (what it actually does).

    Is at least making the docs reflect the latter a reasonable patch to submit, to start, while what to do about the former is thought about?

    @Julian Julian mannequin added docs Documentation in the Doc dir 3.7 (EOL) end of life 3.8 only security fixes 3.9 only security fixes 3.10 only security fixes labels May 24, 2020
    @Julian Julian mannequin assigned docspython May 24, 2020
    @nanjekyejoannah
    Copy link
    Member

    @Julian Am leaning more into us applying a relevant fix, than adjusting the documentation.

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.7 (EOL) end of life 3.8 only security fixes 3.9 only security fixes 3.10 only security fixes docs Documentation in the Doc dir type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    3 participants