Skip to content
New issue

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

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

Already on GitHub? # to your account

Set up automation for updating pytype and pyright versions. #11491

Closed
wants to merge 8 commits into from
34 changes: 34 additions & 0 deletions .github/workflows/update_daily_ dependency.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
name: Check Dependencies

on:
schedule:
# Runs every day at 00:00 UTC
- cron: '0 0 * * *'

jobs:
build:
strategy:
matrix:
os: [ubuntu-latest]
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v2
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: "3.11"
- name: Install dependencies
run: |
pip install requests
pip install toml
- name: Run script
run: python scripts/check_for_update_dependency.py
- uses: peter-evans/create-pull-request@v3
with:
token: ${{ secrets.PAT }}
title: "Update dpendency Daily"
commit-message: "Updated Depenceny after the daily check"
body: |
There appear to be some dependency updates in ${{ github.sha }}. This pull request
uses the script to find and update these dependency.
base: ${{ github.head_ref }}
63 changes: 63 additions & 0 deletions scripts/check_for_update_dependency.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import re
import requests

Check failure on line 2 in scripts/check_for_update_dependency.py

View workflow job for this annotation

GitHub Actions / Run pyright against the scripts and tests directories (Linux)

Import "requests" could not be resolved from source (reportMissingModuleSource)

Check failure on line 2 in scripts/check_for_update_dependency.py

View workflow job for this annotation

GitHub Actions / Run pyright against the scripts and tests directories (Windows)

Import "requests" could not be resolved from source (reportMissingModuleSource)
import toml
from typing import List

from typing import Optional


def get_latest_version(package_name: str) -> Optional[str]:
"""Fetch the latest version of a package from PyPI."""
response = requests.get(f"https://pypi.org/pypi/{package_name}/json")
if response.status_code == 200:
data = response.json()
return str(data["info"]["version"])
else:
return None


def update_versions(file_path: str) -> None:
with open(file_path, "r") as file:
content = file.readlines()

pattern = re.compile(r"^(pytype|mypy)==([\d.]+)")

updated_content: List[str] = []

for line in content:
match = pattern.match(line)
if match:
package_name = match.group(1)
current_version = match.group(2)
latest_version = get_latest_version(package_name)
if latest_version and current_version != latest_version:
print(f"Updating {package_name} from {current_version} to {latest_version}")
line = line.replace(current_version, latest_version)
updated_content.append(line)

with open(file_path, "w") as file:
file.writelines(updated_content)


def update_pyright_version(file_path: str) -> None:
with open(file_path, "r", encoding="utf-8") as file:
config = toml.load(file)

latest_version = get_latest_version("pyright")

if latest_version:
config["tool"]["typeshed"]["pyright_version"] = latest_version

with open(file_path, "w", encoding="utf-8") as file:
toml.dump(config, file)
print(f"Updated pyright to version {latest_version} in {file_path}")


file_paths: List[str] = ["requirements-tests.txt", "pyproject.toml"]

if __name__ == "__main__":
for file_path in file_paths:
if file_path.endswith(".txt"):
update_versions(file_path)
elif file_path.endswith(".toml"):
update_pyright_version(file_path)
Loading