mirror of
https://github.com/pytorch/pytorch.git
synced 2025-10-20 21:14:14 +08:00
Summary: Fixes https://github.com/pytorch/pytorch/pull/60006#issuecomment-866130657. Pull Request resolved: https://github.com/pytorch/pytorch/pull/60480 Test Plan: Run `mypy --config mypy-strict.ini` with [`ruamel.yaml`](https://pypi.org/project/ruamel.yaml/) installed. Reviewed By: zhouzhuojie Differential Revision: D29307823 Pulled By: samestep fbshipit-source-id: 97fa4b7dad0465c269411c48142b22ce751bf830
52 lines
2.1 KiB
Python
Executable File
52 lines
2.1 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
'''
|
|
Verify that it is possible to round-trip native_functions.yaml via ruamel under some
|
|
configuration. Keeping native_functions.yaml consistent in this way allows us to
|
|
run codemods on the file using ruamel without introducing line noise. Note that we don't
|
|
want to normalize the YAML file, as that would to lots of spurious lint failures. Anything
|
|
that ruamel understands how to roundtrip, e.g., whitespace and comments, is OK!
|
|
|
|
ruamel is a bit picky about inconsistent indentation, so you will have to indent your
|
|
file properly. Also, if you are working on changing the syntax of native_functions.yaml,
|
|
you may find that you want to use some format that is not what ruamel prefers. If so,
|
|
it is OK to modify this script (instead of reformatting native_functions.yaml)--the point
|
|
is simply to make sure that there is *some* configuration of ruamel that can round trip
|
|
the YAML, not to be prescriptive about it.
|
|
'''
|
|
|
|
import ruamel.yaml # type: ignore[import]
|
|
import difflib
|
|
import sys
|
|
from pathlib import Path
|
|
from io import StringIO
|
|
|
|
def fn(base: str) -> str:
|
|
return str(base / Path("aten/src/ATen/native/native_functions.yaml"))
|
|
|
|
with open(Path(__file__).parent.parent.parent / fn('.'), "r") as f:
|
|
contents = f.read()
|
|
|
|
yaml = ruamel.yaml.YAML() # type: ignore[attr-defined]
|
|
yaml.preserve_quotes = True
|
|
yaml.width = 1000
|
|
yaml.boolean_representation = ['False', 'True']
|
|
r = yaml.load(contents)
|
|
|
|
# Cuz ruamel's author intentionally didn't include conversion to string
|
|
# https://stackoverflow.com/questions/47614862/best-way-to-use-ruamel-yaml-to-dump-to-string-not-to-stream
|
|
string_stream = StringIO()
|
|
yaml.dump(r, string_stream)
|
|
new_contents = string_stream.getvalue()
|
|
string_stream.close()
|
|
|
|
if contents != new_contents:
|
|
print("""\
|
|
|
|
## LINT FAILURE: native_functions.yaml ##
|
|
|
|
native_functions.yaml failed lint; please apply the diff below to fix lint.
|
|
If you think this is in error, please see .github/scripts/lint_native_functions.py
|
|
""", file=sys.stderr)
|
|
sys.stdout.writelines(difflib.unified_diff(contents.splitlines(True), new_contents.splitlines(True), fn('a'), fn('b')))
|
|
sys.exit(1)
|