Skip to content

Commit

Permalink
[BE] Make legacy type storage warning point to the caller (#113601)
Browse files Browse the repository at this point in the history
`@classproperty` decorator adds another wrapper, so warning with default stacklevel (2) would  always point to the wrapper implementation rather than at callee.

For example, before this change following code
```python
import torch
print(torch.FloatStorage.dtype)
```
will produce inactionable warning:
```
/Users/nshulga/git/pytorch/pytorch/torch/_utils.py:836: UserWarning: TypedStorage is deprecated. It will be removed in the future and UntypedStorage will be the only storage class. This should only matter to you if you are using storages directly.  To access UntypedStorage directly, use tensor.untyped_storage() instead of tensor.storage()
  return self.fget.__get__(instance, owner)()

```
But after the change warning turns into:
```
/Users/nshulga/test/bar.py:2: UserWarning: TypedStorage is deprecated. It will be removed in the future and UntypedStorage will be the only storage class. This should only matter to you if you are using storages directly.  To access UntypedStorage directly, use tensor.untyped_storage() instead of tensor.storage()
  print(torch.FloatStorage.dtype)
```

Discovered while reading #109108

Pull Request resolved: #113601
Approved by: https://github.com/kit1980
  • Loading branch information
malfet authored and pytorchmergebot committed Nov 14, 2023
1 parent ffc3731 commit b3a76cc
Showing 1 changed file with 17 additions and 17 deletions.
34 changes: 17 additions & 17 deletions torch/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1168,7 +1168,7 @@ def _check_tensor_all(cond, message=None): # noqa: F811
class ByteStorage(_LegacyStorage):
@classproperty
def dtype(self):
_warn_typed_storage_removal()
_warn_typed_storage_removal(stacklevel=3)
return self._dtype

@classproperty
Expand All @@ -1178,7 +1178,7 @@ def _dtype(self):
class DoubleStorage(_LegacyStorage):
@classproperty
def dtype(self):
_warn_typed_storage_removal()
_warn_typed_storage_removal(stacklevel=3)
return self._dtype

@classproperty
Expand All @@ -1188,7 +1188,7 @@ def _dtype(self):
class FloatStorage(_LegacyStorage):
@classproperty
def dtype(self):
_warn_typed_storage_removal()
_warn_typed_storage_removal(stacklevel=3)
return self._dtype

@classproperty
Expand All @@ -1198,7 +1198,7 @@ def _dtype(self):
class HalfStorage(_LegacyStorage):
@classproperty
def dtype(self):
_warn_typed_storage_removal()
_warn_typed_storage_removal(stacklevel=3)
return self._dtype

@classproperty
Expand All @@ -1208,7 +1208,7 @@ def _dtype(self):
class LongStorage(_LegacyStorage):
@classproperty
def dtype(self):
_warn_typed_storage_removal()
_warn_typed_storage_removal(stacklevel=3)
return self._dtype

@classproperty
Expand All @@ -1218,7 +1218,7 @@ def _dtype(self):
class IntStorage(_LegacyStorage):
@classproperty
def dtype(self):
_warn_typed_storage_removal()
_warn_typed_storage_removal(stacklevel=3)
return self._dtype

@classproperty
Expand All @@ -1228,7 +1228,7 @@ def _dtype(self):
class ShortStorage(_LegacyStorage):
@classproperty
def dtype(self):
_warn_typed_storage_removal()
_warn_typed_storage_removal(stacklevel=3)
return self._dtype

@classproperty
Expand All @@ -1238,7 +1238,7 @@ def _dtype(self):
class CharStorage(_LegacyStorage):
@classproperty
def dtype(self):
_warn_typed_storage_removal()
_warn_typed_storage_removal(stacklevel=3)
return self._dtype

@classproperty
Expand All @@ -1248,7 +1248,7 @@ def _dtype(self):
class BoolStorage(_LegacyStorage):
@classproperty
def dtype(self):
_warn_typed_storage_removal()
_warn_typed_storage_removal(stacklevel=3)
return self._dtype

@classproperty
Expand All @@ -1258,7 +1258,7 @@ def _dtype(self):
class BFloat16Storage(_LegacyStorage):
@classproperty
def dtype(self):
_warn_typed_storage_removal()
_warn_typed_storage_removal(stacklevel=3)
return self._dtype

@classproperty
Expand All @@ -1268,7 +1268,7 @@ def _dtype(self):
class ComplexDoubleStorage(_LegacyStorage):
@classproperty
def dtype(self):
_warn_typed_storage_removal()
_warn_typed_storage_removal(stacklevel=3)
return self._dtype

@classproperty
Expand All @@ -1278,7 +1278,7 @@ def _dtype(self):
class ComplexFloatStorage(_LegacyStorage):
@classproperty
def dtype(self):
_warn_typed_storage_removal()
_warn_typed_storage_removal(stacklevel=3)
return self._dtype

@classproperty
Expand All @@ -1288,7 +1288,7 @@ def _dtype(self):
class QUInt8Storage(_LegacyStorage):
@classproperty
def dtype(self):
_warn_typed_storage_removal()
_warn_typed_storage_removal(stacklevel=3)
return self._dtype

@classproperty
Expand All @@ -1298,7 +1298,7 @@ def _dtype(self):
class QInt8Storage(_LegacyStorage):
@classproperty
def dtype(self):
_warn_typed_storage_removal()
_warn_typed_storage_removal(stacklevel=3)
return self._dtype

@classproperty
Expand All @@ -1308,7 +1308,7 @@ def _dtype(self):
class QInt32Storage(_LegacyStorage):
@classproperty
def dtype(self):
_warn_typed_storage_removal()
_warn_typed_storage_removal(stacklevel=3)
return self._dtype

@classproperty
Expand All @@ -1318,7 +1318,7 @@ def _dtype(self):
class QUInt4x2Storage(_LegacyStorage):
@classproperty
def dtype(self):
_warn_typed_storage_removal()
_warn_typed_storage_removal(stacklevel=3)
return self._dtype

@classproperty
Expand All @@ -1328,7 +1328,7 @@ def _dtype(self):
class QUInt2x4Storage(_LegacyStorage):
@classproperty
def dtype(self):
_warn_typed_storage_removal()
_warn_typed_storage_removal(stacklevel=3)
return self._dtype

@classproperty
Expand Down

0 comments on commit b3a76cc

Please sign in to comment.