Skip to content

Commit a785041

Browse files
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>
1 parent 6c2c164 commit a785041

File tree

2 files changed

+34
-18
lines changed

2 files changed

+34
-18
lines changed

pyrightconfig.stricter.json

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@
2323
"stdlib/xml/dom/pulldom.pyi",
2424
"stdlib/xml/sax",
2525
"stubs/aws-xray-sdk",
26-
"stubs/bleach",
27-
"stubs/boto",
2826
"stubs/beautifulsoup4",
27+
"stubs/bleach",
2928
"stubs/boltons",
29+
"stubs/boto",
3030
"stubs/braintree",
3131
"stubs/caldav",
3232
"stubs/cffi",
@@ -36,9 +36,11 @@
3636
"stubs/docutils",
3737
"stubs/Flask-Migrate",
3838
"stubs/fpdf2",
39+
"stubs/google-cloud-ndb",
3940
"stubs/html5lib",
4041
"stubs/httplib2",
4142
"stubs/humanfriendly",
43+
"stubs/influxdb-client",
4244
"stubs/invoke",
4345
"stubs/jmespath",
4446
"stubs/jsonschema",
@@ -47,14 +49,12 @@
4749
"stubs/mysqlclient",
4850
"stubs/oauthlib",
4951
"stubs/openpyxl",
50-
"stubs/Pillow",
51-
"stubs/protobuf",
52-
"stubs/google-cloud-ndb",
53-
"stubs/influxdb-client",
5452
"stubs/passlib",
5553
"stubs/peewee",
5654
"stubs/pexpect",
5755
"stubs/pika",
56+
"stubs/Pillow",
57+
"stubs/protobuf",
5858
"stubs/psutil",
5959
"stubs/psycopg2",
6060
"stubs/pyasn1",
@@ -75,7 +75,7 @@
7575
"stubs/urllib3",
7676
"stubs/vobject",
7777
"stubs/WebOb",
78-
"stubs/workalendar"
78+
"stubs/workalendar",
7979
],
8080
"typeCheckingMode": "strict",
8181
// TODO: Complete incomplete stubs

scripts/create_baseline_stubs.py

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -152,22 +152,38 @@ def add_pyright_exclusion(stub_dir: str) -> None:
152152
assert i < len(lines), f"Error parsing {PYRIGHT_CONFIG}"
153153
while not lines[i].strip().startswith("]"):
154154
i += 1
155-
# Must use forward slash in the .json file
156-
line_to_add = f' "{stub_dir}",'.replace("\\", "/")
157-
initial = i - 1
158-
while lines[i].lower() > line_to_add.lower():
155+
end = i
156+
157+
# We assume that all third-party excludes must be at the end of the list.
158+
# This helps with skipping special entries, such as "stubs/**/@tests/test_cases".
159+
while lines[i - 1].strip().startswith('"stubs/'):
159160
i -= 1
160-
if lines[i + 1].strip().rstrip(",") == line_to_add.strip().rstrip(","):
161+
start = i
162+
163+
before_third_party_excludes = lines[:start]
164+
third_party_excludes = lines[start:end]
165+
after_third_party_excludes = lines[end:]
166+
167+
last_line = third_party_excludes[-1].rstrip()
168+
if not last_line.endswith(","):
169+
last_line += ","
170+
third_party_excludes[-1] = last_line + "\n"
171+
172+
# Must use forward slash in the .json file
173+
line_to_add = f' "{stub_dir}",\n'.replace("\\", "/")
174+
175+
if line_to_add in third_party_excludes:
161176
print(f"{PYRIGHT_CONFIG} already up-to-date")
162177
return
163-
if i == initial:
164-
# Special case: when adding to the end of the list, commas need tweaking
165-
line_to_add = line_to_add.rstrip(",")
166-
lines[i] = lines[i].rstrip() + ",\n"
167-
lines.insert(i + 1, line_to_add + "\n")
178+
179+
third_party_excludes.append(line_to_add)
180+
third_party_excludes.sort(key=str.lower)
181+
168182
print(f"Updating {PYRIGHT_CONFIG}")
169183
with open(PYRIGHT_CONFIG, "w", encoding="UTF-8") as f:
170-
f.writelines(lines)
184+
f.writelines(before_third_party_excludes)
185+
f.writelines(third_party_excludes)
186+
f.writelines(after_third_party_excludes)
171187

172188

173189
def main() -> None:

0 commit comments

Comments
 (0)