-
-
Notifications
You must be signed in to change notification settings - Fork 4.4k
Fix(typing): Fix typing in 561 files #98132
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
thetruecpaul
wants to merge
26
commits into
master
Choose a base branch
from
hackweek/typing-25
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
+2,805
−1,904
Conversation
This file contains hidden or 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
Added return types to functions that just return literal strings, ints, or bools. Used the following python script: ``` from re import sub # find **/*.py > files.txt for file in open("files.txt"): source = open(file.strip()).read() """ def foo(x: int, y): return f"1234" """ # Literal strings source = sub( r"def ([^\n>]+):(\n\s+)(return [rf]?(\"[^\"]*\"|'[^']*')\n)", r"def \1 -> str:\2\3", source, ) # Literal ints source = sub( r"def ([^\n>]+):(\n\s+)(return \d+\n)", r"def \1 -> int:\2\3", source, ) # Literal bools source = sub( r"def ([^\n>]+):(\n\s+)(return (True|False)\n)", r"def \1 -> bool:\2\3", source, ) open(file.strip(), "w").write(source) ```
Added a bunch of return types. (Took the HttpResponseBase from the root class.) `mypy` no errors
two super-duper low-lift files (just adding `None` return types).
Part of Hackweek 2025 typing efforts ⭐
# Summary Grabbed this because I thought it would just be a bunch of easy missing-return-type errors. It turns out, when you add return types you expose other errors. Who'd've thunk? 😅 Here's the complicating factor: the plugin framework uses `Request`. `RequestFactory` returns `WSGIRequest`. `BaseTestCase.make_request` returns `HttpRequest`. Our typing revealed that a bunch of incorrect `Request-ish` types were being passed around. This PR changes the incompatible frameworks to `MagicMock`, which more correctly limits behavior to what is explicitly defined (and not what would not actually happen due to type differences). # Test Plan `mypy tests/sentry_plugins` ==> no errors `pytest tests/sentry_plugins/**` ==> `162 passed, 1 skipped`
was picking through the files with only one error. this got a lot of the initially-known-to-mypy errors fixed... but I'm concerned that it exposed more errors that I don't have a good way of exposing, since I didn't stick to a single easily-added-to-pyproject module. I think in the future I'll probably stick to codemods or modules.
Just needed to type the setup functions! `rg -l 'def setUp\(self\):' tests/apidocs | xargs sed -i '' -E -e 's/def setUp\(self\):/def setUp(self) -> None:/'` # Test Plan Ran `mypy tests/apidocs`, no errors
# Summary 59 files, newly typed! # Test Plan `mypy` no errors --------- Co-authored-by: getsantry[bot] <66042841+getsantry[bot]@users.noreply.github.com>
# Summary Developed a new pattern that let me run through some files I identified as low-lift quickly: * Added `"sentry.*", "test.*"` to `pyproject.toml` * Developed a list of expected-to-be-low-lift files from mypy output * Mostly files that were in test directory, only had one error, or only needed `None` return types * Then repeatedly ran `mypy <big_list_of_files>` and whittled that down * Before pushing the branch, I removed the changes to `pyproject.toml` Result is that I can confidently say that this PR successfully types ~150 new files. # Test Plan Reverted changes to `pyproject.toml` and ran `mypy` to ensure the new types had no effect on steady-state — no errors!
These magic test functions always have the same returns! I already messed with some in `tests/sentry`; worth expanding to all of `tests` ``` rg 'def tearDown' tests -ls | xargs sed -i '' 's/def tearDown(self):/def tearDown(self) -> None:/' rg 'def setUp' tests -ls | xargs sed -i '' 's/def setUp(self):/def setUp(self) -> None:/' ```
Follows #97998 Turns out there's a utility function to convert `HttpRequest` to `Request`! Better to use the normal requests than full mocks.
completely types an additional 65 files with low errors-per-file count
This was more complex than expected 😅
# Summary Typed a new module, 11 files fixed. # Test Plan `mypy` no errors
`mypy` no errors
# Summary dragged types from `pytest.mark.parametrize` to the test function header fully types an additional 28 files # Test Plan `mypy` no errors
Found a pattern where this module just needed their `run_test` methods typed.
Victory lap!!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Hackweek!