Skip to content

[Formatting] headers and future imports checks #2973

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,17 @@ repos:
entry: autoflake --in-place --remove-unused-variables --remove-all-unused-imports
language: system
types: [python]

- id: check-future-annotations
name: Check for `from __future__ import annotations`
entry: python packaging/check_future_annotations.py
language: python
files: \.py$
exclude: run-clang-format\.py

- id: check-header
name: Check for Meta copyright header
entry: python packaging/check_headers.py
language: python
files: \.py$
exclude: run-clang-format\.py
43 changes: 43 additions & 0 deletions packaging/check_future_annotations.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.
from __future__ import annotations

import sys


def add_future_import(filename):
with open(filename, encoding="utf-8") as f:
lines = f.readlines()

# Check if the import is already present
for line in lines:
if line.strip() == "from __future__ import annotations":
return # Import already present, no need to modify

# Find the position to insert the import
insert_pos = 0
for i, line in enumerate(lines):
stripped_line = line.strip()
if stripped_line and not stripped_line.startswith("#"):
insert_pos = i
break

# Insert the import statement after the first comment block
lines.insert(insert_pos, "from __future__ import annotations\n\n")

# Write the modified lines back to the file
with open(filename, "w", encoding="utf-8") as f:
f.writelines(lines)


def main():
files = sys.argv[1:]
for f in files:
add_future_import(f)
print("Processed files to ensure `from __future__ import annotations` is present.")


if __name__ == "__main__":
main()
39 changes: 39 additions & 0 deletions packaging/check_headers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.

from __future__ import annotations

import sys

HEADER = """# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.
"""


def check_header(filename):
with open(filename, encoding="utf-8") as f:
file_content = f.read()

if not file_content.startswith(HEADER):
print(f"Missing or incorrect header in {filename}")
return False
return True


def main():
files = sys.argv[1:]
all_passed = True
for f in files:
if not check_header(f):
all_passed = False
if not all_passed:
sys.exit(1)
sys.exit(0)


if __name__ == "__main__":
main()
Loading