create_baseline_stubs.py: Improve pyright config file editing (#10629)

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
This commit is contained in:
Akuli
2023-08-30 17:43:46 +03:00
committed by GitHub
parent 6c2c164de2
commit a785041250
2 changed files with 34 additions and 18 deletions

View File

@@ -23,10 +23,10 @@
"stdlib/xml/dom/pulldom.pyi",
"stdlib/xml/sax",
"stubs/aws-xray-sdk",
"stubs/bleach",
"stubs/boto",
"stubs/beautifulsoup4",
"stubs/bleach",
"stubs/boltons",
"stubs/boto",
"stubs/braintree",
"stubs/caldav",
"stubs/cffi",
@@ -36,9 +36,11 @@
"stubs/docutils",
"stubs/Flask-Migrate",
"stubs/fpdf2",
"stubs/google-cloud-ndb",
"stubs/html5lib",
"stubs/httplib2",
"stubs/humanfriendly",
"stubs/influxdb-client",
"stubs/invoke",
"stubs/jmespath",
"stubs/jsonschema",
@@ -47,14 +49,12 @@
"stubs/mysqlclient",
"stubs/oauthlib",
"stubs/openpyxl",
"stubs/Pillow",
"stubs/protobuf",
"stubs/google-cloud-ndb",
"stubs/influxdb-client",
"stubs/passlib",
"stubs/peewee",
"stubs/pexpect",
"stubs/pika",
"stubs/Pillow",
"stubs/protobuf",
"stubs/psutil",
"stubs/psycopg2",
"stubs/pyasn1",
@@ -75,7 +75,7 @@
"stubs/urllib3",
"stubs/vobject",
"stubs/WebOb",
"stubs/workalendar"
"stubs/workalendar",
],
"typeCheckingMode": "strict",
// TODO: Complete incomplete stubs

View File

@@ -152,22 +152,38 @@ def add_pyright_exclusion(stub_dir: str) -> None:
assert i < len(lines), f"Error parsing {PYRIGHT_CONFIG}"
while not lines[i].strip().startswith("]"):
i += 1
# Must use forward slash in the .json file
line_to_add = f' "{stub_dir}",'.replace("\\", "/")
initial = i - 1
while lines[i].lower() > line_to_add.lower():
end = i
# We assume that all third-party excludes must be at the end of the list.
# This helps with skipping special entries, such as "stubs/**/@tests/test_cases".
while lines[i - 1].strip().startswith('"stubs/'):
i -= 1
if lines[i + 1].strip().rstrip(",") == line_to_add.strip().rstrip(","):
start = i
before_third_party_excludes = lines[:start]
third_party_excludes = lines[start:end]
after_third_party_excludes = lines[end:]
last_line = third_party_excludes[-1].rstrip()
if not last_line.endswith(","):
last_line += ","
third_party_excludes[-1] = last_line + "\n"
# Must use forward slash in the .json file
line_to_add = f' "{stub_dir}",\n'.replace("\\", "/")
if line_to_add in third_party_excludes:
print(f"{PYRIGHT_CONFIG} already up-to-date")
return
if i == initial:
# Special case: when adding to the end of the list, commas need tweaking
line_to_add = line_to_add.rstrip(",")
lines[i] = lines[i].rstrip() + ",\n"
lines.insert(i + 1, line_to_add + "\n")
third_party_excludes.append(line_to_add)
third_party_excludes.sort(key=str.lower)
print(f"Updating {PYRIGHT_CONFIG}")
with open(PYRIGHT_CONFIG, "w", encoding="UTF-8") as f:
f.writelines(lines)
f.writelines(before_third_party_excludes)
f.writelines(third_party_excludes)
f.writelines(after_third_party_excludes)
def main() -> None: