Skip to content

Commit

Permalink
fix: better stacklevel for warnings (#487)
Browse files Browse the repository at this point in the history
* chore: update pre-commit hooks

updates:
- [github.com/charliermarsh/ruff-pre-commit: v0.0.255 → v0.0.257](astral-sh/ruff-pre-commit@v0.0.255...v0.0.257)
- [github.com/nbQA-dev/nbQA: 1.6.3 → 1.6.4](nbQA-dev/nbQA@1.6.3...1.6.4)

* fix: set the stacklevel for warnings

Signed-off-by: Henry Schreiner <henryschreineriii@gmail.com>

* chore: bump Ruff once more

Signed-off-by: Henry Schreiner <henryschreineriii@gmail.com>

* tests: lock seed for numpy

Signed-off-by: Henry Schreiner <henryschreineriii@gmail.com>

---------

Signed-off-by: Henry Schreiner <henryschreineriii@gmail.com>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Henry Schreiner <henryschreineriii@gmail.com>
  • Loading branch information
pre-commit-ci[bot] and henryiii committed Mar 23, 2023
1 parent b5bd0dc commit ec6707b
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 6 deletions.
4 changes: 2 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ repos:
- id: trailing-whitespace

- repo: https://github.com/charliermarsh/ruff-pre-commit
rev: v0.0.255
rev: v0.0.258
hooks:
- id: ruff
args: ["--fix", "--show-fixes"]
Expand All @@ -35,7 +35,7 @@ repos:

# Notebook formatting
- repo: https://github.com/nbQA-dev/nbQA
rev: 1.6.3
rev: 1.6.4
hooks:
- id: nbqa-ruff
additional_dependencies: [ruff==0.0.253]
Expand Down
2 changes: 1 addition & 1 deletion src/hist/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,6 @@ def __dir__() -> tuple[str, ...]:
def __getattr__(name: str) -> ModuleType:
if name == "axes":
msg = f"Misspelling error, '{name}' should be 'axis'"
warnings.warn(msg)
warnings.warn(msg, stacklevel=2)
return axis
raise AttributeError(f"module {__name__} has no attribute {name}")
2 changes: 1 addition & 1 deletion src/hist/axestuple.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def name(self, values: Iterable[str]) -> None:
disallowed_warning = (
f"{ax.name} is a protected keyword and cannot be used as axis name"
)
warnings.warn(disallowed_warning)
warnings.warn(disallowed_warning, stacklevel=2)

valid_names = [ax.name for ax in self if ax.name]
if len(valid_names) != len(set(valid_names)):
Expand Down
4 changes: 2 additions & 2 deletions src/hist/basehist.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ def __init__(
storage = getattr(bh.storage, storage_str)()
elif isinstance(storage, type):
msg = f"Please use '{storage.__name__}()' instead of '{storage.__name__}'"
warnings.warn(msg)
warnings.warn(msg, stacklevel=2)
storage = storage()

super().__init__(*args, storage=storage, metadata=metadata) # type: ignore[call-overload]
Expand All @@ -104,7 +104,7 @@ def __init__(
disallowed_warning = (
f"{ax.name} is a protected keyword and cannot be used as axis name"
)
warnings.warn(disallowed_warning)
warnings.warn(disallowed_warning, stacklevel=2)

valid_names = [ax.name for ax in self.axes if ax.name]
if len(valid_names) != len(set(valid_names)):
Expand Down
2 changes: 2 additions & 0 deletions tests/test_stacks.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

from hist import Hist, NamedHist, Stack, axis

np.random.seed(42)

# different histograms here!
reg_ax = axis.Regular(10, 0, 1)
boo_ax = axis.Boolean()
Expand Down

0 comments on commit ec6707b

Please sign in to comment.