pytorch/torch
Sam Estep 5bcbbf5373 Lint trailing newlines (#54737)
Summary:
*Context:* https://github.com/pytorch/pytorch/issues/53406 added a lint for trailing whitespace at the ends of lines. However, in order to pass FB-internal lints, that PR also had to normalize the trailing newlines in four of the files it touched. This PR adds an OSS lint to normalize trailing newlines.

The changes to the following files (made in 54847d0adb9be71be4979cead3d9d4c02160e4cd) are the only manually-written parts of this PR:

- `.github/workflows/lint.yml`
- `mypy-strict.ini`
- `tools/README.md`
- `tools/test/test_trailing_newlines.py`
- `tools/trailing_newlines.py`

I would have liked to make this just a shell one-liner like the other three similar lints, but nothing I could find quite fit the bill. Specifically, all the answers I tried from the following Stack Overflow questions were far too slow (at least a minute and a half to run on this entire repository):

- [How to detect file ends in newline?](https://stackoverflow.com/q/38746)
- [How do I find files that do not end with a newline/linefeed?](https://stackoverflow.com/q/4631068)
- [How to list all files in the Git index without newline at end of file](https://stackoverflow.com/q/27624800)
- [Linux - check if there is an empty line at the end of a file [duplicate]](https://stackoverflow.com/q/34943632)
- [git ensure newline at end of each file](https://stackoverflow.com/q/57770972)

To avoid giving false positives during the few days after this PR is merged, we should probably only merge it after https://github.com/pytorch/pytorch/issues/54967.

Pull Request resolved: https://github.com/pytorch/pytorch/pull/54737

Test Plan:
Running the shell script from the "Ensure correct trailing newlines" step in the `quick-checks` job of `.github/workflows/lint.yml` should print no output and exit in a fraction of a second with a status of 0. That was not the case prior to this PR, as shown by this failing GHA workflow run on an earlier draft of this PR:

- https://github.com/pytorch/pytorch/runs/2197446987?check_suite_focus=true

In contrast, this run (after correcting the trailing newlines in this PR) succeeded:

- https://github.com/pytorch/pytorch/pull/54737/checks?check_run_id=2197553241

To unit-test `tools/trailing_newlines.py` itself (this is run as part of our "Test tools" GitHub Actions workflow):
```
python tools/test/test_trailing_newlines.py
```

Reviewed By: malfet

Differential Revision: D27409736

Pulled By: samestep

fbshipit-source-id: 46f565227046b39f68349bbd5633105b2d2e9b19
2021-03-30 13:09:52 -07:00
..
_C Lint trailing newlines (#54737) 2021-03-30 13:09:52 -07:00
autograd docs: change to FloatTensor for requires_grad=True (#54658) 2021-03-29 10:25:56 -07:00
backends Add size op in nnapi serializer (#52026) 2021-02-10 15:57:01 -08:00
contrib Add type annotations to _tensorboard_vis.py and hipify_python.py (#49834) 2021-01-04 09:29:51 -08:00
csrc Lint trailing newlines (#54737) 2021-03-30 13:09:52 -07:00
cuda docs: add reset_peak_memory_stats in cuda.rst (#54668) 2021-03-29 10:05:20 -07:00
distributed Clang-format powerSGD_hook.py (#54839) 2021-03-30 09:28:45 -07:00
distributions Remove legacy constructor calls from _torch_ folder. (#53889) 2021-03-19 15:20:19 -07:00
fft [doc] Fix documentations of torch functions (#52982) 2021-03-01 09:59:57 -08:00
for_onnx
futures Ensure torch.futures.wait_all exits early on error. (#53953) 2021-03-25 07:39:14 -07:00
fx Improve placeholder matching in subgraph rewriter (#54958) 2021-03-30 11:40:33 -07:00
jit Add documentation for torch.jit.Attribute and torch.jit.annotate (#54485) 2021-03-29 14:44:53 -07:00
legacy
lib [NCCL][Blocking Wait] Log set exceptions when checking for exceptions in (#54558) 2021-03-29 14:15:45 -07:00
linalg Added torch.linalg.matrix_power (#52608) 2021-03-23 15:10:06 -07:00
multiprocessing Make SharedCache thread-safe (#53750) 2021-03-25 06:35:03 -07:00
nn Fix ConvTranspose with padding as a list of values (#54911) 2021-03-30 07:37:31 -07:00
onnx docs: fix docstring signature of torch.{onnx,utils} (#54662) 2021-03-29 10:07:42 -07:00
optim Lint trailing newlines (#54737) 2021-03-30 13:09:52 -07:00
package [package] rename PackageExporter.external to PacakgeExporter.extern_modules (#54601) 2021-03-25 11:50:07 -07:00
profiler Add FLOPS support to the new profiler API. (#51734) 2021-02-05 15:03:35 -08:00
quantization [quant][fx] Add pass in convert to fold quant-dequant sequence (#54860) 2021-03-30 08:40:24 -07:00
sparse Forbid trailing whitespace (#53406) 2021-03-05 17:22:55 -08:00
special [special] Alias for special.expm1 and special.exp2 (#54670) 2021-03-30 10:03:13 -07:00
testing OpInfo: Add opinfo for cum{min,max} and minor fixes (#54762) 2021-03-30 10:24:38 -07:00
utils docs: fix docstring signature of torch.{onnx,utils} (#54662) 2021-03-29 10:07:42 -07:00
__config__.py
__future__.py
__init__.py [codemod] fix tautological imports 2021-03-27 01:15:57 -07:00
_appdirs.py
_autograd_functions.py lu_backward: more numerically stable and with complex support. (#53994) 2021-03-25 13:33:58 -07:00
_classes.py
_deploy.py [package] Pull out _UnpicklerWrapper into PackageUnpickler (#53049) 2021-03-01 18:40:52 -08:00
_jit_internal.py Fix doc (#53996) 2021-03-15 15:44:30 -07:00
_linalg_utils.py Drop unused imports (#49972) 2021-01-13 12:26:17 -08:00
_lobpcg.py
_lowrank.py Drop unused imports (#49972) 2021-01-13 12:26:17 -08:00
_namedtensor_internals.py
_ops.py Back out "Revert D26077905: Back out "Revert D25850783: Add torch::deploy, an embedded torch-python interpreter"" (#51267) 2021-01-28 19:30:45 -08:00
_python_dispatcher.py Rename DefaultBackend to CompositeExplicitAutograd (#54470) 2021-03-26 10:53:30 -07:00
_six.py Clean up usage of torch._six partially (#49785) 2021-02-08 13:58:34 -08:00
_storage_docs.py
_tensor_docs.py docs: remove extra backticks in narrow_copy (#54669) 2021-03-29 10:38:21 -07:00
_tensor_str.py
_tensor.py Fix override for __iter__ (#54702) 2021-03-30 08:30:50 -07:00
_torch_docs.py [special] Alias for special.expm1 and special.exp2 (#54670) 2021-03-30 10:03:13 -07:00
_utils_internal.py Back out "Revert D26077905: Back out "Revert D25850783: Add torch::deploy, an embedded torch-python interpreter"" (#51267) 2021-01-28 19:30:45 -08:00
_utils.py Merge CUDA Streams and Events (#53902) 2021-03-26 14:19:39 -07:00
_VF.py
_vmap_internals.py Beef up {jacobian, hessian} vectorize docs; eliminate a warning (#51638) 2021-02-03 17:15:16 -08:00
abi-check.cpp
CMakeLists.txt Use touch() in pathlib for better compatibility on Windows (#52729) 2021-02-25 13:46:21 -08:00
custom_class_detail.h [PyTorch] Remove reference_cast in make_boxed_from_unboxed_functor (#51319) 2021-02-17 10:58:44 -08:00
custom_class.h [PyTorch][easy] Move more strings in torch::class_ (#54547) 2021-03-29 12:27:11 -07:00
deploy.h Lint trailing newlines (#54737) 2021-03-30 13:09:52 -07:00
extension.h
functional.py lu_backward: more numerically stable and with complex support. (#53994) 2021-03-25 13:33:58 -07:00
hub.py Only allow hub.load() from original repo. (#54451) 2021-03-22 20:27:54 -07:00
library.h Make meta a device (getting rid of empty_meta) (#53143) 2021-03-03 11:24:13 -08:00
overrides.py [special] Alias for special.expm1 and special.exp2 (#54670) 2021-03-30 10:03:13 -07:00
py.typed
quasirandom.py [SobolEngine] Fix edge case of dtype of first sample (#51578) 2021-02-02 14:24:56 -08:00
random.py
README.txt
script.h Revert D26973911: Implement public API InferenceMode and its error handling 2021-03-29 11:17:49 -07:00
serialization.py docs: reference links to serialization.html (#54659) 2021-03-29 10:15:07 -07:00
storage.py
types.py

Note [TH abstraction violation]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

TH/THC provide some hpp headers, which are proper C++ headers rather than
C headers.  These headers serve double duty as *internal implementation
detail* headers, whose contents should largely not be used by external
clients.

Ideally, we would not install these headers at all; instead, you should
use public functions (in headers like `THTensor.h`, NOT `THTensor.hpp`)
to manipulate these structs.  However, there are a few places
in torch/csrc where we violate this abstraction.  They are marked with
a pointer to this note.  Each of those sites will have to be refactored
when we refactor the guts of THTensor and related structures.