Skip to content

Commit 5bab9ae

Browse files
Update @property attributes to @cached_property part 2 (#1768)
* Fix more cached properties Using this shell and this python script (EXCLUDING `output_field`, will fix in another MR): ```shell rm -rf .mypy_cache/ && stubtest django \ --mypy-config-file mypy.ini \ --ignore-positional-only \ --allowlist scripts/stubtest/allowlist.txt | rg "cannot reconcile @Property on stub" -A4 > t.txt && cat t.txt | rg "Stub: in file " | cut -d" " -f4 > t2.txt ``` -------- ```python from collections import defaultdict line_to_fix = defaultdict(set) with open("t2.txt") as f: for line in f: file_path, lineno = line.strip().split(":") line_to_fix[file_path].add(int(lineno)) for file_path, line_numbers in line_to_fix.items(): lines = ["from django.utils.functional import cached_property\n"] with open(file_path) as f: lines.extend(f.readlines()) nb_lines = len(lines) for line_number in line_numbers: if line_number<nb_lines: lines[line_number] = lines[line_number].replace("@Property", "@cached_property") with open(file_path, "w") as f: f.writelines(lines) ```
1 parent c7d6adc commit 5bab9ae

File tree

25 files changed

+231
-213
lines changed

25 files changed

+231
-213
lines changed

django-stubs/contrib/admin/helpers.pyi

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,8 +146,7 @@ class InlineAdminFormSet:
146146
def inline_formset_data(self) -> str: ...
147147
@property
148148
def forms(self) -> list[BaseForm]: ...
149-
@property
150-
def non_form_errors(self) -> Callable[[], ErrorList]: ...
149+
def non_form_errors(self) -> ErrorList: ...
151150
@property
152151
def media(self) -> Media: ...
153152

django-stubs/contrib/gis/db/backends/base/operations.pyi

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
from typing import Any
22

3+
from django.utils.functional import cached_property
4+
35
class BaseSpatialOperations:
46
postgis: bool
57
spatialite: bool
68
mysql: bool
79
oracle: bool
810
spatial_version: Any
911
select: str
10-
@property
12+
@cached_property
1113
def select_extent(self) -> str: ...
1214
geography: bool
1315
geometry: bool

django-stubs/contrib/gis/db/backends/mysql/features.pyi

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ from typing import Any
22

33
from django.contrib.gis.db.backends.base.features import BaseSpatialFeatures
44
from django.db.backends.mysql.features import DatabaseFeatures as MySQLDatabaseFeatures
5+
from django.utils.functional import cached_property
56

67
class DatabaseFeatures(BaseSpatialFeatures, MySQLDatabaseFeatures):
78
has_spatialrefsys_table: bool
@@ -14,7 +15,7 @@ class DatabaseFeatures(BaseSpatialFeatures, MySQLDatabaseFeatures):
1415
supports_num_points_poly: bool
1516
@property
1617
def empty_intersection_returns_none(self) -> bool: ...
17-
@property
18+
@cached_property
1819
def supports_geometry_field_unique_index(self) -> bool: ... # type: ignore[override]
19-
@property
20+
@cached_property
2021
def django_test_skips(self) -> dict[str, Any]: ... # type: ignore[override]

django-stubs/contrib/gis/db/backends/mysql/operations.pyi

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,24 @@ from django.contrib.gis.db.backends.base.operations import BaseSpatialOperations
55
from django.contrib.gis.db.backends.utils import SpatialOperator
66
from django.contrib.gis.geos.geometry import GEOSGeometryBase
77
from django.db.backends.mysql.operations import DatabaseOperations
8+
from django.utils.functional import cached_property
89

910
class MySQLOperations(BaseSpatialOperations, DatabaseOperations):
1011
name: str
1112
geom_func_prefix: str
1213
Adapter: Any
13-
@property
14+
@cached_property
1415
def mariadb(self) -> bool: ...
15-
@property
16+
@cached_property
1617
def mysql(self) -> bool: ... # type: ignore[override]
17-
@property
18+
@cached_property
1819
def select(self) -> str: ... # type: ignore[override]
19-
@property
20+
@cached_property
2021
def from_text(self) -> str: ... # type: ignore[override]
21-
@property
22+
@cached_property
2223
def gis_operators(self) -> dict[str, SpatialOperator]: ...
2324
disallowed_aggregates: Any
24-
@property
25+
@cached_property
2526
def unsupported_functions(self) -> set[str]: ... # type: ignore[override]
2627
def geo_db_type(self, f: Any) -> Any: ...
2728
def get_distance(self, f: Any, value: Any, lookup_type: Any) -> list[Any]: ...
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
from django.contrib.gis.db.backends.base.features import BaseSpatialFeatures
22
from django.db.backends.sqlite3.features import DatabaseFeatures as SQLiteDatabaseFeatures
3+
from django.utils.functional import cached_property
34

45
class DatabaseFeatures(BaseSpatialFeatures, SQLiteDatabaseFeatures):
56
supports_3d_storage: bool
6-
@property
7+
@cached_property
78
def supports_area_geodetic(self) -> bool: ... # type: ignore[override]

django-stubs/contrib/gis/db/backends/spatialite/operations.pyi

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ from typing import Any
33
from django.contrib.gis.db.backends.base.operations import BaseSpatialOperations
44
from django.contrib.gis.db.backends.utils import SpatialOperator
55
from django.db.backends.sqlite3.operations import DatabaseOperations
6+
from django.utils.functional import cached_property
67

78
class SpatialiteNullCheckOperator(SpatialOperator): ...
89

@@ -18,7 +19,7 @@ class SpatiaLiteOperations(BaseSpatialOperations, DatabaseOperations):
1819
disallowed_aggregates: Any
1920
select: str
2021
function_names: Any
21-
@property
22+
@cached_property
2223
def unsupported_functions(self) -> set[str]: ... # type: ignore[override]
2324
@property
2425
def spatial_version(self) -> Any: ...

django-stubs/contrib/gis/gdal/raster/source.pyi

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ from django.contrib.gis.gdal.driver import Driver
77
from django.contrib.gis.gdal.raster.band import BandList
88
from django.contrib.gis.gdal.raster.base import GDALRasterBase
99
from django.contrib.gis.gdal.srs import SpatialReference
10+
from django.utils.functional import cached_property
1011

1112
class TransformPoint(list[Sequence[float]]):
1213
indices: dict[str, tuple[int, int]]
@@ -26,11 +27,11 @@ class GDALRaster(GDALRasterBase):
2627
def __del__(self) -> None: ...
2728
@property
2829
def vsi_buffer(self) -> bytes | None: ...
29-
@property
30+
@cached_property
3031
def is_vsi_based(self) -> bool: ...
3132
@property
3233
def name(self) -> str: ...
33-
@property
34+
@cached_property
3435
def driver(self) -> Driver: ...
3536
@property
3637
def width(self) -> int: ...

django-stubs/contrib/postgres/indexes.pyi

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@ from django.db.models import Func, Index, Model
77
from django.db.models.expressions import BaseExpression, Combinable
88
from django.db.models.query_utils import Q
99
from django.utils.datastructures import _ListOrTuple
10+
from django.utils.functional import cached_property
1011

1112
class PostgresIndex(Index):
12-
@property
13+
@cached_property
1314
def max_name_length(self) -> int: ... # type: ignore[override]
1415
def create_sql(
1516
self, model: type[Model], schema_editor: BaseDatabaseSchemaEditor, using: str = ..., **kwargs: Any

django-stubs/contrib/sessions/backends/db.pyi

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@ from typing import Any
22

33
from django.contrib.sessions.backends.base import SessionBase
44
from django.contrib.sessions.base_session import AbstractBaseSession
5+
from django.utils.functional import cached_property
56

67
class SessionStore(SessionBase):
78
def __init__(self, session_key: str | None = ...) -> None: ...
89
@classmethod
910
def get_model_class(cls) -> type[AbstractBaseSession]: ...
10-
@property
11+
@cached_property
1112
def model(self) -> type[AbstractBaseSession]: ...
1213
def create_model_instance(self, data: dict[str, Any]) -> AbstractBaseSession: ...

django-stubs/db/backends/base/base.pyi

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ from django.db.backends.base.operations import BaseDatabaseOperations
1212
from django.db.backends.base.schema import BaseDatabaseSchemaEditor
1313
from django.db.backends.base.validation import BaseDatabaseValidation
1414
from django.db.backends.utils import CursorDebugWrapper, CursorWrapper
15+
from django.utils.functional import cached_property
1516
from typing_extensions import Self, TypeAlias
1617

1718
NO_DB_ALIAS: str
@@ -65,9 +66,9 @@ class BaseDatabaseWrapper:
6566
operators: MutableMapping[str, str]
6667
def __init__(self, settings_dict: dict[str, Any], alias: str = ...) -> None: ...
6768
def ensure_timezone(self) -> bool: ...
68-
@property
69+
@cached_property
6970
def timezone(self) -> tzinfo | None: ...
70-
@property
71+
@cached_property
7172
def timezone_name(self) -> str: ...
7273
@property
7374
def queries_logged(self) -> bool: ...

0 commit comments

Comments
 (0)