Switched from parameter in can_cast to from_. (#126030)

Fixes #126012.

`from` is a reserved keyword in Python, thus we can't make the C++ impl available with `from` as function parameter. This PR changes the name to `from_` and also adjusts the docs.

If we want to preserve backwards compatibility, we can leave the C++ name as-is and only fix the docs. However, `torch.can_cast(from_=torch.int, to=torch.int)` won't work then.

Pull Request resolved: https://github.com/pytorch/pytorch/pull/126030
Approved by: https://github.com/albanD
This commit is contained in:
Tobias Ringwald 2024-05-16 20:58:24 +00:00 committed by PyTorch MergeBot
parent 82c66bc41a
commit 4f1a56cd42
4 changed files with 7 additions and 5 deletions

View File

@ -191,8 +191,8 @@ ScalarType result_type(const Scalar& scalar1, const Scalar& scalar2) {
return result_type(state); return result_type(state);
} }
bool can_cast(const at::ScalarType from, const at::ScalarType to) { bool can_cast(const at::ScalarType from_, const at::ScalarType to) {
return at::canCast(from, to); return at::canCast(from_, to);
} }
ScalarType promote_types(ScalarType type1, ScalarType type2) { ScalarType promote_types(ScalarType type1, ScalarType type2) {

View File

@ -7714,7 +7714,7 @@
- func: result_type.Scalar_Scalar(Scalar scalar1, Scalar scalar2) -> ScalarType - func: result_type.Scalar_Scalar(Scalar scalar1, Scalar scalar2) -> ScalarType
- func: can_cast(ScalarType from, ScalarType to) -> bool - func: can_cast(ScalarType from_, ScalarType to) -> bool
variants: function variants: function
- func: promote_types(ScalarType type1, ScalarType type2) -> ScalarType - func: promote_types(ScalarType type1, ScalarType type2) -> ScalarType

View File

@ -140,6 +140,8 @@ ALLOW_LIST = [
("onednn::qconv2d_pointwise", datetime.date(2024, 12, 31)), ("onednn::qconv2d_pointwise", datetime.date(2024, 12, 31)),
("onednn::qconv3d_pointwise", datetime.date(2024, 12, 31)), ("onednn::qconv3d_pointwise", datetime.date(2024, 12, 31)),
("onednn::qconv2d_pointwise.binary", datetime.date(2024, 12, 31)), ("onednn::qconv2d_pointwise.binary", datetime.date(2024, 12, 31)),
# BC-breaking change in can_cast signature: 'from' -> 'from_'
("aten::can_cast", datetime.date(2024, 5, 31)),
] ]
ALLOW_LIST_COMPILED = [ ALLOW_LIST_COMPILED = [

View File

@ -2195,13 +2195,13 @@ Example::
add_docstr( add_docstr(
torch.can_cast, torch.can_cast,
r""" r"""
can_cast(from, to) -> bool can_cast(from_, to) -> bool
Determines if a type conversion is allowed under PyTorch casting rules Determines if a type conversion is allowed under PyTorch casting rules
described in the type promotion :ref:`documentation <type-promotion-doc>`. described in the type promotion :ref:`documentation <type-promotion-doc>`.
Args: Args:
from (dtype): The original :class:`torch.dtype`. from\_ (dtype): The original :class:`torch.dtype`.
to (dtype): The target :class:`torch.dtype`. to (dtype): The target :class:`torch.dtype`.
Example:: Example::