Skip to content
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

ref: get proper base manager typing #72221

Merged
merged 1 commit into from
Jul 29, 2024
Merged

Conversation

asottile-sentry
Copy link
Member

@asottile-sentry asottile-sentry commented Jun 6, 2024

an absolute ton of work went into enabling this -- but after this PR model types / create / update / etc. should mostly be typechecked whereas they were entirely unchecked (everything Any) prior to this.

most of the core problem was that mypy and django-stubs did not understand our custom BaseManager and BaseQuerySet since there's a significant amount of django stuff that goes into making those classes "real"

fix that involved these issues:

with that fixed, it exposed a handful issues in django-stubs itself:

fixing all of those together exposed around 1k issues in sentry code itself which was fixed over a number of PRs:

mypy 1.11 also included some important improvements with typechecking for model_cls in (M1, M2, M2): ... which also exposed some problems in our code. unfortunately upgrading mypy involved fixing a lot of things as well:

needless to say this is probably the largest stacked PR I've ever done -- and I'm pretty happy with how I was able to split this up into digestable chunks

@github-actions github-actions bot added the Scope: Backend Automatically applied to PRs that change backend components label Jun 6, 2024
Copy link

codecov bot commented Jun 6, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 78.08%. Comparing base (3dc95d7) to head (0d84170).
Report is 5 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master   #72221      +/-   ##
==========================================
- Coverage   78.09%   78.08%   -0.01%     
==========================================
  Files        6758     6758              
  Lines      301731   301732       +1     
  Branches    51925    51925              
==========================================
- Hits       235624   235617       -7     
- Misses      59761    59765       +4     
- Partials     6346     6350       +4     
Files Coverage Δ
src/sentry/backup/imports.py 90.35% <100.00%> (ø)
src/sentry/db/models/manager/base.py 75.07% <100.00%> (+0.07%) ⬆️
src/sentry/models/files/abstractfile.py 90.36% <100.00%> (ø)
src/sentry/models/files/abstractfileblob.py 86.66% <100.00%> (ø)
...sentry/organizations/services/organization/impl.py 85.44% <100.00%> (ø)

... and 5 files with indirect coverage changes

@getsantry getsantry bot added the Stale label Jun 28, 2024
@getsantry
Copy link
Contributor

getsantry bot commented Jun 28, 2024

This pull request has gone three weeks without activity. In another week, I will close it.

But! If you comment or otherwise update it, I will reset the clock, and if you add the label WIP, I will leave it alone unless WIP is removed ... forever!


"A weed is but an unloved flower." ― Ella Wheeler Wilcox 🥀

@getsantry
Copy link
Contributor

getsantry bot commented Jul 23, 2024

This pull request has gone three weeks without activity. In another week, I will close it.

But! If you comment or otherwise update it, I will reset the clock, and if you add the label WIP, I will leave it alone unless WIP is removed ... forever!


"A weed is but an unloved flower." ― Ella Wheeler Wilcox 🥀

@asottile-sentry asottile-sentry marked this pull request as ready for review July 29, 2024 20:12
@asottile-sentry asottile-sentry requested review from a team as code owners July 29, 2024 20:12
@asottile-sentry asottile-sentry requested review from a team July 29, 2024 20:12
@@ -219,7 +219,8 @@ class Meta:

