Skip to content

Commit

Permalink
Update @property attributes to @cached_property part 2 (#1768)
Browse files Browse the repository at this point in the history
* 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)
```
  • Loading branch information
UnknownPlatypus authored Nov 7, 2023
1 parent c7d6adc commit 5bab9ae
Show file tree
Hide file tree
Showing 25 changed files with 231 additions and 213 deletions.
3 changes: 1 addition & 2 deletions django-stubs/contrib/admin/helpers.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,7 @@ class InlineAdminFormSet:
def inline_formset_data(self) -> str: ...
@property
def forms(self) -> list[BaseForm]: ...
@property
def non_form_errors(self) -> Callable[[], ErrorList]: ...
def non_form_errors(self) -> ErrorList: ...
@property
def media(self) -> Media: ...

Expand Down
4 changes: 3 additions & 1 deletion django-stubs/contrib/gis/db/backends/base/operations.pyi
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
from typing import Any

from django.utils.functional import cached_property

class BaseSpatialOperations:
postgis: bool
spatialite: bool
mysql: bool
oracle: bool
spatial_version: Any
select: str
@property
@cached_property
def select_extent(self) -> str: ...
geography: bool
geometry: bool
Expand Down
5 changes: 3 additions & 2 deletions django-stubs/contrib/gis/db/backends/mysql/features.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ from typing import Any

from django.contrib.gis.db.backends.base.features import BaseSpatialFeatures
from django.db.backends.mysql.features import DatabaseFeatures as MySQLDatabaseFeatures
from django.utils.functional import cached_property

class DatabaseFeatures(BaseSpatialFeatures, MySQLDatabaseFeatures):
has_spatialrefsys_table: bool
Expand All @@ -14,7 +15,7 @@ class DatabaseFeatures(BaseSpatialFeatures, MySQLDatabaseFeatures):
supports_num_points_poly: bool
@property
def empty_intersection_returns_none(self) -> bool: ...
@property
@cached_property
def supports_geometry_field_unique_index(self) -> bool: ... # type: ignore[override]
@property
@cached_property
def django_test_skips(self) -> dict[str, Any]: ... # type: ignore[override]
13 changes: 7 additions & 6 deletions django-stubs/contrib/gis/db/backends/mysql/operations.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,24 @@ from django.contrib.gis.db.backends.base.operations import BaseSpatialOperations
from django.contrib.gis.db.backends.utils import SpatialOperator
from django.contrib.gis.geos.geometry import GEOSGeometryBase
from django.db.backends.mysql.operations import DatabaseOperations
from django.utils.functional import cached_property

class MySQLOperations(BaseSpatialOperations, DatabaseOperations):
name: str
geom_func_prefix: str
Adapter: Any
@property
@cached_property
def mariadb(self) -> bool: ...
@property
@cached_property
def mysql(self) -> bool: ... # type: ignore[override]
@property
@cached_property
def select(self) -> str: ... # type: ignore[override]
@property
@cached_property
def from_text(self) -> str: ... # type: ignore[override]
@property
@cached_property
def gis_operators(self) -> dict[str, SpatialOperator]: ...
disallowed_aggregates: Any
@property
@cached_property
def unsupported_functions(self) -> set[str]: ... # type: ignore[override]
def geo_db_type(self, f: Any) -> Any: ...
def get_distance(self, f: Any, value: Any, lookup_type: Any) -> list[Any]: ...
Expand Down
3 changes: 2 additions & 1 deletion django-stubs/contrib/gis/db/backends/spatialite/features.pyi
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from django.contrib.gis.db.backends.base.features import BaseSpatialFeatures
from django.db.backends.sqlite3.features import DatabaseFeatures as SQLiteDatabaseFeatures
from django.utils.functional import cached_property

class DatabaseFeatures(BaseSpatialFeatures, SQLiteDatabaseFeatures):
supports_3d_storage: bool
@property
@cached_property
def supports_area_geodetic(self) -> bool: ... # type: ignore[override]
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ from typing import Any
from django.contrib.gis.db.backends.base.operations import BaseSpatialOperations
from django.contrib.gis.db.backends.utils import SpatialOperator
from django.db.backends.sqlite3.operations import DatabaseOperations
from django.utils.functional import cached_property

class SpatialiteNullCheckOperator(SpatialOperator): ...

Expand All @@ -18,7 +19,7 @@ class SpatiaLiteOperations(BaseSpatialOperations, DatabaseOperations):
disallowed_aggregates: Any
select: str
function_names: Any
@property
@cached_property
def unsupported_functions(self) -> set[str]: ... # type: ignore[override]
@property
def spatial_version(self) -> Any: ...
Expand Down
5 changes: 3 additions & 2 deletions django-stubs/contrib/gis/gdal/raster/source.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ from django.contrib.gis.gdal.driver import Driver
from django.contrib.gis.gdal.raster.band import BandList
from django.contrib.gis.gdal.raster.base import GDALRasterBase
from django.contrib.gis.gdal.srs import SpatialReference
from django.utils.functional import cached_property

class TransformPoint(list[Sequence[float]]):
indices: dict[str, tuple[int, int]]
Expand All @@ -26,11 +27,11 @@ class GDALRaster(GDALRasterBase):
def __del__(self) -> None: ...
@property
def vsi_buffer(self) -> bytes | None: ...
@property
@cached_property
def is_vsi_based(self) -> bool: ...
@property
def name(self) -> str: ...
@property
@cached_property
def driver(self) -> Driver: ...
@property
def width(self) -> int: ...
Expand Down
3 changes: 2 additions & 1 deletion django-stubs/contrib/postgres/indexes.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ from django.db.models import Func, Index, Model
from django.db.models.expressions import BaseExpression, Combinable
from django.db.models.query_utils import Q
from django.utils.datastructures import _ListOrTuple
from django.utils.functional import cached_property

class PostgresIndex(Index):
@property
@cached_property
def max_name_length(self) -> int: ... # type: ignore[override]
def create_sql(
self, model: type[Model], schema_editor: BaseDatabaseSchemaEditor, using: str = ..., **kwargs: Any
Expand Down
3 changes: 2 additions & 1 deletion django-stubs/contrib/sessions/backends/db.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ from typing import Any

from django.contrib.sessions.backends.base import SessionBase
from django.contrib.sessions.base_session import AbstractBaseSession
from django.utils.functional import cached_property

class SessionStore(SessionBase):
def __init__(self, session_key: str | None = ...) -> None: ...
@classmethod
def get_model_class(cls) -> type[AbstractBaseSession]: ...
@property
@cached_property
def model(self) -> type[AbstractBaseSession]: ...
def create_model_instance(self, data: dict[str, Any]) -> AbstractBaseSession: ...
5 changes: 3 additions & 2 deletions django-stubs/db/backends/base/base.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ from django.db.backends.base.operations import BaseDatabaseOperations
from django.db.backends.base.schema import BaseDatabaseSchemaEditor
from django.db.backends.base.validation import BaseDatabaseValidation
from django.db.backends.utils import CursorDebugWrapper, CursorWrapper
from django.utils.functional import cached_property
from typing_extensions import Self, TypeAlias

NO_DB_ALIAS: str
Expand Down Expand Up @@ -65,9 +66,9 @@ class BaseDatabaseWrapper:
operators: MutableMapping[str, str]
def __init__(self, settings_dict: dict[str, Any], alias: str = ...) -> None: ...
def ensure_timezone(self) -> bool: ...
@property
@cached_property
def timezone(self) -> tzinfo | None: ...
@property
@cached_property
def timezone_name(self) -> str: ...
@property
def queries_logged(self) -> bool: ...
Expand Down
5 changes: 3 additions & 2 deletions django-stubs/db/backends/base/features.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ from typing import Any
from django.db.backends.base.base import BaseDatabaseWrapper
from django.db.models.base import Model
from django.db.utils import DatabaseError, DataError
from django.utils.functional import cached_property

class BaseDatabaseFeatures:
minimum_database_version: tuple[int, ...] | None
Expand Down Expand Up @@ -137,8 +138,8 @@ class BaseDatabaseFeatures:
django_test_skips: dict[str, set[str]]
connection: BaseDatabaseWrapper
def __init__(self, connection: BaseDatabaseWrapper) -> None: ...
@property
@cached_property
def supports_explaining_query_execution(self) -> bool: ...
@property
@cached_property
def supports_transactions(self) -> bool: ...
def allows_group_by_selected_pks_on_model(self, model: type[Model]) -> bool: ...
4 changes: 2 additions & 2 deletions django-stubs/db/backends/mysql/base.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ class DatabaseWrapper(BaseDatabaseWrapper):
def check_constraints(self, table_names: Any | None = ...) -> None: ...
def is_usable(self) -> bool: ...
@property
def display_name(self) -> str: ... # type: ignore [override]
def display_name(self) -> str: ... # type: ignore[override]
@property
def data_type_check_constraints(self) -> dict[str, str]: ... # type: ignore [override]
def data_type_check_constraints(self) -> dict[str, str]: ... # type: ignore[override]
@property
def mysql_server_data(self) -> dict[str, Any]: ...
@property
Expand Down
45 changes: 23 additions & 22 deletions django-stubs/db/backends/mysql/features.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ from typing import Any

from django.db.backends.base.features import BaseDatabaseFeatures
from django.db.backends.mysql.base import DatabaseWrapper
from django.utils.functional import cached_property

class DatabaseFeatures(BaseDatabaseFeatures):
connection: DatabaseWrapper
Expand Down Expand Up @@ -41,47 +42,47 @@ class DatabaseFeatures(BaseDatabaseFeatures):
supports_frame_range_fixed_distance: bool
supports_table_check_constraints: bool
can_return_rows_from_bulk_insert: bool
@property
@cached_property
def allows_auto_pk_0(self) -> bool: ... # type: ignore[override]
@property
@cached_property
def update_can_self_select(self) -> bool: ... # type: ignore[override]
@property
@cached_property
def can_introspect_foreign_keys(self) -> bool: ... # type: ignore[override]
@property
def introspected_field_types(self) -> dict[str, str]: ... # type: ignore [override]
@property
@cached_property
def introspected_field_types(self) -> dict[str, str]: ... # type: ignore[override]
@cached_property
def can_return_columns_from_insert(self) -> bool: ... # type: ignore[override]
@property
@cached_property
def has_zoneinfo_database(self) -> bool: ... # type: ignore[override]
@property
@cached_property
def is_sql_auto_is_null_enabled(self) -> bool: ...
@property
@cached_property
def supports_over_clause(self) -> bool: ... # type: ignore[override]
@property
@cached_property
def supports_column_check_constraints(self) -> bool: ... # type: ignore[override]
@property
@cached_property
def can_introspect_check_constraints(self) -> bool: ... # type: ignore[override]
@property
@cached_property
def has_select_for_update_skip_locked(self) -> bool: ... # type: ignore[override]
@property
@cached_property
def has_select_for_update_nowait(self) -> bool: ... # type: ignore[override]
@property
@cached_property
def has_select_for_update_of(self) -> bool: ... # type: ignore[override]
@property
@cached_property
def supports_explain_analyze(self) -> bool: ...
@property
@cached_property
def supported_explain_formats(self) -> set[str]: ... # type: ignore[override]
@property
@cached_property
def supports_transactions(self) -> bool: ...
@property
@cached_property
def ignores_table_name_case(self) -> bool: ... # type: ignore[override]
@property
@cached_property
def supports_default_in_lead_lag(self) -> bool: ... # type: ignore[override]
@property
def supports_json_field(self) -> bool: ... # type: ignore[override]
@property
@cached_property
def can_introspect_json_field(self) -> bool: ... # type: ignore[override]
@property
@cached_property
def supports_index_column_ordering(self) -> bool: ... # type: ignore[override]
@property
@cached_property
def supports_expression_indexes(self) -> bool: ... # type: ignore[override]
3 changes: 2 additions & 1 deletion django-stubs/db/backends/postgresql/base.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ from typing import Any
from django.db.backends.base.base import BaseDatabaseWrapper
from django.db.backends.utils import CursorDebugWrapper as BaseCursorDebugWrapper
from django.db.backends.utils import _ExecuteQuery
from django.utils.functional import cached_property

from .client import DatabaseClient
from .creation import DatabaseCreation
Expand Down Expand Up @@ -34,7 +35,7 @@ class DatabaseWrapper(BaseDatabaseWrapper):

# PostgreSQL backend-specific attributes.
_named_cursor_idx: int
@property
@cached_property
def pg_version(self) -> int: ...

class CursorDebugWrapper(BaseCursorDebugWrapper):
Expand Down
3 changes: 2 additions & 1 deletion django-stubs/db/backends/postgresql/features.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ from typing import Any

from django.db.backends.base.features import BaseDatabaseFeatures
from django.db.backends.postgresql.base import DatabaseWrapper
from django.utils.functional import cached_property

class DatabaseFeatures(BaseDatabaseFeatures):
connection: DatabaseWrapper
Expand Down Expand Up @@ -49,7 +50,7 @@ class DatabaseFeatures(BaseDatabaseFeatures):
def is_postgresql_11(self) -> bool: ...
@property
def is_postgresql_12(self) -> bool: ...
@property
@cached_property
def is_postgresql_13(self) -> bool: ...
has_brin_autosummarize: bool
has_websearch_to_tsquery: bool
Expand Down
9 changes: 5 additions & 4 deletions django-stubs/db/models/expressions.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ from django.db.models.query import QuerySet
from django.db.models.sql.compiler import SQLCompiler, _AsSqlType
from django.db.models.sql.query import Query
from django.utils.deconstruct import _Deconstructible
from django.utils.functional import cached_property
from typing_extensions import Self, TypeAlias

class SQLiteNumericMixin:
Expand Down Expand Up @@ -64,11 +65,11 @@ class BaseExpression:
def get_db_converters(self, connection: BaseDatabaseWrapper) -> list[Callable]: ...
def get_source_expressions(self) -> list[Any]: ...
def set_source_expressions(self, exprs: Sequence[Combinable]) -> None: ...
@property
@cached_property
def contains_aggregate(self) -> bool: ...
@property
@cached_property
def contains_over_clause(self) -> bool: ...
@property
@cached_property
def contains_column_references(self) -> bool: ...
def resolve_expression(
self,
Expand All @@ -84,7 +85,7 @@ class BaseExpression:
def field(self) -> Field: ...
@property
def output_field(self) -> Field: ...
@property
@cached_property
def convert_value(self) -> Callable: ...
def get_lookup(self, lookup: str) -> type[Lookup] | None: ...
def get_transform(self, name: str) -> type[Transform] | None: ...
Expand Down
6 changes: 3 additions & 3 deletions django-stubs/db/models/fields/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ from django.db.models.fields.reverse_related import ForeignObjectRel
from django.db.models.query_utils import Q, RegisterLookupMixin
from django.forms import Widget
from django.utils.datastructures import DictWrapper
from django.utils.functional import _Getter, _StrOrPromise
from django.utils.functional import _Getter, _StrOrPromise, cached_property
from typing_extensions import Self, TypeAlias

class Empty: ...
Expand Down Expand Up @@ -212,7 +212,7 @@ class Field(RegisterLookupMixin, Generic[_ST, _GT]):
def save_form_data(self, instance: Model, data: Any) -> None: ...
def contribute_to_class(self, cls: type[Model], name: str, private_only: bool = ...) -> None: ...
def to_python(self, value: Any) -> Any: ...
@property
@cached_property
def validators(self) -> list[validators._ValidatorCallable]: ...
def run_validators(self, value: Any) -> None: ...
def validate(self, value: Any, model_instance: Model | None) -> None: ...
Expand All @@ -231,7 +231,7 @@ class Field(RegisterLookupMixin, Generic[_ST, _GT]):
def get_default(self) -> Any: ...
def check(self, **kwargs: Any) -> list[CheckMessage]: ...
def get_col(self, alias: str, output_field: Field | None = ...) -> Col: ...
@property
@cached_property
def cached_col(self) -> Col: ...
def value_from_object(self, obj: Model) -> _GT: ...
def get_attname(self) -> str: ...
Expand Down
Loading

0 comments on commit 5bab9ae

Please sign in to comment.