[BE]: Apply RUF025 dict.fromkeys preview rule (#118637)

Simplifies and optimizes dict construction using the `fromkeys` classmethod ctor. This also makes it really obvious when all the keys will have the same static value, which could be a bug if unintentional. It is also significantly faster than using a dict comprehension. The rule is in preview, but I am adding a forward fix for when it becomes stable.

Pull Request resolved: https://github.com/pytorch/pytorch/pull/118637
Approved by: https://github.com/albanD
This commit is contained in:
Aaron Gokaslan
2024-01-30 20:46:48 +00:00
committed by PyTorch MergeBot
parent e33e88e5bc
commit 1562dae62c
26 changed files with 48 additions and 74 deletions

View File

@ -1485,15 +1485,7 @@ class TestMakeTensor(TestCase):
low_inclusive, high_exclusive = {
torch.bool: (0, 2),
torch.uint8: (0, 10),
**{
signed_integral_dtype: (-9, 10)
for signed_integral_dtype in [
torch.int8,
torch.int16,
torch.int32,
torch.int64,
]
},
**dict.fromkeys([torch.int8, torch.int16, torch.int32, torch.int64], (-9, 10)),
}.get(dtype, (-9, 9))
t = torch.testing.make_tensor(10_000, dtype=dtype, device=device, low=low_inclusive, high=high_exclusive)