Skip to content

WARN,TST check stacklevel for all warnings #47998

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

Merged
merged 23 commits into from
Aug 17, 2022
Merged
Show file tree
Hide file tree
Changes from 22 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
6f4280d
use find_stack_level everywhere
MarcoGorelli Aug 7, 2022
781fe67
fixup
MarcoGorelli Aug 7, 2022
503f4bc
pyx fixups
MarcoGorelli Aug 7, 2022
aa57ad2
fixup test_optional_dependency
MarcoGorelli Aug 8, 2022
fe06f18
fixup api
MarcoGorelli Aug 8, 2022
b18b223
set check_stacklevel=False for some tests
MarcoGorelli Aug 8, 2022
ff84081
use lru_cache for currentframe
MarcoGorelli Aug 8, 2022
feb36cc
fixup import in __init__
MarcoGorelli Aug 8, 2022
ed1e6a7
add missing imports to pyx files
MarcoGorelli Aug 8, 2022
d20c05c
add missing import
MarcoGorelli Aug 8, 2022
0e40eb7
fixup import in conversion
MarcoGorelli Aug 8, 2022
0f7400b
revert some __init__ changes
MarcoGorelli Aug 8, 2022
89b5f52
start n=1
MarcoGorelli Aug 8, 2022
591b2bb
temporarily dont check stacklevel in _check_plot_works
MarcoGorelli Aug 10, 2022
7578ab9
Merge remote-tracking branch 'upstream/main' into find-stack-level
MarcoGorelli Aug 12, 2022
87c8ac3
Merge remote-tracking branch 'upstream/main' into find-stack-level
MarcoGorelli Aug 12, 2022
1a68c9e
catch some more warnings
MarcoGorelli Aug 12, 2022
1b67679
dont check stacklevel in check_plot_works
MarcoGorelli Aug 12, 2022
9c55b19
fixup
MarcoGorelli Aug 13, 2022
139a418
Merge remote-tracking branch 'upstream/main' into find-stack-level
MarcoGorelli Aug 13, 2022
2eb9704
Merge branch 'main' into find-stack-level
MarcoGorelli Aug 14, 2022
538c37a
ignore stacklevel in check_plot_works
MarcoGorelli Aug 14, 2022
1549bb6
Merge branch 'main' into find-stack-level
MarcoGorelli Aug 17, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion doc/source/development/contributing_codebase.rst
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ Otherwise, you need to do it manually:
.. code-block:: python

import warnings
from pandas.util._exceptions import find_stack_level


