Skip to content
Draft
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
2 changes: 1 addition & 1 deletion .github/codeowners-coverage-baseline.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2379,7 +2379,7 @@ tests/sentry/test_dependencies.py
tests/sentry/test_devimports.py
tests/sentry/test_http.py
tests/sentry/test_killswitches.py
tests/sentry/test_mypy_stronglist.py
tests/sentry/test_mypy_weaklist.py
tests/sentry/test_no_create_or_update_usage.py
tests/sentry/test_sdk_updates.py
tests/sentry/test_stacktraces.py
Expand Down
4 changes: 2 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,12 @@ repos:
types: [python]
- id: sort-mypy-weaklist
name: sort mypy weaklist
entry: python3 -m tools.mypy_helpers.sort_stronger_modules
entry: python3 -m tools.mypy_helpers.sort_weaklist
files: ^pyproject\.toml$
language: python
- id: check-mypy-weaklist
name: check mypy weaklist
entry: python3 -m tools.mypy_helpers.check_stronglist pyproject.toml
entry: python3 -m tools.mypy_helpers.check_weaklist pyproject.toml
files: (^pyproject\.toml$|\.py$)
pass_filenames: false
language: python
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2376,7 +2376,7 @@ module = [
"tests.sentry.test_devimports",
"tests.sentry.test_http",
"tests.sentry.test_killswitches",
"tests.sentry.test_mypy_stronglist",
"tests.sentry.test_mypy_weaklist",
"tests.sentry.test_no_create_or_update_usage",
"tests.sentry.test_sdk_updates",
"tests.sentry.test_stacktraces",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
import sys


def test_stronglist() -> None:
def test_weaklist() -> None:
pyproject_path = os.path.join(os.path.dirname(__file__), "../../pyproject.toml")
pyproject_path = os.path.relpath(pyproject_path)

proc = subprocess.run(
(sys.executable, "-uSm", "tools.mypy_helpers.check_stronglist", pyproject_path),
(sys.executable, "-uSm", "tools.mypy_helpers.check_weaklist", pyproject_path),
check=False,
text=True,
stdout=subprocess.PIPE,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import re

from tools.mypy_helpers.check_stronglist import _glob_to_re, main
from tools.mypy_helpers.check_weaklist import _glob_to_re, main


def test_glob_to_re_exact_matches() -> None:
Expand Down
19 changes: 19 additions & 0 deletions tests/tools/mypy_helpers/test_prevent_weaklist_additions.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,25 @@ def test_addition_and_removal_same_diff_only_addition_fails(tmp_path, capsys) ->
assert "'d.e.f'" not in out


def test_stronglist_to_weaklist_rename_passes(tmp_path) -> None:
initial = """\
[[tool.mypy.overrides]]
module = ["tests.sentry.test_mypy_stronglist"]
disallow_untyped_defs = false
"""
updated = """\
[[tool.mypy.overrides]]
module = ["tests.sentry.test_mypy_weaklist"]
disallow_untyped_defs = false
"""
_init_repo(tmp_path)
_commit_pyproject(tmp_path, initial)
tmp_path.joinpath("pyproject.toml").write_text(updated)

with contextlib.chdir(tmp_path):
assert main(("pyproject.toml",)) == 0


def test_unrelated_overrides_ignored(tmp_path) -> None:
initial = """\
[[tool.mypy.overrides]]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from pathlib import Path

from tools.mypy_helpers.sort_stronger_modules import main
from tools.mypy_helpers.sort_weaklist import main


def test_sort_stronger_modules(tmp_path: Path) -> None:
def test_sort_weaklist(tmp_path: Path) -> None:
src = """\
# before
Expand Down
23 changes: 23 additions & 0 deletions tools/mypy_helpers/prevent_weaklist_additions.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,23 @@ def _modules_for_override_key(data: dict[str, object], key: str, filename: str)
)


def _is_weaklist_rename(
*,
added_module: str,
head_modules: frozenset[str],
staged_modules: frozenset[str],
) -> bool:
if "weaklist" not in added_module:
return False

legacy_module = added_module.replace("weaklist", "stronglist")
return (
legacy_module != added_module
and legacy_module in head_modules
and legacy_module not in staged_modules
)


def main(argv: Sequence[str] | None = None) -> int:
parser = argparse.ArgumentParser(description="Prevent new additions to mypy override lists.")
parser.add_argument("filenames", nargs="*")
Expand All @@ -53,6 +70,12 @@ def main(argv: Sequence[str] | None = None) -> int:
head_modules = _modules_for_override_key(head_data, key, filename)
staged_modules = _modules_for_override_key(staged_data, key, filename)
for mod in sorted(staged_modules - head_modules):
if _is_weaklist_rename(
added_module=mod,
head_modules=head_modules,
staged_modules=staged_modules,
):
continue
print(
f"{filename}: '{mod}' was added to the {list_name} — "
f"do not add new modules; {advice}."
Expand Down
Loading