[Dynamo][Better Engineering] Add type coverage to decorators (#158509)

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

This PR adds strict typing support to an important file in dynamo, `decorators.py`

NOTE: Untyped fns are because there is a conflict with `__init__.py` in compiler so we can't type these at this time

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

| -------- | Lines Unannotated | Lines Total | % lines covered | Funcs Unannotated | Funcs Total | % funcs covered |
| -------- | ------- | -------- | ------- | ------- | ------- | ------- |
| Main  |  209 | 908 | 23.02% | 9 | 39 | 23.08% |
| This PR | 870 | 943 | 100.00% | 36 | 39 | 100.00% |
| Delta    | +661 | +35 | +76.98% | +27 | 0 | +76.92% |

Pull Request resolved: https://github.com/pytorch/pytorch/pull/158509
Approved by: https://github.com/williamwen42
This commit is contained in:
Lucas Kabela
2025-07-17 23:31:23 +00:00
committed by PyTorch MergeBot
parent f63988ae00
commit b0e325c2c8
2 changed files with 87 additions and 47 deletions

View File

@ -1,5 +1,5 @@
# mypy: allow-untyped-defs
from typing import Any, Callable, Optional, TYPE_CHECKING, TypeVar
from typing import Any, Callable, Optional, TYPE_CHECKING, TypeVar, Union
from typing_extensions import ParamSpec
import torch
@ -39,6 +39,8 @@ __all__ = [
_P = ParamSpec("_P")
_R = TypeVar("_R")
FuncType = Callable[..., Any]
F = TypeVar("F", bound=FuncType)
def compile(*args, **kwargs):
@ -252,7 +254,10 @@ def disable(fn=None, recursive=True, *, reason=None):
def set_stance(
stance: str = "default", *, skip_guard_eval_unsafe=False, force_backend=None
stance: str = "default",
*,
skip_guard_eval_unsafe: bool = False,
force_backend: Union[str, Callable[..., Any], None] = None,
):
"""
Set the current stance of the compiler.