def old_func():
Expand All @@ -130,7 +131,11 @@ Otherwise, you need to do it manually:
.. deprecated:: 1.1.0
Use new_func instead.
"""
warnings.warn('Use new_func instead.', FutureWarning, stacklevel=2)
warnings.warn(
'Use new_func instead.',
FutureWarning,
stacklevel=find_stack_level(inspect.currentframe()),
)
new_func()


Expand Down
12 changes: 10 additions & 2 deletions pandas/_config/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
ContextDecorator,
contextmanager,
)
import inspect
import re
from typing import (
Any,
Expand All @@ -70,6 +71,7 @@
F,
T,
)
from pandas.util._exceptions import find_stack_level


class DeprecatedOption(NamedTuple):
Expand Down Expand Up @@ -657,7 +659,11 @@ def _warn_if_deprecated(key: str) -> bool:
d = _get_deprecated_option(key)
if d:
if d.msg:
warnings.warn(d.msg, FutureWarning)
warnings.warn(
d.msg,
FutureWarning,
stacklevel=find_stack_level(inspect.currentframe()),
)
else:
msg = f"'{key}' is deprecated"
if d.removal_ver:
Expand All @@ -667,7 +673,9 @@ def _warn_if_deprecated(key: str) -> bool:
else:
msg += ", please refrain from using it."

warnings.warn(msg, FutureWarning)
warnings.warn(
msg, FutureWarning, stacklevel=find_stack_level(inspect.currentframe())
)
return True
return False

Expand Down
2 changes: 1 addition & 1 deletion pandas/_libs/interval.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ def _warning_interval(inclusive: str | None = None, closed: None | lib.NoDefault
warnings.warn(
"Argument `closed` is deprecated in favor of `inclusive`.",
FutureWarning,
stacklevel=2,
stacklevel=find_stack_level(inspect.currentframe()),
)
if closed is None:
inclusive = "right"
Expand Down
4 changes: 4 additions & 0 deletions pandas/_libs/lib.pyx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from collections import abc
from decimal import Decimal
from enum import Enum
import inspect
from typing import Literal
import warnings

Expand Down Expand Up @@ -30,6 +31,8 @@ from cython cimport (
floating,
)

from pandas.util._exceptions import find_stack_level

import_datetime()

import numpy as np
Expand Down Expand Up @@ -352,6 +355,7 @@ def fast_unique_multiple(list arrays, sort: bool = True):
"The values in the array are unorderable. "
"Pass `sort=False` to suppress this warning.",
RuntimeWarning,
stacklevel=find_stack_level(inspect.currentframe()),
)
pass

Expand Down
7 changes: 5 additions & 2 deletions pandas/_libs/parsers.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,13 @@ from csv import (
QUOTE_NONNUMERIC,
)
from errno import ENOENT
import inspect
import sys
import time
import warnings

from pandas.util._exceptions import find_stack_level

cimport cython
from cpython.bytes cimport (
PyBytes_AsString,
Expand Down Expand Up @@ -958,7 +961,7 @@ cdef class TextReader:
"Defining usecols with out of bounds indices is deprecated "
"and will raise a ParserError in a future version.",
FutureWarning,
stacklevel=6,
stacklevel=find_stack_level(inspect.currentframe()),
)

results = {}
Expand Down Expand Up @@ -1009,7 +1012,7 @@ cdef class TextReader:
warnings.warn((f"Both a converter and dtype were specified "
f"for column {name} - only the converter will "
f"be used."), ParserWarning,
stacklevel=5)
stacklevel=find_stack_level(inspect.currentframe()))
results[i] = _apply_converter(conv, self.parser, i, start, end)
continue

Expand Down
5 changes: 4 additions & 1 deletion pandas/_libs/tslib.pyx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import inspect
import warnings

cimport cython
Expand All @@ -9,6 +10,8 @@ from cpython.datetime cimport (
tzinfo,
)

from pandas.util._exceptions import find_stack_level

# import datetime C API
import_datetime()

Expand Down Expand Up @@ -843,7 +846,7 @@ cdef inline bint _parse_today_now(str val, int64_t* iresult, bint utc):
"deprecated. In a future version, this will match Timestamp('now') "
"and Timestamp.now()",
FutureWarning,
stacklevel=1,
stacklevel=find_stack_level(inspect.currentframe()),
)

return True
Expand Down
6 changes: 5 additions & 1 deletion pandas/_libs/tslibs/conversion.pyx
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import inspect

cimport cython

import warnings

import numpy as np

from pandas.util._exceptions import find_stack_level

cimport numpy as cnp
from cpython.object cimport PyObject
from numpy cimport (
Expand Down Expand Up @@ -287,7 +291,7 @@ cdef _TSObject convert_to_tsobject(object ts, tzinfo tz, str unit,
"Conversion of non-round float with unit={unit} is ambiguous "
"and will raise in a future version.",
FutureWarning,
stacklevel=1,
stacklevel=find_stack_level(inspect.currentframe()),
)

ts = cast_from_unit(ts, unit)
Expand Down
7 changes: 5 additions & 2 deletions pandas/_libs/tslibs/nattype.pyx
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import inspect
import warnings

from pandas.util._exceptions import find_stack_level

from cpython.datetime cimport (
PyDate_Check,
PyDateTime_Check,
Expand Down Expand Up @@ -135,7 +138,7 @@ cdef class _NaT(datetime):
"order to match the standard library behavior. "
"In a future version these will be considered non-comparable.",
FutureWarning,
stacklevel=1,
stacklevel=find_stack_level(inspect.currentframe()),
)
return False

Expand Down Expand Up @@ -379,7 +382,7 @@ class NaTType(_NaT):
warnings.warn(
"NaT.freq is deprecated and will be removed in a future version.",
FutureWarning,
stacklevel=1,
stacklevel=find_stack_level(inspect.currentframe()),
)
return None

Expand Down
11 changes: 7 additions & 4 deletions pandas/_libs/tslibs/offsets.pyx
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import inspect
import operator
import re
import time
import warnings

from pandas.util._exceptions import find_stack_level

cimport cython
from cpython.datetime cimport (
PyDate_Check,
Expand Down Expand Up @@ -499,7 +502,7 @@ cdef class BaseOffset:
"DateOffset.__call__ is deprecated and will be removed in a future "
"version. Use `offset + other` instead.",
FutureWarning,
stacklevel=1,
stacklevel=find_stack_level(inspect.currentframe()),
)
return self._apply(other)

Expand All @@ -509,7 +512,7 @@ cdef class BaseOffset:
f"{type(self).__name__}.apply is deprecated and will be removed "
"in a future version. Use `offset + other` instead",
FutureWarning,
stacklevel=2,
stacklevel=find_stack_level(inspect.currentframe()),
)
return self._apply(other)

Expand Down Expand Up @@ -820,15 +823,15 @@ cdef class BaseOffset:
warnings.warn(
"onOffset is a deprecated, use is_on_offset instead.",
FutureWarning,
stacklevel=1,
stacklevel=find_stack_level(inspect.currentframe()),
)
return self.is_on_offset(dt)

def isAnchored(self) -> bool:
warnings.warn(
"isAnchored is a deprecated, use is_anchored instead.",
FutureWarning,
stacklevel=1,
stacklevel=find_stack_level(inspect.currentframe()),
)
return self.is_anchored()

Expand Down
7 changes: 5 additions & 2 deletions pandas/_libs/tslibs/parsing.pyx
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
"""
Parsing functions for datetime and datetime-like strings.
"""
import inspect
import re
import time
import warnings

from pandas.util._exceptions import find_stack_level

cimport cython
from cpython.datetime cimport (
datetime,
Expand Down Expand Up @@ -214,15 +217,15 @@ cdef inline object _parse_delimited_date(str date_string, bint dayfirst):
format='MM/DD/YYYY',
dayfirst='True',
),
stacklevel=4,
stacklevel=find_stack_level(inspect.currentframe()),
)
elif not dayfirst and swapped_day_and_month:
warnings.warn(
PARSING_WARNING_MSG.format(
format='DD/MM/YYYY',
dayfirst='False (the default)',
),
stacklevel=4,
stacklevel=find_stack_level(inspect.currentframe()),
)
# In Python <= 3.6.0 there is no range checking for invalid dates
# in C api, thus we call faster C version for 3.6.1 or newer
Expand Down
5 changes: 4 additions & 1 deletion pandas/_libs/tslibs/period.pyx
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import inspect
import warnings

from pandas.util._exceptions import find_stack_level

cimport numpy as cnp
from cpython.object cimport (
Py_EQ,
Expand Down Expand Up @@ -1827,7 +1830,7 @@ cdef class _Period(PeriodMixin):
"be removed in a future version. Use "
"`per.to_timestamp(...).tz_localize(tz)` instead.",
FutureWarning,
stacklevel=1,
stacklevel=find_stack_level(inspect.currentframe()),
)

how = validate_end_alias(how)
Expand Down
11 changes: 7 additions & 4 deletions pandas/_libs/tslibs/timedeltas.pyx
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import collections
import inspect
import warnings

from pandas.util._exceptions import find_stack_level

cimport cython
from cpython.object cimport (
Py_EQ,
Expand Down Expand Up @@ -683,7 +686,7 @@ cdef inline timedelta_from_spec(object number, object frac, object unit):
"Units 'M', 'Y' and 'y' do not represent unambiguous "
"timedelta values and will be removed in a future version.",
FutureWarning,
stacklevel=3,
stacklevel=find_stack_level(inspect.currentframe()),
)

if unit == 'M':
Expand Down Expand Up @@ -1055,7 +1058,7 @@ cdef class _Timedelta(timedelta):
warnings.warn(
"Timedelta.freq is deprecated and will be removed in a future version",
FutureWarning,
stacklevel=1,
stacklevel=find_stack_level(inspect.currentframe()),
)
return None

Expand All @@ -1065,7 +1068,7 @@ cdef class _Timedelta(timedelta):
warnings.warn(
"Timedelta.is_populated is deprecated and will be removed in a future version",
FutureWarning,
stacklevel=1,
stacklevel=find_stack_level(inspect.currentframe()),
)
return self._is_populated

Expand Down Expand Up @@ -1269,7 +1272,7 @@ cdef class _Timedelta(timedelta):
warnings.warn(
"Timedelta.delta is deprecated and will be removed in a future version.",
FutureWarning,
stacklevel=1,
stacklevel=find_stack_level(inspect.currentframe()),
)
return self.value

Expand Down
Loading