From d1ca40deea0b3ce60964ba383d3256b755910d44 Mon Sep 17 00:00:00 2001 From: anthony sottile Date: Mon, 22 Jul 2024 15:23:39 -0400 Subject: [PATCH] ref: fix signatures of contribute_to_class --- pyproject.toml | 1 + src/bitfield/models.py | 5 +++-- src/sentry/db/models/fields/array.py | 6 +++-- src/sentry/db/models/fields/citext.py | 26 +++++++++++++--------- src/sentry/db/models/fields/gzippeddict.py | 6 ++--- src/sentry/db/models/fields/jsonfield.py | 8 +++++-- src/sentry/db/models/fields/node.py | 5 +++-- src/sentry/db/models/fields/uuid.py | 8 +++++-- src/social_auth/fields.py | 8 ++++--- 9 files changed, 46 insertions(+), 27 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 92902db8f36b1..d9338f8e736b8 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -512,6 +512,7 @@ module = [ "sentry.auth.services.*", "sentry.buffer.*", "sentry.build.*", + "sentry.db.models.fields.citext", "sentry.db.models.fields.hybrid_cloud_foreign_key", "sentry.db.models.fields.types", "sentry.db.models.manager.*", diff --git a/src/bitfield/models.py b/src/bitfield/models.py index 1e6c3ed35971c..abe24a23283f2 100644 --- a/src/bitfield/models.py +++ b/src/bitfield/models.py @@ -1,6 +1,7 @@ from collections.abc import Mapping, Sequence from typing import Any, TypeVar, cast +from django.db.models import Model from django.db.models.fields import BigIntegerField from bitfield.query import BitQueryExactLookupStub @@ -77,8 +78,8 @@ def __get__(self, obj, type=None): class BitField(BigIntegerField): - def contribute_to_class(self, cls, name, **kwargs): - super().contribute_to_class(cls, name, **kwargs) + def contribute_to_class(self, cls: type[Model], name: str, private_only: bool = False) -> None: + super().contribute_to_class(cls, name, private_only=private_only) setattr(cls, self.name, BitFieldCreator(self)) def __init__(self, flags, default=None, *args, **kwargs): diff --git a/src/sentry/db/models/fields/array.py b/src/sentry/db/models/fields/array.py index 838722d2407f7..7aa80341e0287 100644 --- a/src/sentry/db/models/fields/array.py +++ b/src/sentry/db/models/fields/array.py @@ -1,3 +1,5 @@ +from __future__ import annotations + import ast from django.db import models @@ -23,12 +25,12 @@ def __init__(self, of=models.TextField, **kwargs): super().__init__(**kwargs) - def contribute_to_class(self, cls, name): + def contribute_to_class(self, cls: type[models.Model], name: str, private_only: bool = False): """ Add a descriptor for backwards compatibility with previous Django behavior. """ - super().contribute_to_class(cls, name) + super().contribute_to_class(cls, name, private_only=private_only) setattr(cls, name, Creator(self)) def db_type(self, connection): diff --git a/src/sentry/db/models/fields/citext.py b/src/sentry/db/models/fields/citext.py index 1176356ad1bdc..db21f50e1c41d 100644 --- a/src/sentry/db/models/fields/citext.py +++ b/src/sentry/db/models/fields/citext.py @@ -1,4 +1,8 @@ +from __future__ import annotations + from django.db import connections, models +from django.db.backends.base.base import BaseDatabaseWrapper +from django.db.models import Model from django.db.models.signals import pre_migrate from sentry.db.models.utils import Creator @@ -7,29 +11,29 @@ class CIText: - def db_type(self, connection): + def db_type(self, connection: BaseDatabaseWrapper) -> str: return "citext" -class CITextField(CIText, models.TextField): - def contribute_to_class(self, cls, name): - super().contribute_to_class(cls, name) +class CITextField(CIText, models.TextField[str, str]): + def contribute_to_class(self, cls: type[Model], name: str, private_only: bool = False) -> None: + super().contribute_to_class(cls, name, private_only=private_only) setattr(cls, name, Creator(self)) -class CICharField(CIText, models.CharField): - def contribute_to_class(self, cls, name): - super().contribute_to_class(cls, name) +class CICharField(CIText, models.CharField[str, str]): + def contribute_to_class(self, cls: type[Model], name: str, private_only: bool = False) -> None: + super().contribute_to_class(cls, name, private_only=private_only) setattr(cls, name, Creator(self)) -class CIEmailField(CIText, models.EmailField): - def contribute_to_class(self, cls, name): - super().contribute_to_class(cls, name) +class CIEmailField(CIText, models.EmailField[str, str]): + def contribute_to_class(self, cls: type[Model], name: str, private_only: bool = False) -> None: + super().contribute_to_class(cls, name, private_only=private_only) setattr(cls, name, Creator(self)) -def create_citext_extension(using, **kwargs): +def create_citext_extension(using: str, **kwargs: object) -> None: # We always need the citext extension installed for Postgres, # and for tests, it's not always guaranteed that we will have # run full migrations which installed it. diff --git a/src/sentry/db/models/fields/gzippeddict.py b/src/sentry/db/models/fields/gzippeddict.py index bf620def21e5a..30461d7eed98e 100644 --- a/src/sentry/db/models/fields/gzippeddict.py +++ b/src/sentry/db/models/fields/gzippeddict.py @@ -3,7 +3,7 @@ import logging import pickle -from django.db.models import TextField +from django.db.models import Model, TextField from sentry.db.models.utils import Creator from sentry.utils import json @@ -20,12 +20,12 @@ class GzippedDictField(TextField): value is a dictionary. """ - def contribute_to_class(self, cls, name): + def contribute_to_class(self, cls: type[Model], name: str, private_only: bool = False) -> None: """ Add a descriptor for backwards compatibility with previous Django behavior. """ - super().contribute_to_class(cls, name) + super().contribute_to_class(cls, name, private_only=private_only) setattr(cls, name, Creator(self)) def to_python(self, value): diff --git a/src/sentry/db/models/fields/jsonfield.py b/src/sentry/db/models/fields/jsonfield.py index d0dea9b26f057..d8d3e3bf7b2d5 100644 --- a/src/sentry/db/models/fields/jsonfield.py +++ b/src/sentry/db/models/fields/jsonfield.py @@ -25,6 +25,8 @@ OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. """ +from __future__ import annotations + from django.core.exceptions import ValidationError from django.db import models from django.db.models.lookups import Contains, Exact, IContains, IExact, In, Lookup @@ -60,12 +62,14 @@ def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.validate(self.get_default(), None) - def contribute_to_class(self, cls, name): + def contribute_to_class( + self, cls: type[models.Model], name: str, private_only: bool = False + ) -> None: """ Add a descriptor for backwards compatibility with previous Django behavior. """ - super().contribute_to_class(cls, name) + super().contribute_to_class(cls, name, private_only=private_only) if not self.no_creator_hook: setattr(cls, name, Creator(self)) diff --git a/src/sentry/db/models/fields/node.py b/src/sentry/db/models/fields/node.py index 858974826817e..7e3844319f67e 100644 --- a/src/sentry/db/models/fields/node.py +++ b/src/sentry/db/models/fields/node.py @@ -7,6 +7,7 @@ from typing import Any from uuid import uuid4 +from django.db.models import Model from django.db.models.signals import post_delete from django.utils.functional import cached_property @@ -167,8 +168,8 @@ def __init__( self.ref_version = ref_version super().__init__(blank=blank, null=null) - def contribute_to_class(self, cls, name): - super().contribute_to_class(cls, name) + def contribute_to_class(self, cls: type[Model], name: str, private_only: bool = False) -> None: + super().contribute_to_class(cls, name, private_only=private_only) setattr(cls, name, Creator(self)) post_delete.connect(self.on_delete, sender=self.model, weak=False) diff --git a/src/sentry/db/models/fields/uuid.py b/src/sentry/db/models/fields/uuid.py index 741fd484abc46..78ec78697220c 100644 --- a/src/sentry/db/models/fields/uuid.py +++ b/src/sentry/db/models/fields/uuid.py @@ -27,6 +27,8 @@ (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. """ +from __future__ import annotations + import importlib from uuid import UUID, uuid4 @@ -126,8 +128,10 @@ def pre_save(self, instance, add): # This is the standard case; just use the superclass logic. return super().pre_save(instance, add) - def contribute_to_class(self, cls, name): - super().contribute_to_class(cls, name) + def contribute_to_class( + self, cls: type[models.Model], name: str, private_only: bool = False + ) -> None: + super().contribute_to_class(cls, name, private_only=private_only) setattr(cls, name, Creator(self)) def to_python(self, value): diff --git a/src/social_auth/fields.py b/src/social_auth/fields.py index 521ddf6689066..e02470ddfa777 100644 --- a/src/social_auth/fields.py +++ b/src/social_auth/fields.py @@ -1,5 +1,7 @@ +from __future__ import annotations + from django.core.exceptions import ValidationError -from django.db.models import TextField +from django.db.models import Model, TextField from django.utils.encoding import smart_str from sentry.db.models.utils import Creator @@ -11,12 +13,12 @@ class JSONField(TextField): on database. """ - def contribute_to_class(self, cls, name): + def contribute_to_class(self, cls: type[Model], name: str, private_only: bool = False) -> None: """ Add a descriptor for backwards compatibility with previous Django behavior. """ - super().contribute_to_class(cls, name) + super().contribute_to_class(cls, name, private_only=private_only) setattr(cls, name, Creator(self)) def to_python(self, value):