Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nit: Fix/add some type annotations #1982

Merged
merged 8 commits into from
Nov 13, 2024
Merged

Conversation

bradhilton
Copy link
Contributor

Context

What is the purpose of this PR? Is it to

  • add a new feature
  • fix a bug
  • update tests and/or documentation
  • other (please add here)

Changelog

What are the changes made in this PR?

  • Fix/add a few type annotations in torchtune.config._instantiate.py and touchtone.config._parse.py

Copy link

pytorch-bot bot commented Nov 11, 2024

🔗 Helpful Links

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

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

✅ No Failures

As of commit 2e21126 with merge base ac4f88e (image):
💚 Looks good so far! There are no failures yet. 💚

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

@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Nov 11, 2024
RdoubleA
RdoubleA previously approved these changes Nov 12, 2024
Copy link
Contributor

@RdoubleA RdoubleA left a comment

Choose a reason for hiding this comment

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

Thanks for the fix! Out of curiosity, is there a "correct" typing for *args and **kwargs? I thought they actually should be untyped. The updated return types look good to me, though.

@RdoubleA RdoubleA dismissed their stale review November 12, 2024 18:52

should address comments

@bradhilton
Copy link
Contributor Author

Thanks for the fix! Out of curiosity, is there a "correct" typing for *args and **kwargs? I thought they actually should be untyped. The updated return types look good to me, though.

You can type them, but there is a bit of nuance:

def foo(*args: Any, **kwargs: Any) -> None:
    args # inferred type here is tuple[Any, ...]
    kwargs # inferred type here is dict[str, Any]
    
def bar(*args: tuple[Any, ...], **kwargs: dict[str, Any]) -> None:
    args # inferred type here would be tuple[tuple[Any, ...], ...]
    kwargs # inferred type here would be dict[str, dict[str, Any]]

You can see that the type annotations are for the members of args and kwargs, not args and kwargs themselves. If you type them as tuples or dicts, then it will infer them to be nested tuples or nested dicts. If you try to call bar with something other than tuple args and dict kwargs then a type error will be presented and the user directed to pass tuples and dicts which is usually not the desired behavior.

You can leave *args and **kwargs untyped and it should infer tuple[Any, ...] and dict[str, Any] automatically. You can also narrow the type of args and kwargs like this for example:

def foobar(*strings: str, **ints: int) -> None:
    strings # inferred type is tuple[str, ...]
    ints # inferred type is dict[str, int]
    
foobar("hello", a=10, b=3) # okay
foobar(10, c="world") # not okay, args are expected to be strings and kwargs are expected to be integers

@RdoubleA
Copy link
Contributor

@bradhilton just need to fix lint and I can stamp it

@bradhilton
Copy link
Contributor Author

I think lint passed @RdoubleA

@bradhilton
Copy link
Contributor Author

Test failing, need to revert some changes.

@felipemello1
Copy link
Contributor

@bradhilton , thanks for the PR! Would you mind making it consistent in the other places that we have typing for args, kwargs?

https://github.com/search?q=repo%3Apytorch%2Ftorchtune%20*args%20(Tuple%5BAny%2C%20...%5D)&type=code

uv.lock Outdated
Copy link
Contributor

Choose a reason for hiding this comment

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

probably unintentional?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yup! 🤦‍♂️

@felipemello1
Copy link
Contributor

will merge after tests pass. Thanks!

@felipemello1 felipemello1 merged commit 4b6877a into pytorch:main Nov 13, 2024
17 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants