forked from getsentry/sentry
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ref: ensure stronger typing list is sorted (getsentry#68128)
<!-- Describe your PR here. -->
- Loading branch information
1 parent
f8439c5
commit c1edfd1
Showing
5 changed files
with
121 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
from tools.mypy_helpers.sort_stronger_modules import main | ||
|
||
|
||
def test_sort_stronger_modules(tmp_path): | ||
src = """\ | ||
# before | ||
# begin: stronger typing | ||
[[tool.mypy.overrides]] | ||
module = [ | ||
"mod2", | ||
"mod1", | ||
"mod3", | ||
] | ||
some_setting = true | ||
# end: stronger typing | ||
# after | ||
""" | ||
expected = """\ | ||
# before | ||
# begin: stronger typing | ||
[[tool.mypy.overrides]] | ||
module = [ | ||
"mod1", | ||
"mod2", | ||
"mod3", | ||
] | ||
some_setting = true | ||
# end: stronger typing | ||
# after | ||
""" | ||
|
||
f = tmp_path.joinpath("f") | ||
f.write_text(src) | ||
|
||
assert main((str(f),)) == 1 | ||
|
||
assert f.read_text() == expected | ||
|
||
assert main((str(f),)) == 0 | ||
|
||
|
||
def test_removes_duplicates(tmp_path): | ||
src = """\ | ||
# begin: stronger typing | ||
[[tool.mypy.overrides]] | ||
module = [ | ||
"mod1", | ||
"mod1", | ||
] | ||
some_setting = true | ||
# end: stronger typing | ||
""" | ||
expected = """\ | ||
# begin: stronger typing | ||
[[tool.mypy.overrides]] | ||
module = [ | ||
"mod1", | ||
] | ||
some_setting = true | ||
# end: stronger typing | ||
""" | ||
f = tmp_path.joinpath("f") | ||
f.write_text(src) | ||
|
||
assert main((str(f),)) == 1 | ||
|
||
assert f.read_text() == expected |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
from __future__ import annotations | ||
|
||
import argparse | ||
from collections.abc import Sequence | ||
|
||
|
||
def main(argv: Sequence[str] | None = None) -> int: | ||
parser = argparse.ArgumentParser() | ||
parser.add_argument("filenames", nargs="*") | ||
args = parser.parse_args(argv) | ||
|
||
ret = 0 | ||
for filename in args.filenames: | ||
with open(filename) as f: | ||
contents = f.read() | ||
|
||
rest = contents | ||
b1, m1, rest = rest.partition("# begin: stronger typing\n") | ||
b2, m2, rest = rest.partition("module = [\n") | ||
b3, m3, rest = rest.partition("]\n") | ||
b4, m4, rest = rest.partition("# end: stronger typing\n") | ||
|
||
b3 = "".join(sorted(frozenset(b3.splitlines(True)))) | ||
|
||
new_contents = b1 + m1 + b2 + m2 + b3 + m3 + b4 + m4 + rest | ||
if new_contents != contents: | ||
with open(filename, "w") as f: | ||
f.write(new_contents) | ||
ret = 1 | ||
|
||
return ret | ||
|
||
|
||
if __name__ == "__main__": | ||
raise SystemExit(main()) |