Skip to content

warnings.filterwarnings do not allow filtering warning that starts with empty line #114426

Open
@Czaki

Description

@Czaki

Bug report

Bug description:

# Add a code block here, if required

I have found that I cannot filter warnings that start with an empty line. Like this warning in pandas: https://github.com/pandas-dev/pandas/blob/488f6bd5c08111603549d42effc6dbb7fec935f0/pandas/__init__.py#L221-L231

On a project that I work (napari) we have a rule "error:::napari" in pytest that changes all warning into errors. If it is possible, then we solve warnings immediately, but some of them needs to be ignored (like linked one).

Based on documentation, I assume that rule "ignore:Pyarrow will become a required dependency of pandas in the next major release of pandas.*:DeprecationWarning", should work. But it don't.

Rule is effectively calling warnings.filterwarnings("ignore", "Pyarrow will become a required dependency of pandas in the next major release of pandas.*", DeprecationWarning).

I have found that in filterwarnings the message is changed to regexp using only re.IGNORECASE flag

message = re.compile(message, re.I)
without re.MULTILINE flag.

And then the match of the regexp is used

if ((msg is None or msg.match(text)) and
.

Based on my knowledge, the problem could be solved by either adding re.MULTILINE to compile flags or using msg.search during filtering.

Which solution is preferred? I could make a PR if someone point which one I should select.

CPython versions tested on:

3.11

Operating systems tested on:

Linux

Metadata

Metadata

Assignees

No one assigned

    Labels

    type-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions