[Dynamo][Better Engineering] Type coverage for torch/_dynamo/utils.py (#159580)

As part of better engineering effort, we would like to improve out type support to improve dev experience in dynamo

This PR adds strict typing support to `torch/_dynamo/utils.py`

Running
```
mypy torch/_dynamo/utils.py --linecount-report /tmp/coverage_log
```

| -------- | Lines Annotated | Lines Total | % lines covered | Funcs Annotated | Funcs Total | % funcs covered |
| -------- | ------- | -------- | ------- | ------- | ------- | ------- |
| Main  |  2163 | 4792 | 45.14% | 121 | 268 | 45.15% |
| This PR | 4818 | 4818 | 100.00% | 268 | 268 | 100.00% |
| Delta    | +2655 | +26 | +54.84% | +147 | 0 | +54.85% |

Pull Request resolved: https://github.com/pytorch/pytorch/pull/159580
Approved by: https://github.com/williamwen42
This commit is contained in:
Lucas Kabela
2025-08-04 21:51:48 +00:00
committed by PyTorch MergeBot
parent 510e8b4ae0
commit a7f3bdf550
2 changed files with 313 additions and 267 deletions

View File

@ -15,8 +15,9 @@ and recreate specific program states.
import dataclasses
from dataclasses import field
from io import BufferedReader, BufferedWriter
from types import CellType, CodeType, ModuleType
from typing import Any, IO
from typing import Any, IO, Union
from typing_extensions import Self
from torch.utils._import_utils import import_dill
@ -51,12 +52,12 @@ class ExecutionRecord:
builtins: dict[str, Any] = field(default_factory=dict)
code_options: dict[str, Any] = field(default_factory=dict)
def dump(self, f: IO[str]) -> None:
def dump(self, f: Union[IO[str], BufferedWriter]) -> None:
assert dill is not None, "replay_record requires `pip install dill`"
dill.dump(self, f)
@classmethod
def load(cls, f: IO[bytes]) -> Self:
def load(cls, f: Union[IO[bytes], BufferedReader]) -> Self:
assert dill is not None, "replay_record requires `pip install dill`"
return dill.load(f)

File diff suppressed because it is too large Load Diff