Skip to content

Commit 24c1516

Browse files
add
1 parent 2117f4a commit 24c1516

File tree

3 files changed

+27
-14
lines changed

3 files changed

+27
-14
lines changed

django_ltree/apps.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ class DjangoLtreeConfig(AppConfig):
55
default_auto_field = "django.db.models.BigAutoField"
66
name = "django_ltree"
77

8-
def ready(self):
8+
def ready(self) -> None:
99
from . import checks as checks
1010
from . import lookups as lookups
1111
from . import functions as functions

django_ltree/fields.py

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
from django.forms.widgets import TextInput
66

77
from collections.abc import Iterable
8+
from typing import TypeVarTuple, NoReturn, Any, TYPE_CHECKING
9+
10+
if TYPE_CHECKING:
11+
from django_ltree.models import TreeModel
812

913
path_label_validator = RegexValidator(
1014
r"^(?P<root>[a-zA-Z][a-zA-Z0-9_]*|\d+)(?:\.[a-zA-Z0-9_]+)*$",
@@ -13,8 +17,8 @@
1317
)
1418

1519

16-
class PathValue(UserList):
17-
def __init__(self, value):
20+
class PathValue(UserList[str]):
21+
def __init__(self, value: str | int | Iterable[str]):
1822
if isinstance(value, str):
1923
split_by = "/" if "/" in value else "."
2024
value = value.strip().split(split_by) if value else []
@@ -27,18 +31,20 @@ def __init__(self, value):
2731

2832
super().__init__(initlist=value)
2933

30-
def __repr__(self):
34+
def __repr__(self) -> str:
3135
return str(self)
3236

33-
def __str__(self):
37+
def __str__(self) -> str:
3438
return ".".join(self)
3539

3640

3741
class PathValueProxy:
38-
def __init__(self, field_name):
42+
def __init__(self, field_name: str) -> None:
3943
self.field_name = field_name
4044

41-
def __get__(self, instance, owner):
45+
def __get__(
46+
self, instance: "PathValueProxy" | None, *args: TypeVarTuple
47+
) -> "PathValueProxy" | "PathValue" | None:
4248
if instance is None:
4349
return self
4450

@@ -49,7 +55,9 @@ def __get__(self, instance, owner):
4955

5056
return PathValue(instance.__dict__[self.field_name])
5157

52-
def __set__(self, instance, value):
58+
def __set__(
59+
self, instance: "PathValueProxy" | None, value: str
60+
) -> NoReturn | "PathValueProxy":
5361
if instance is None:
5462
return self
5563

@@ -63,15 +71,17 @@ class PathFormField(forms.CharField):
6371
class PathField(TextField):
6472
default_validators = [path_label_validator]
6573

66-
def db_type(self, connection):
74+
def db_type(self, *args: TypeVarTuple) -> str:
6775
return "ltree"
6876

69-
def formfield(self, **kwargs):
77+
def formfield(self, **kwargs: Any) -> Any:
7078
kwargs["form_class"] = PathFormField
7179
kwargs["widget"] = TextInput(attrs={"class": "vTextField"})
7280
return super().formfield(**kwargs)
7381

74-
def contribute_to_class(self, cls, name, private_only=False):
82+
def contribute_to_class(
83+
self, cls: type["TreeModel"], name: str, private_only: bool = False
84+
) -> None:
7585
super().contribute_to_class(cls, name)
7686
setattr(cls, self.name, PathValueProxy(self.name))
7787

@@ -80,20 +90,22 @@ def from_db_value(self, value, expression, connection, *args):
8090
return value
8191
return PathValue(value)
8292

83-
def get_prep_value(self, value):
93+
def get_prep_value(self, value: str | None) -> str | None:
8494
if value is None:
8595
return value
8696
return str(PathValue(value))
8797

88-
def to_python(self, value):
98+
def to_python(self, value: str | None | PathValue) -> PathValue | None:
8999
if value is None:
90100
return value
91101
elif isinstance(value, PathValue):
92102
return value
93103

94104
return PathValue(value)
95105

96-
def get_db_prep_value(self, value, connection, prepared=False):
106+
def get_db_prep_value(
107+
self, value: str | None | PathValue, connection: str, prepared: bool = False
108+
) -> str | None:
97109
if value is None:
98110
return value
99111
elif isinstance(value, PathValue):

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ build-backend = "poetry.core.masonry.api"
5353
[tool.mypy]
5454
mypy_path = "./django_ltree/"
5555
namespace_packages = false
56+
ignore_missing_imports = true
5657
show_error_codes = true
5758
strict = true
5859
warn_unreachable = true

0 commit comments

Comments
 (0)