10000 Add `Any` return annotation to `__getattr__` methods that return a union of types. by rchen152 · Pull Request #150204 · pytorch/pytorch · GitHub
[go: up one dir, main page]

Skip to content

Add Any return annotation to __getattr__ methods that return a union of types. #150204

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

Closed
wants to merge 1 commit into from

Conversation

rchen152
Copy link
Contributor
@rchen152 rchen152 commented Mar 28, 2025

Adds an Any return type annotation to __getattr__ methods in torch/_ops.py that return a union of types. Attribute access returning a union of types can cause issues downstream because consumers would need to handle all of the possible types to make the type checker happy. This doesn't seem to matter today for mypy, presumably because Any is always inferred when a return type annotation is missing, but it still makes explicit what mypy is already doing implicitly.

…ion of types.

Summary: Adds an `Any` return type annotation to `__getattr__` methods in `torch/_ops.py` that return a union of types. Attribute access returning a union of types can cause issues downstream because consumers would need to handle all of the possible types to make the type checker happy. This doesn't seem to matter today for mypy, presumably because `Any` is always inferred when a return type annotation is missing, but it still makes explicit what mypy is already doing implicitly.

Test Plan:

Reviewers:

Subscribers:

Tasks:

Tags:
Copy link
pytorch-bot bot commented Mar 28, 2025

🔗 Helpful Links

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

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

✅ You can merge normally! (4 Unrelated Failures)

As of commit 6103902 with merge base 35ff508 (image):

FLAKY - The following job failed but was likely due to flakiness present on trunk:

BROKEN TRUNK - The following jobs failed but was present on the merge base:

👉 Rebase onto the `viable/strict` branch to avoid these failures

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.

Copy link
linux-foundation-easycla bot commented Mar 28, 2025

CLA Signed

The committers listed above are authorized under a signed CLA.

  • ✅ login: rchen152 / name: Rebecca Chen (6103902)

@rchen152 rchen152 added the topic: not user facing topic category label Mar 28, 2025
@rchen152
Copy link
Contributor Author

This is ready for review! The two test failures are unrelated as far as I can tell.

@rchen152 rchen152 marked this pull request as ready for review March 29, 2025 02:50
@zou3519
Copy link
Contributor
zou3519 commented Mar 31, 2025

@malfet do you have thoughts on this from the infra side? I'd like to avoid a situation where internal typing is more strict than OSS mypy resulting in frequent reverts of OSS PRs for breaking internal typing.

So usually we'd require a test for these changes such that they are actually testable in open source.

Copy link
Contributor
@malfet malfet left a comment

Choose a reason for hiding this comment

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

LGTM, but one needs to annotate the entire file and add it to subset of files which are validate by mypy-strict.ini

@rchen152
Copy link
Contributor Author
rchen152 commented Apr 1, 2025

Thanks! @malfet Just to clarify, is this PR ok to merge as-is, or should I not add these annotations without completely annotating the file and adding it to mypy-strict.ini?

@malfet malfet added ciflow/inductor ciflow/trunk Trigger trunk jobs on your pull request release notes: python_frontend python frontend release notes category topic: improvements topic category and removed topic: not user facing topic category labels Apr 1, 2025
@malfet
Copy link
Contributor
malfet commented Apr 1, 2025

Thanks! @malfet Just to clarify, is this PR ok to merge as-is, or should I not add these annotations without completely annotating the file and adding it to mypy-strict.ini?

If it passes CI, it's OK as is, as long as everyone understands that without enabling linter that actively checks for those, one should not expect that those annotations will stay there, someone can as easily remove them later by accident

@rchen152
Copy link
Contributor Author
rchen152 commented Apr 1, 2025

If it passes CI, it's OK as is, as long as everyone understands that without enabling linter that actively checks for those, one should not expect that those annotations will stay there, someone can as easily remove them later by accident

Ah, got it. Thanks!

@rchen152
Copy link
Contributor Author
rchen152 commented Apr 1, 2025

@pytorchbot merge

@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

@pytorchmergebot
Copy link
Collaborator

Merge failed

Reason: 1 jobs have failed, first few of them are: inductor / unit-test / linux-jammy-cpu-py3.12-gcc11-inductor-halide / build

Details for Dev Infra team Raised by workflow job

@malfet
Copy link
Contributor
malfet commented Apr 2, 2025

@pytorchbot merge -i

@pytorchmergebot
Copy link
Collaborator

Merge started

Your change will be merged while ignoring the following 3 checks: pull / linux-jammy-py3-clang12-executorch / build, pull / cuda12.4-py3.10-gcc9-sm75 / test (pr_time_benchmarks, 1, 1, linux.g4dn.metal.nvidia.gpu), trunk / macos-py3-arm64-mps / test (mps, 1, 1, macos-m2-15)

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

@rchen152 rchen152 deleted the any branch April 2, 2025 05:26
amathewc pushed a commit to amathewc/pytorch that referenced this pull request Apr 17, 2025
…ion of types. (pytorch#150204)

Adds an `Any` return type annotation to `__getattr__` methods in `torch/_ops.py` that return a union of types. Attribute access returning a union of types can cause issues downstream because consumers would need to handle all of the possible types to make the type checker happy. This doesn't seem to matter today for mypy, presumably because `Any` is always inferred when a return type annotation is missing, but it still makes explicit what mypy is already doing implicitly.

Pull Request resolved: pytorch#150204
Approved by: https://github.com/malfet
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ciflow/inductor ciflow/trunk Trigger trunk jobs on your pull request Merged release notes: python_frontend python frontend release notes category topic: improvements topic category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants
0