Skip to content

Conversation

@Lucaskabela
Copy link
Contributor

@Lucaskabela Lucaskabela commented Jul 31, 2025

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%

cc @voznesenskym @penguinwu @EikanWang @jgong5 @Guobing-Chen @XiaobingSuper @zhuhaozhe @blzheng @wenzhe-nrv @jiayisunx @chenyang78 @kadeng @chauhang @amjames

@pytorch-bot
Copy link

pytorch-bot bot commented Jul 31, 2025

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/159580

Note: Links to docs will display an error until the docs builds have been completed.

✅ You can merge normally! (1 Unrelated Failure)

As of commit 1187733 with merge base 2a286cb (image):

UNSTABLE - The following job is marked as unstable, possibly due to flakiness on trunk:

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@Lucaskabela
Copy link
Contributor Author

@pytorchbot label "topic: not user facing"

@pytorch-bot pytorch-bot bot added the topic: not user facing topic category label Jul 31, 2025
@Lucaskabela Lucaskabela changed the title Lucaskabela/typing utils.py [Dynamo][Better Engineering] Type coverage for torch/_dynamo/utils.py Jul 31, 2025
@Lucaskabela Lucaskabela force-pushed the lucaskabela/typing_utils.py branch from 1eb77e6 to a6dc480 Compare July 31, 2025 21:36
@Lucaskabela Lucaskabela marked this pull request as ready for review August 1, 2025 15:54
@Lucaskabela Lucaskabela force-pushed the lucaskabela/typing_utils.py branch from d58c817 to 1187733 Compare August 1, 2025 16:00
@Lucaskabela Lucaskabela requested a review from Skylion007 August 1, 2025 16:19
@Lucaskabela
Copy link
Contributor Author

@pytorchmergebot merge

@pytorch-bot pytorch-bot bot added the ciflow/trunk Trigger trunk jobs on your pull request label Aug 4, 2025
@pytorchmergebot
Copy link
Collaborator

Merge started

Your change will be merged once all checks pass (ETA 0-4 Hours).

Learn more about merging in the wiki.

Questions? Feedback? Please reach out to the PyTorch DevX Team

Advanced Debugging
Check the merge workflow status
here

Copy link
Collaborator

@Skylion007 Skylion007 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some nits about these types

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:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting, bufferedWriter doesn't necessarily subclass this? This is an fsspec bug, isn't it. :(

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:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting, bufferedWriter doesn't necessarily subclass this? This is an fsspec bug, isn't it. :(



def hashable(x):
def hashable(x: Any) -> bool:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use a typing_extension.TypeIs[Hashable] as return type. Also this is very antiquated, you can use the abc protocol for this now, can't you?



def is_numpy_float_type(value):
def is_numpy_float_type(value: Any) -> bool:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could be TypeIs/Typeguard

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These numpy types may be undefined unfortunately if the import fails, so I am not sure how to use them as type annotations in this event :/



def is_numpy_int_type(value):
def is_numpy_int_type(value: Any) -> bool:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here. Allows type narrowing

return fn(*args, **kwargs)


def is_utils_checkpoint(obj):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could be TypeIs return type with TypeChecking block to avoid circular import

@Skylion007
Copy link
Collaborator

@Lucaskabela Feel free to do a subsequent PR to further improve these

@Lucaskabela
Copy link
Contributor Author

@Skylion007 thanks for the feedback! I will follow up shortly with improvements :) Honestly before these comments I was not very familiar with TypeIs/TypeGuard for narrowing so thank you for bringing these to my attention!

facebook-github-bot pushed a commit to pytorch/benchmark that referenced this pull request Aug 5, 2025
Summary:
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% |

X-link: pytorch/pytorch#159580
Approved by: https://github.com/williamwen42

Reviewed By: izaitsevfb

Differential Revision: D79597355

fbshipit-source-id: 5bb7a21d68ef3c24659c5a79e148caeb9219a11f
@github-actions github-actions bot deleted the lucaskabela/typing_utils.py branch September 4, 2025 02:07
markc-614 pushed a commit to markc-614/pytorch that referenced this pull request Sep 17, 2025
…y` (pytorch#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: pytorch#159580
Approved by: https://github.com/williamwen42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants