Skip to content

Commit 80c3bc1

Browse files
Update typing annotations for Python 3.9+
1 parent ddb3c11 commit 80c3bc1

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+309
-348
lines changed

benchmarks/plot.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def grouped_barplot(df, cat, subcat, val, err, subcats=None, **kwargs):
2424
x + offsets[i],
2525
dfg[val].values,
2626
width=width,
27-
label="{}".format(gr),
27+
label=f"{gr}",
2828
yerr=dfg[err].values,
2929
capsize=6,
3030
**kwargs,

docsite/docs/guides/integration.ipynb

+3-2
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,8 @@
141141
}
142142
],
143143
"source": [
144-
"from typing import Iterable, List, Optional\n",
144+
"from collections.abc import Iterable\n",
145+
"from typing import Optional\n",
145146
"\n",
146147
"from sklearn.base import BaseEstimator, TransformerMixin\n",
147148
"from sklearn.linear_model import LinearRegression\n",
@@ -181,7 +182,7 @@
181182
"\n",
182183
" def get_feature_names_out(\n",
183184
" self, input_features: Optional[Iterable[str]] = None\n",
184-
" ) -> List[str]:\n",
185+
" ) -> list[str]:\n",
185186
" \"\"\"\n",
186187
" Expose model spec column names to scikit learn to allow column transforms later in the pipeline.\n",
187188
" \"\"\"\n",

formulaic/formula.py

+18-26
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,12 @@
22

33
import sys
44
from abc import ABCMeta, abstractmethod
5-
from collections.abc import MutableSequence
5+
from collections.abc import Generator, Iterable, Mapping, MutableSequence
66
from enum import Enum
77
from typing import (
88
Any,
99
Callable,
10-
Dict,
11-
Generator,
12-
Iterable,
13-
List,
14-
Mapping,
1510
Optional,
16-
Set,
17-
Tuple,
18-
Type,
1911
TypeVar,
2012
Union,
2113
cast,
@@ -38,10 +30,10 @@
3830
FormulaSpec: TypeAlias = Union[
3931
"Formula",
4032
str,
41-
List[Union[str, Term]],
42-
Set[Union[str, Term]],
43-
Dict[str, "FormulaSpec"],
44-
Tuple["FormulaSpec", ...],
33+
list[Union[str, Term]],
34+
set[Union[str, Term]],
35+
dict[str, "FormulaSpec"],
36+
tuple["FormulaSpec", ...],
4537
Structured["FormulaSpec"],
4638
]
4739
_SelfType = TypeVar("_SelfType", bound="Formula")
@@ -281,7 +273,7 @@ def get_model_matrix(
281273
self,
282274
data: Any,
283275
context: Optional[Mapping[str, Any]] = None,
284-
drop_rows: Optional[Set[int]] = None,
276+
drop_rows: Optional[set[int]] = None,
285277
**spec_overrides: Any,
286278
) -> Union[ModelMatrix, Structured[ModelMatrix]]:
287279
"""
@@ -302,7 +294,7 @@ def get_model_matrix(
302294

303295
@property
304296
@abstractmethod
305-
def required_variables(self) -> Set[Variable]:
297+
def required_variables(self) -> set[Variable]:
306298
"""
307299
The set of variables required to be in the data to materialize this
308300
formula.
@@ -504,7 +496,7 @@ def get_model_matrix(
504496
self,
505497
data: Any,
506498
context: Optional[Mapping[str, Any]] = None,
507-
drop_rows: Optional[Set[int]] = None,
499+
drop_rows: Optional[set[int]] = None,
508500
**spec_overrides: Any,
509501
) -> Union[ModelMatrix, Structured[ModelMatrix]]:
510502
"""
@@ -529,7 +521,7 @@ def get_model_matrix(
529521
)
530522

531523
@property
532-
def required_variables(self) -> Set[Variable]:
524+
def required_variables(self) -> set[Variable]:
533525
"""
534526
The set of variables required in the data order to materialize this
535527
formula.
@@ -542,7 +534,7 @@ def required_variables(self) -> Set[Variable]:
542534
evaluation context rather than the data context.
543535
"""
544536

545-
variables: List[Variable] = [
537+
variables: list[Variable] = [
546538
variable
547539
for term in self.__terms
548540
for factor in term.factors
@@ -605,11 +597,11 @@ def _map(
605597
self,
606598
func: Union[
607599
Callable[[SimpleFormula], Any],
608-
Callable[[SimpleFormula, Tuple[Union[str, int], ...]], Any],
600+
Callable[[SimpleFormula, tuple[Union[str, int], ...]], Any],
609601
],
610602
recurse: bool = True,
611-
as_type: Optional[Type[Structured]] = None,
612-
_context: Tuple[Union[str, int], ...] = (),
603+
as_type: Optional[type[Structured]] = None,
604+
_context: tuple[Union[str, int], ...] = (),
613605
) -> Any:
614606
try:
615607
return func(self, ()) # type: ignore
@@ -629,7 +621,7 @@ def _flatten(self) -> Generator[SimpleFormula, None, None]:
629621
as_of=(1, 1),
630622
removed_in=(2, 0),
631623
)
632-
def _to_dict(self) -> Dict[str, SimpleFormula]:
624+
def _to_dict(self) -> dict[str, SimpleFormula]:
633625
return {"root": self}
634626

635627
@deprecated(
@@ -726,7 +718,7 @@ def get_model_matrix(
726718
self,
727719
data: Any,
728720
context: Optional[Mapping[str, Any]] = None,
729-
drop_rows: Optional[Set[int]] = None,
721+
drop_rows: Optional[set[int]] = None,
730722
**spec_overrides: Any,
731723
) -> Union[ModelMatrix, Structured[ModelMatrix]]:
732724
"""
@@ -751,7 +743,7 @@ def get_model_matrix(
751743
)
752744

753745
@property
754-
def required_variables(self) -> Set[Variable]:
746+
def required_variables(self) -> set[Variable]:
755747
"""
756748
The set of variables required in the data order to materialize this
757749
formula.
@@ -764,7 +756,7 @@ def required_variables(self) -> Set[Variable]:
764756
evaluation context rather than the data context.
765757
"""
766758

767-
variables: List[Variable] = []
759+
variables: list[Variable] = []
768760

769761
# Recurse through formula to collect all variables
770762
self._map(
@@ -797,7 +789,7 @@ def differentiate( # pylint: disable=redefined-builtin
797789
)
798790

799791
# Ensure pickling never includes context
800-
def __getstate__(self) -> Tuple[None, Dict[str, Any]]:
792+
def __getstate__(self) -> tuple[None, dict[str, Any]]:
801793
slots = self.__slots__ + Structured.__slots__
802794
return (
803795
None,

formulaic/materializers/arrow.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from __future__ import annotations
22

3-
from collections.abc import Mapping
4-
from typing import TYPE_CHECKING, Any, Dict, Iterator, Sequence
3+
from collections.abc import Iterator, Mapping, Sequence
4+
from typing import TYPE_CHECKING, Any
55

66
import pandas
77
from interface_meta import override
@@ -30,7 +30,7 @@ class LazyArrowTableProxy(Mapping):
3030
def __init__(self, table: pyarrow.Table):
3131
self.table = table
3232
self.column_names = set(self.table.column_names)
33-
self._cache: Dict[str, pandas.Series] = {}
33+
self._cache: dict[str, pandas.Series] = {}
3434
self.index = pandas.RangeIndex(len(table))
3535

3636
def __contains__(self, value: Any) -> Any:

0 commit comments

Comments
 (0)