def _get_chunked_blob(self, mode=None, prefetch=False, prefetch_to=None, delete=True):
return ChunkedFileBlobIndexWrapper(
self.FILE_BLOB_INDEX_MODEL.objects.filter(file=self)
# TODO: file blob inheritance hierarchy is unsound
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

talked a bit with @markstory about these particular models -- we believe there's a way to convert these to free functions to capture the dependent-types inheritance hierarchy with the type system and make these sound -- but as a followup

@@ -475,7 +475,7 @@ def get_or_create_team_member(

def update_membership_flags(self, *, organization_member: RpcOrganizationMember) -> None:
model = OrganizationMember.objects.get(id=organization_member.id)
model.flags = self._deserialize_member_flags(organization_member.flags)
model.flags = self._deserialize_member_flags(organization_member.flags) # type: ignore[assignment] # TODO: make BitField a mypy plugin
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the issue here is HC converts the bitfield to an integer over the wire and the assignment then confuses our workaround for bitfields

I plan to follow up by making bitfield an actual mypy plugin which understands the assignments and values properly here

@asottile-sentry
Copy link
Member Author

note that there are a number of "newly identified errors" in ignored files (~50-100) -- I'm deferring fixing those for now and will fix those as they get opted into type checking

@asottile-sentry asottile-sentry enabled auto-merge (squash) July 29, 2024 20:56
Copy link
Member

@joshuarli joshuarli left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

damnnnnnnnnnnnn

@asottile-sentry asottile-sentry merged commit 89697c3 into master Jul 29, 2024
52 checks passed
@asottile-sentry asottile-sentry deleted the asottile-manager-typing branch July 29, 2024 20:58
roaga pushed a commit that referenced this pull request Jul 31, 2024
an absolute ton of work went into enabling this -- but after this PR
model types / create / update / etc. should mostly be typechecked
whereas they were entirely unchecked (everything `Any`) prior to this.

most of the core problem was that mypy and django-stubs did not
understand our custom `BaseManager` and `BaseQuerySet` since there's a
significant amount of django stuff that goes into making those classes
"real"

fix that involved these issues:

- python/mypy#17402 (worked around)
- typeddjango/django-stubs#2228 (the workaround)

with that fixed, it exposed a handful issues in django-stubs itself:

- typeddjango/django-stubs#2240
- typeddjango/django-stubs#2241
- typeddjango/django-stubs#2244
- typeddjango/django-stubs#2248
- typeddjango/django-stubs#2276
- typeddjango/django-stubs#2281

fixing all of those together exposed around 1k issues in sentry code
itself which was fixed over a number of PRs:
- #75186
- #75108
- #75149
- #75162
- #75150
- #75154
- #75158
- #75148
- #75157
- #75156
- #75152
- #75153
- #75151
- #75113
- #75112
- #75111
- #75110
- #75109
- #75013
- #74641
- #74640
- #73783
- #73787
- #73788
- #73793
- #73791
- #73786
- #73761
- #73742
- #73744
- #73602
- #73596
- #73595
- #72892
- #73589
- #73587
- #73581
- #73580
- #73213
- #73207
- #73206
- #73205
- #73202
- #73198
- #73121
- #73109
- #73073
- #73061
- getsentry/getsentry#14370
- #72965
- #72963
- #72967
- #72877 (eventually was able to remove this when upgrading to mypy
1.11)
- #72948
- #72799
- #72898
- #72899
- #72896
- #72895
- #72903
- #72894
- #72897
- #72893
- #72889
- #72887
- #72888
- #72811
- #72872
- #72813
- #72815
- #72812
- #72808
- #72802
- #72807
- #72809
- #72810
- #72814
- #72816
- #72818
- #72806
- #72801
- #72797
- #72800
- #72798
- #72771
- #72718
- #72719
- #72710
- #72709
- #72706
- #72693
- #72641
- #72591
- #72635


mypy 1.11 also included some important improvements with typechecking
`for model_cls in (M1, M2, M2): ...` which also exposed some problems in
our code. unfortunately upgrading mypy involved fixing a lot of things
as well:
- #75020
- #74982
- #74976
- #74974
- #74972
- #74967
- #74954
- getsentry/getsentry#14739
- getsentry/getsentry#14734
- #74959
- #74958
- #74956
- #74953
- #74955
- #74952
- #74895
- #74892
- #74897
- #74896
- #74893
- #74880
- #74900
- #74902
- #74903
- #74904
- #74899
- #74894
- #74882
- #74881
- #74871
- #74870
- #74855
- #74856
- #74853
- #74857
- #74858
- #74807
- #74805
- #74803
- #74806
- #74798
- #74809
- #74801
- #74804
- #74800
- #74799
- #74725
- #74682
- #74677
- #74680
- #74683
- #74681


needless to say this is probably the largest stacked PR I've ever done
-- and I'm pretty happy with how I was able to split this up into
digestable chunks

<!-- Describe your PR here. -->
@github-actions github-actions bot locked and limited conversation to collaborators Aug 14, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Scope: Backend Automatically applied to PRs that change backend components
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants