Skip to content

Commit 01d062d

Browse files
committed
Extend use of simplified optionals
1 parent 360cc37 commit 01d062d

File tree

10 files changed

+54
-39
lines changed

10 files changed

+54
-39
lines changed

src/mui/v5/integrations/sqlalchemy/structures/query.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ def pages(self, total: Optional[int] = None) -> int:
198198
"""
199199
if not total:
200200
total = self.total()
201-
return int(ceil(total / float(self.per_page)))
201+
return ceil(total / float(self.per_page))
202202

203203
@property
204204
def page(self) -> int:

src/mui/v6/integrations/sqlalchemy/filter/applicators/after.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
"""The is after applicator applies the is after operator to the data."""
22

3+
from __future__ import annotations
4+
35
from datetime import datetime
46
from operator import gt
5-
from typing import Any, Optional
7+
from typing import Any
68
from zoneinfo import ZoneInfo
79

810
from mui.v6.integrations.sqlalchemy.utils import apply_timezone_to_datetime
911

1012

11-
def apply_after_operator(column: Any, value: Any, timezone: Optional[ZoneInfo]) -> Any:
13+
def apply_after_operator(column: Any, value: Any, timezone: ZoneInfo | None) -> Any:
1214
"""Handles applying the after x-data-grid operator to a column.
1315
1416
Args:

src/mui/v6/integrations/sqlalchemy/filter/applicators/before.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
"""The is before applicator applies the is before operator to the data."""
22

3+
from __future__ import annotations
4+
35
from datetime import datetime
46
from operator import lt
5-
from typing import Any, Optional
7+
from typing import Any
68
from zoneinfo import ZoneInfo
79

810
from mui.v6.integrations.sqlalchemy.utils import apply_timezone_to_datetime
911

1012

11-
def apply_before_operator(column: Any, value: Any, timezone: Optional[ZoneInfo]) -> Any:
13+
def apply_before_operator(column: Any, value: Any, timezone: ZoneInfo | None) -> Any:
1214
"""Handles applying the before x-data-grid operator to a column.
1315
1416
Args:

src/mui/v6/integrations/sqlalchemy/filter/applicators/does_not_contain.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
from sqlalchemy import not_
99

10+
1011
def apply_does_not_contain_operator(column: Any, value: Any) -> Any:
1112
"""Handles applying the doesNotContain x-data-grid operator to a column.
1213
@@ -23,4 +24,4 @@ def apply_does_not_contain_operator(column: Any, value: Any) -> Any:
2324
# only '=', '!=', 'is_()', 'is_not()', 'is_distinct_from()',
2425
# 'is_not_distinct_from()' operators can be used with None/True/False
2526
# so below have to special case them.
26-
return not_(column.contains(value if value is not None else ""))
27+
return not_(column.contains(value if value is not None else ""))

src/mui/v6/integrations/sqlalchemy/filter/applicators/is_.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
Meant as an equality check.
44
"""
55

6+
from __future__ import annotations
7+
68
from datetime import date, datetime, time
79
from operator import eq
8-
from typing import Any, Optional
10+
from typing import Any
911
from zoneinfo import ZoneInfo
1012

1113
from mui.v6.integrations.sqlalchemy.utils import (
@@ -14,7 +16,7 @@
1416
)
1517

1618

17-
def apply_is_operator(column: Any, value: Any, timezone: Optional[ZoneInfo]) -> Any:
19+
def apply_is_operator(column: Any, value: Any, timezone: ZoneInfo | None) -> Any:
1820
"""Handles applying the is x-data-grid operator to a column.
1921
2022
The is operator requires special handling when differentiating between data

src/mui/v6/integrations/sqlalchemy/filter/applicators/not_.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
"""The is not applicator applies the is not operator to the data."""
22

3+
from __future__ import annotations
4+
35
from datetime import date, datetime, time
46
from operator import ne
5-
from typing import Any, Optional
7+
from typing import Any
68
from zoneinfo import ZoneInfo
79

810
from mui.v6.integrations.sqlalchemy.utils import (
@@ -11,7 +13,7 @@
1113
)
1214

1315

14-
def apply_not_operator(column: Any, value: Any, timezone: Optional[ZoneInfo]) -> Any:
16+
def apply_not_operator(column: Any, value: Any, timezone: ZoneInfo | None) -> Any:
1517
"""Handles applying the not x-data-grid operator to a column.
1618
1719
The not operator exists on enum selections as well as datetimes. Care

src/mui/v6/integrations/sqlalchemy/filter/applicators/on_or_after.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
"""The is on or after applicator applies the is on or after operator to the data."""
22

3+
from __future__ import annotations
4+
35
from datetime import datetime
46
from operator import ge
5-
from typing import Any, Optional
7+
from typing import Any
68
from zoneinfo import ZoneInfo
79

810
from mui.v6.integrations.sqlalchemy.utils import apply_timezone_to_datetime
911

1012

1113
def apply_on_or_after_operator(
12-
column: Any, value: Any, timezone: Optional[ZoneInfo]
14+
column: Any, value: Any, timezone: ZoneInfo | None
1315
) -> Any:
1416
"""Handles applying the on or after x-data-grid operator to a column.
1517

src/mui/v6/integrations/sqlalchemy/filter/applicators/on_or_before.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
"""The is before applicator applies the is before operator to the data."""
22

3+
from __future__ import annotations
4+
35
from datetime import datetime
46
from operator import le
5-
from typing import Any, Optional
7+
from typing import Any
68
from zoneinfo import ZoneInfo
79

810
from mui.v6.integrations.sqlalchemy.utils import apply_timezone_to_datetime
911

1012

1113
def apply_on_or_before_operator(
12-
column: Any, value: Any, timezone: Optional[ZoneInfo]
14+
column: Any, value: Any, timezone: ZoneInfo | None
1315
) -> Any:
1416
"""Handles applying the on or before x-data-grid operator to a column.
1517

src/mui/v6/integrations/sqlalchemy/filter/apply_items.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
"""The apply_model module is responsible for applying a GridSortModel to a query."""
22

3-
from typing import Any, Callable, Optional, TypeVar
3+
from __future__ import annotations
4+
5+
from typing import Any, Callable, TypeVar
46
from zoneinfo import ZoneInfo
57

68
from sqlalchemy import and_, or_
@@ -56,7 +58,7 @@ def _get_link_operator(
5658

5759

5860
def apply_operator_to_column(
59-
item: GridFilterItem, resolver: Resolver, timezone: Optional[ZoneInfo]
61+
item: GridFilterItem, resolver: Resolver, timezone: ZoneInfo | None
6062
) -> Any:
6163
"""Applies the operator value represented by the GridFilterItem to the column.
6264
@@ -141,11 +143,11 @@ def apply_operator_to_column(
141143

142144

143145
def apply_filter_items_to_query_from_items(
144-
query: "Query[_Q]",
146+
query: Query[_Q],
145147
model: GridFilterModel,
146148
resolver: Resolver,
147-
timezone: Optional[ZoneInfo],
148-
) -> "Query[_Q]":
149+
timezone: ZoneInfo | None,
150+
) -> Query[_Q]:
149151
"""Applies a grid filter model's items section to a SQLAlchemy query.
150152
151153
Args:

src/mui/v6/integrations/sqlalchemy/structures/query.py

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44
total row counts.
55
"""
66

7+
from __future__ import annotations
8+
79
from math import ceil
8-
from typing import Generic, Optional, TypeVar, Union, overload
10+
from typing import Generic, TypeVar, overload
911
from zoneinfo import ZoneInfo
1012

1113
from sqlalchemy.orm import Query
@@ -30,22 +32,22 @@ class DataGridQuery(Generic[_T]):
3032
Generic (_type_): The model being retrieved by the query.
3133
"""
3234

33-
_query: "Query[_T]"
35+
_query: Query[_T]
3436
column_resovler: Resolver
35-
filter_model: Optional[GridFilterModel]
36-
pagination_model: Optional[GridPaginationModel]
37-
query: "Query[_T]"
38-
sort_model: Optional[GridSortModel]
39-
timezone: Optional[ZoneInfo]
37+
filter_model: GridFilterModel | None
38+
pagination_model: GridPaginationModel | None
39+
query: Query[_T]
40+
sort_model: GridSortModel | None
41+
timezone: ZoneInfo | None
4042

4143
def __init__( # noqa: PLR0917
4244
self,
43-
query: "Query[_T]",
45+
query: Query[_T],
4446
column_resolver: Resolver,
45-
filter_model: Optional[GridFilterModel] = None,
46-
sort_model: Optional[GridSortModel] = None,
47-
pagination_model: Optional[GridPaginationModel] = None,
48-
timezone: Optional[ZoneInfo] = None,
47+
filter_model: GridFilterModel | None = None,
48+
sort_model: GridSortModel | None = None,
49+
pagination_model: GridPaginationModel | None = None,
50+
timezone: ZoneInfo | None = None,
4951
) -> None:
5052
"""Initialize a new data grid query.
5153
@@ -73,7 +75,7 @@ def __init__( # noqa: PLR0917
7375
query = self._paginate_query(query=query)
7476
self.query = query
7577

76-
def _filter_query(self, query: "Query[_T]") -> "Query[_T]":
78+
def _filter_query(self, query: Query[_T]) -> Query[_T]:
7779
"""Applies the filter model to the query.
7880
7981
Args:
@@ -91,7 +93,7 @@ def _filter_query(self, query: "Query[_T]") -> "Query[_T]":
9193
timezone=self.timezone,
9294
)
9395

94-
def _order_query(self, query: "Query[_T]") -> "Query[_T]":
96+
def _order_query(self, query: Query[_T]) -> Query[_T]:
9597
"""Applies the sort model to the query.
9698
9799
Args:
@@ -106,7 +108,7 @@ def _order_query(self, query: "Query[_T]") -> "Query[_T]":
106108
query=query, model=self.sort_model, resolver=self.column_resovler
107109
)
108110

109-
def _paginate_query(self, query: "Query[_T]") -> "Query[_T]":
111+
def _paginate_query(self, query: Query[_T]) -> Query[_T]:
110112
"""Applies the pagination model to the query.
111113
112114
Args:
@@ -168,9 +170,7 @@ def items(self, factory: Factory[_T, _R]) -> list[_R]:
168170
List[_R]: The list of created items.
169171
"""
170172

171-
def items(
172-
self, factory: Optional[Factory[_T, _R]] = None
173-
) -> Union[list[_T], list[_R]]:
173+
def items(self, factory: Factory[_T, _R] | None = None) -> list[_T] | list[_R]:
174174
"""Returns all results of the query, after all models have been applied.
175175
176176
Args:
@@ -184,7 +184,7 @@ def items(
184184
items = self.query.all()
185185
return [factory(item) for item in items] if factory is not None else items
186186

187-
def pages(self, total: Optional[int] = None) -> int:
187+
def pages(self, total: int | None = None) -> int:
188188
"""Returns the number of pages to display all results.
189189
190190
Args:
@@ -198,7 +198,7 @@ def pages(self, total: Optional[int] = None) -> int:
198198
"""
199199
if not total:
200200
total = self.total()
201-
return int(ceil(total / float(self.per_page)))
201+
return ceil(total / float(self.per_page))
202202

203203
@property
204204
def page(self) -> int:

0 commit comments

Comments
 (0)