Skip to content

Commit

Permalink
Remove typing as tp import (#2101)
Browse files Browse the repository at this point in the history
This was done to resolve conflicts between typing.List and polars.datatypes.List. Given that we rarely use the latter in the code, but mostly typing.List, making this change. Makes the code more pythonic, especially as in newer python versions we could even do `list[int]` rather than `List[int]`.
  • Loading branch information
zundertj committed Dec 21, 2021
1 parent 3dc2264 commit b08be5e
Show file tree
Hide file tree
Showing 6 changed files with 199 additions and 210 deletions.
41 changes: 19 additions & 22 deletions py-polars/polars/internals/expr.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import copy
import typing as tp
from datetime import date, datetime, timedelta
from typing import Any, Callable, Optional, Sequence, Type, Union
from typing import Any, Callable, List, Optional, Sequence, Tuple, Type, Union

import numpy as np

Expand Down Expand Up @@ -29,8 +28,8 @@

def _selection_to_pyexpr_list(
exprs: Union[str, "Expr", Sequence[Union[str, "Expr"]], "pli.Series"]
) -> tp.List["PyExpr"]:
pyexpr_list: tp.List[PyExpr]
) -> List["PyExpr"]:
pyexpr_list: List[PyExpr]
if isinstance(exprs, Sequence) and not isinstance(exprs, str):
pyexpr_list = []
for expr in exprs:
Expand Down Expand Up @@ -287,7 +286,7 @@ def alias(self, name: str) -> "Expr":

def exclude(
self,
columns: Union[str, tp.List[str], Type[DataType], Sequence[Type[DataType]]],
columns: Union[str, List[str], Type[DataType], Sequence[Type[DataType]]],
) -> "Expr":
"""
Exclude certain columns from a wildcard/regex selection.
Expand Down Expand Up @@ -806,8 +805,8 @@ def arg_min(self) -> "Expr":

def sort_by(
self,
by: Union["Expr", str, tp.List[Union["Expr", str]]],
reverse: Union[bool, tp.List[bool]] = False,
by: Union["Expr", str, List[Union["Expr", str]]],
reverse: Union[bool, List[bool]] = False,
) -> "Expr":
"""
Sort this column by the ordering of another column, or multiple other columns.
Expand All @@ -830,9 +829,7 @@ def sort_by(

return wrap_expr(self._pyexpr.sort_by(by, reverse))

def take(
self, index: Union[tp.List[int], "Expr", "pli.Series", np.ndarray]
) -> "Expr":
def take(self, index: Union[List[int], "Expr", "pli.Series", np.ndarray]) -> "Expr":
"""
Take values by index.
Expand Down Expand Up @@ -1014,7 +1011,7 @@ def list(self) -> "Expr":
"""
return wrap_expr(self._pyexpr.list())

def over(self, expr: Union[str, "Expr", tp.List[Union["Expr", str]]]) -> "Expr":
def over(self, expr: Union[str, "Expr", List[Union["Expr", str]]]) -> "Expr":
"""
Apply window function over a subgroup.
This is similar to a groupby + aggregation + self join.
Expand Down Expand Up @@ -1274,7 +1271,7 @@ def pow(self, exponent: float) -> "Expr":
"""
return wrap_expr(self._pyexpr.pow(exponent))

def is_in(self, other: Union["Expr", tp.List[Any]]) -> "Expr":
def is_in(self, other: Union["Expr", List[Any]]) -> "Expr":
"""
Check if elements of this Series are in the right Series, or List values of the right Series.
Expand Down Expand Up @@ -1426,7 +1423,7 @@ def interpolate(self) -> "Expr":
def rolling_min(
self,
window_size: int,
weights: Optional[tp.List[float]] = None,
weights: Optional[List[float]] = None,
min_periods: Optional[int] = None,
center: bool = False,
) -> "Expr":
Expand Down Expand Up @@ -1458,7 +1455,7 @@ def rolling_min(
def rolling_max(
self,
window_size: int,
weights: Optional[tp.List[float]] = None,
weights: Optional[List[float]] = None,
min_periods: Optional[int] = None,
center: bool = False,
) -> "Expr":
Expand Down Expand Up @@ -1490,7 +1487,7 @@ def rolling_max(
def rolling_mean(
self,
window_size: int,
weights: Optional[tp.List[float]] = None,
weights: Optional[List[float]] = None,
min_periods: Optional[int] = None,
center: bool = False,
) -> "Expr":
Expand Down Expand Up @@ -1551,7 +1548,7 @@ def rolling_mean(
def rolling_sum(
self,
window_size: int,
weights: Optional[tp.List[float]] = None,
weights: Optional[List[float]] = None,
min_periods: Optional[int] = None,
center: bool = False,
) -> "Expr":
Expand Down Expand Up @@ -1583,7 +1580,7 @@ def rolling_sum(
def rolling_std(
self,
window_size: int,
weights: Optional[tp.List[float]] = None,
weights: Optional[List[float]] = None,
min_periods: Optional[int] = None,
center: bool = False,
) -> "Expr":
Expand Down Expand Up @@ -1616,7 +1613,7 @@ def rolling_std(
def rolling_var(
self,
window_size: int,
weights: Optional[tp.List[float]] = None,
weights: Optional[List[float]] = None,
min_periods: Optional[int] = None,
center: bool = False,
) -> "Expr":
Expand Down Expand Up @@ -2068,7 +2065,7 @@ def arctan(self) -> "Expr":
"""
return np.arctan(self) # type: ignore

def reshape(self, dims: tp.Tuple[int, ...]) -> "Expr":
def reshape(self, dims: Tuple[int, ...]) -> "Expr":
"""
Reshape this Expr to a flat series, shape: (len,)
or a List series, shape: (rows, cols)
Expand Down Expand Up @@ -2178,7 +2175,7 @@ def unique(self) -> "Expr":
"""
return wrap_expr(self._pyexpr.lst_unique())

def concat(self, other: Union[tp.List[Union[Expr, str]], Expr, str]) -> "Expr":
def concat(self, other: Union[List[Union[Expr, str]], Expr, str]) -> "Expr":
"""
Concat the arrays in a Series dtype List in linear time.
Expand All @@ -2187,7 +2184,7 @@ def concat(self, other: Union[tp.List[Union[Expr, str]], Expr, str]) -> "Expr":
other
Columns to concat into a List Series
"""
other_list: tp.List[Union[Expr, str]]
other_list: List[Union[Expr, str]]
if not isinstance(other, list):
other_list = [other]
else:
Expand Down Expand Up @@ -2748,7 +2745,7 @@ def timestamp(self) -> Expr:


def expr_to_lit_or_expr(
expr: Union[Expr, bool, int, float, str, tp.List[Expr], tp.List[str], "pli.Series"],
expr: Union[Expr, bool, int, float, str, List[Expr], List[str], "pli.Series"],
str_to_lit: bool = True,
) -> Expr:
"""
Expand Down

0 comments on commit b08be5e

Please sign in to comment.