Skip to content

Commit

Permalink
ref: fix signatures of contribute_to_class (#74680)
Browse files Browse the repository at this point in the history
fixes issues when upgrading mypy to 1.11.0

<!-- Describe your PR here. -->
  • Loading branch information
asottile-sentry authored Jul 23, 2024
1 parent 9669f33 commit e4c807c
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 27 deletions.
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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.*",
Expand Down
5 changes: 3 additions & 2 deletions src/bitfield/models.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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):
Expand Down
6 changes: 4 additions & 2 deletions src/sentry/db/models/fields/array.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import ast

from django.db import models
Expand All @@ -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):
Expand Down
26 changes: 15 additions & 11 deletions src/sentry/db/models/fields/citext.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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.
Expand Down
6 changes: 3 additions & 3 deletions src/sentry/db/models/fields/gzippeddict.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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):
Expand Down
8 changes: 6 additions & 2 deletions src/sentry/db/models/fields/jsonfield.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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))

Expand Down
5 changes: 3 additions & 2 deletions src/sentry/db/models/fields/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)

Expand Down
8 changes: 6 additions & 2 deletions src/sentry/db/models/fields/uuid.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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):
Expand Down
8 changes: 5 additions & 3 deletions src/social_auth/fields.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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):
Expand Down

0 comments on commit e4c807c

Please sign in to comment.