Skip to content

Commit 79a4023

Browse files
committed
refactor: fix deprecation warnings and typo in encoders
- Remove module warning, use lazy imports, fix stacklevels - Rename calc_pariwise_coo_indices → calc_pairwise_coo_indices - Zero warnings for normal usage, proper warnings for deprecated code
1 parent 7c1ef7a commit 79a4023

5 files changed

Lines changed: 96 additions & 78 deletions

File tree

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,44 @@
11
# Import from the new pairwise_encoder module (recommended)
22
from .pairwise_encoder import PairwiseEncoder, calc_pairwise_coo_indices_nd
33

4-
# Import from the deprecated pairwise_voxel_encoder module for backward compatibility
5-
from .pairwise_voxel_encoder import PairwiseVoxelEncoder, calc_pariwise_coo_indices
4+
5+
def __getattr__(name):
6+
"""Lazy-load deprecated aliases to avoid import-time warnings."""
7+
if name == "PairwiseVoxelEncoder":
8+
# Import without warning here - the class itself will warn when instantiated
9+
from .pairwise_voxel_encoder import PairwiseVoxelEncoder
10+
11+
return PairwiseVoxelEncoder
12+
elif name == "calc_pairwise_coo_indices":
13+
import warnings
14+
15+
warnings.warn(
16+
"calc_pairwise_coo_indices is deprecated and will be removed in a future version. "
17+
"Use calc_pairwise_coo_indices_nd instead.",
18+
DeprecationWarning,
19+
stacklevel=2,
20+
)
21+
from .pairwise_encoder import calc_pairwise_coo_indices
22+
23+
return calc_pairwise_coo_indices
24+
elif name == "calc_pariwise_coo_indices":
25+
import warnings
26+
27+
warnings.warn(
28+
"calc_pariwise_coo_indices (typo) is deprecated and will be removed in a future version. "
29+
"Use calc_pairwise_coo_indices_nd instead. "
30+
"Note: This function name contains a typo; use calc_pairwise_coo_indices for the corrected spelling.",
31+
DeprecationWarning,
32+
stacklevel=2,
33+
)
34+
from .pairwise_encoder import calc_pariwise_coo_indices
35+
36+
return calc_pariwise_coo_indices
37+
raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
38+
639

740
__all__ = [
841
# New recommended classes and functions
942
"PairwiseEncoder",
1043
"calc_pairwise_coo_indices_nd",
11-
# Deprecated but maintained for backward compatibility
12-
"PairwiseVoxelEncoder",
13-
"calc_pariwise_coo_indices",
1444
]

torchsparsegradutils/encoders/pairwise_encoder.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -505,7 +505,7 @@ def calc_pairwise_coo_indices_nd(
505505
return indices
506506

507507

508-
def calc_pariwise_coo_indices(
508+
def calc_pairwise_coo_indices(
509509
radius: float,
510510
volume_shape: Tuple[int, int, int, int],
511511
diag: bool = False,
@@ -548,13 +548,17 @@ def calc_pariwise_coo_indices(
548548
"""
549549
# Validate 4D shape for backward compatibility
550550
if not (len(volume_shape) == 4 and all(isinstance(dim, int) and dim > 0 for dim in volume_shape)):
551-
raise ValueError("volume_shape must be a 4D tuple of positive integers for backward compatibility")
551+
raise ValueError("`volume_shape` must be a 4D tuple of positive integers, representing [C, H, D, W]")
552552

553553
out = calc_pairwise_coo_indices_nd(radius, volume_shape, diag, upper, channel_voxel_relation, dtype, device)
554554
# Narrow key type for static checker (all offsets have length 4 here)
555555
return {tuple(k): v for k, v in out.items()} # type: ignore[return-value]
556556

557557

558+
# Keep the typo version for backward compatibility
559+
calc_pariwise_coo_indices = calc_pairwise_coo_indices # type: ignore[misc]
560+
561+
558562
class PairwiseEncoder(torch.nn.Module):
559563
r"""Encode pairwise spatial–channel neighborhoods as sparse tensors.
560564

torchsparsegradutils/encoders/pairwise_voxel_encoder.py

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,17 @@
33

44
import torch
55

6-
# Deprecation warning for the entire module
7-
warnings.warn(
8-
"The 'pairwise_voxel_encoder' module is deprecated and will be removed in a future version. "
9-
"Please use 'pairwise_encoder' instead, which provides the same functionality with support for arbitrary N-dimensional spatial relationships.",
10-
DeprecationWarning,
11-
stacklevel=2,
12-
)
13-
146
# Import everything from the new module for backward compatibility
7+
from .pairwise_encoder import calc_pariwise_coo_indices # typo version for backward compatibility
158
from .pairwise_encoder import (
169
PairwiseEncoder,
1710
_gen_coords,
1811
_gen_coords_nd,
1912
_gen_offsets,
2013
_gen_offsets_nd,
2114
_trim_nd,
15+
calc_pairwise_coo_indices,
2216
calc_pairwise_coo_indices_nd,
23-
calc_pariwise_coo_indices,
2417
)
2518

2619

@@ -105,7 +98,7 @@ def __init__(
10598
"PairwiseVoxelEncoder is deprecated and will be removed in a future version. "
10699
"Use PairwiseEncoder instead, which supports arbitrary N-dimensional spatial relationships.",
107100
DeprecationWarning,
108-
stacklevel=2,
101+
stacklevel=3,
109102
)
110103

111104
# Validate input for backward compatibility

0 commit comments

Comments
 (0)