[BE]: Enable ruff SLOT checks (#146276)

This enables a check that which a class which only inherits from immutable classes like str, tuple, and NamedTuple, also defined `__slots__` so they don't allocate memory unnecessarily. This also ensure contributors think about how they define their classes with subclass NamedTuples and str, of which we have many in our codebase

Pull Request resolved: https://github.com/pytorch/pytorch/pull/146276
Approved by: https://github.com/aorenste
This commit is contained in:
Aaron Gokaslan
2025-02-04 19:18:21 +00:00
committed by PyTorch MergeBot
parent 3525b834f0
commit 7f65a20884
16 changed files with 32 additions and 7 deletions

View File

@ -40,6 +40,8 @@ T = TypeVar("T", bound="Module")
class _IncompatibleKeys(
namedtuple("IncompatibleKeys", ["missing_keys", "unexpected_keys"]),
):
__slots__ = ()
def __repr__(self):
if not self.missing_keys and not self.unexpected_keys:
return "<All keys matched successfully>"