Skip to content

Commit

Permalink
Add some type annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
fsbraun committed Oct 28, 2024
1 parent 5c8e79f commit edfd76b
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 14 deletions.
25 changes: 14 additions & 11 deletions djangocms_link/fields.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
from __future__ import annotations

import json

from django.apps import apps
from django.conf import settings
from django.contrib.admin import site
from django.contrib.admin.widgets import SELECT2_TRANSLATIONS, AutocompleteSelect
from django.contrib.sites.models import Site
from django.db import models
from django.db.models import JSONField, ManyToOneRel
from django.forms import Field, MultiWidget, Select, TextInput, URLInput
from django.utils.translation import get_language
Expand All @@ -30,10 +33,10 @@


class LinkAutoCompleteWidget(AutocompleteSelect):
def __init__(self, attrs=None):
def __init__(self, attrs: dict | None = None):
super().__init__(None, None, attrs)

def get_internal_obj(self, values):
def get_internal_obj(self, values: list[str | None]) -> list[models.Model | None]:
internal_obj = []
for value in values:
if value:
Expand All @@ -44,7 +47,7 @@ def get_internal_obj(self, values):
internal_obj.append(None)
return internal_obj

def optgroups(self, name, value, attr=None):
def optgroups(self, name: str, value: str, attr: str | None = None):
default = (None, [], 0)
groups = [default]
has_selected = False
Expand All @@ -69,7 +72,7 @@ def optgroups(self, name, value, attr=None):
def get_url(self):
return admin_reverse("djangocms_link_link_urls")

def build_attrs(self, base_attrs, extra_attrs=None):
def build_attrs(self, base_attrs: dict, extra_attrs: dict | None = None) -> dict:
"""
Set select2's AJAX attributes.
Expand Down Expand Up @@ -104,7 +107,7 @@ def build_attrs(self, base_attrs, extra_attrs=None):
class SiteAutocompleteSelect(AutocompleteSelect):
no_sites = None

def __init__(self, attrs=None):
def __init__(self, attrs: dict | None = None):
# Hack: Pretend that the user is selecting a site for a Page object
# and use Django admin's autocomplete widget
try:
Expand All @@ -118,7 +121,7 @@ def __init__(self, attrs=None):
field = Page._meta.get_field("site")
super().__init__(field, site, attrs)

def optgroups(self, name, value, attr=None):
def optgroups(self, name: str, value: str, attr: dict | None = None):
default = (None, [], 0)
groups = [default]
has_selected = False
Expand Down Expand Up @@ -224,7 +227,7 @@ class Media:
js = ("djangocms_link/link-widget.js",)
css = {"all": ("djangocms_link/link-widget.css",)}

def __init__(self, site_selector=None):
def __init__(self, site_selector: bool | None = None):
if site_selector is None:
site_selector = LinkWidget.default_site_selector

Expand All @@ -246,7 +249,7 @@ def __init__(self, site_selector=None):
}
super().__init__(widgets)

def get_context(self, name, value, attrs):
def get_context(self, name: str, value: str | None, attrs: dict) -> dict:
if not self.is_required:
self.widgets[0].choices = [("empty", "---------")] + self.widgets[0].choices
context = super().get_context(name, value, attrs)
Expand Down Expand Up @@ -277,7 +280,7 @@ def __init__(self, *args, **kwargs):
kwargs.pop("decoder", None) # but not needed
super().__init__(*args, **kwargs)

def prepare_value(self, value):
def prepare_value(self, value: dict) -> list[str | None]:
if isinstance(value, list):
return value

Check warning on line 285 in djangocms_link/fields.py

View check run for this annotation

Codecov / codecov/patch

djangocms_link/fields.py#L285

Added line #L285 was not covered by tests
multi_value = len(self.widget.widgets) * [None]
Expand All @@ -297,7 +300,7 @@ def prepare_value(self, value):
multi_value[pos] = str(value["file_link"])
return multi_value

def to_python(self, value):
def to_python(self, value: list[str | None]) -> dict:
"""Turn MultiWidget list data into LinkField dict format"""
if not value:
return {}
Expand All @@ -313,7 +316,7 @@ def to_python(self, value):
python["anchor"] = value[pos_anchor]
return python

def run_validators(self, value):
def run_validators(self, value: dict):
"""Check for <link_type>_validators property and run the validators"""
for link_type in link_types:
if link_type in value:
Expand Down
1 change: 0 additions & 1 deletion djangocms_link/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ def get_rel_obj(internal_link: str) -> models.Model | None:
model, pk = internal_link.split(":", 1)
model = apps.get_model(*model.split(".", 1))
return get_manager(model).filter(pk=pk).first()
return None


def get_link(link_field_value: dict, site_id: int | None = None) -> str | None:
Expand Down
4 changes: 2 additions & 2 deletions djangocms_link/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class AnchorValidator:
message = _("Enter a valid anchor")
code = "invalid"

def __call__(self, value):
def __call__(self, value: str):
value = value.lstrip("#")
if not value:
return value
Expand All @@ -71,7 +71,7 @@ def __init__(self, allowed_link_types: list = None, **kwargs):
self.allowed_link_types = allowed_link_types
super().__init__(**kwargs)

def __call__(self, value):
def __call__(self, value: str):
if not isinstance(value, str) or len(value) > self.max_length:
raise ValidationError(self.message, code=self.code, params={"value": value})
if self.unsafe_chars.intersection(value):
Expand Down

0 comments on commit edfd76b

Please sign in to comment.