Skip to content

Commit

Permalink
Add mypy optional error codes (#4054)
Browse files Browse the repository at this point in the history
  • Loading branch information
stinodego committed Jul 18, 2022
1 parent 20134a9 commit d707480
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 18 deletions.
2 changes: 1 addition & 1 deletion py-polars/polars/internals/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ def concat(

def _ensure_datetime(value: date | datetime) -> tuple[datetime, bool]:
is_date_type = False
if isinstance(value, date) and not isinstance(value, datetime):
if not isinstance(value, datetime):
value = datetime(value.year, value.month, value.day)
is_date_type = True
return value, is_date_type
Expand Down
31 changes: 16 additions & 15 deletions py-polars/polars/internals/lazy_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -1484,7 +1484,7 @@ def join(
def with_columns(
self: LDF,
exprs: pli.Expr | pli.Series | Sequence[pli.Expr | pli.Series] | None = None,
**named_exprs: pli.Expr | pli.Series,
**named_exprs: pli.Expr | pli.Series | str,
) -> LDF:
"""
Add or overwrite multiple columns in a DataFrame.
Expand Down Expand Up @@ -1534,21 +1534,22 @@ def with_columns(
>>> ldf.with_columns(
... d=pl.col("a") * pl.col("b"),
... e=pl.col("c").is_not(),
... f="foo",
... ).collect()
shape: (4, 5)
┌─────┬──────┬───────┬──────┬───────┐
│ a ┆ b ┆ c ┆ d ┆ e │
│ --- ┆ --- ┆ --- ┆ --- ┆ --- │
│ i64 ┆ f64 ┆ bool ┆ f64 ┆ bool │
╞═════╪══════╪═══════╪══════╪═══════╡
│ 1 ┆ 0.5 ┆ true ┆ 0.5 ┆ false │
├╌╌╌╌╌┼╌╌╌╌╌╌┼╌╌╌╌╌╌╌┼╌╌╌╌╌╌┼╌╌╌╌╌╌╌┤
│ 2 ┆ 4.0 ┆ true ┆ 8.0 ┆ false │
├╌╌╌╌╌┼╌╌╌╌╌╌┼╌╌╌╌╌╌╌┼╌╌╌╌╌╌┼╌╌╌╌╌╌╌┤
│ 3 ┆ 10.0 ┆ false ┆ 30.0 ┆ true │
├╌╌╌╌╌┼╌╌╌╌╌╌┼╌╌╌╌╌╌╌┼╌╌╌╌╌╌┼╌╌╌╌╌╌╌┤
│ 4 ┆ 13.0 ┆ true ┆ 52.0 ┆ false │
└─────┴──────┴───────┴──────┴───────┘
shape: (4, 6)
┌─────┬──────┬───────┬──────┬───────┬─────
│ a ┆ b ┆ c ┆ d ┆ e ┆ f
│ --- ┆ --- ┆ --- ┆ --- ┆ --- ┆ ---
│ i64 ┆ f64 ┆ bool ┆ f64 ┆ bool ┆ str
╞═════╪══════╪═══════╪══════╪═══════╪═════
│ 1 ┆ 0.5 ┆ true ┆ 0.5 ┆ false ┆ foo
├╌╌╌╌╌┼╌╌╌╌╌╌┼╌╌╌╌╌╌╌┼╌╌╌╌╌╌┼╌╌╌╌╌╌╌┼╌╌╌╌╌
│ 2 ┆ 4.0 ┆ true ┆ 8.0 ┆ false ┆ foo
├╌╌╌╌╌┼╌╌╌╌╌╌┼╌╌╌╌╌╌╌┼╌╌╌╌╌╌┼╌╌╌╌╌╌╌┼╌╌╌╌╌
│ 3 ┆ 10.0 ┆ false ┆ 30.0 ┆ true ┆ foo
├╌╌╌╌╌┼╌╌╌╌╌╌┼╌╌╌╌╌╌╌┼╌╌╌╌╌╌┼╌╌╌╌╌╌╌┼╌╌╌╌╌
│ 4 ┆ 13.0 ┆ true ┆ 52.0 ┆ false ┆ foo
└─────┴──────┴───────┴──────┴───────┴─────
"""
if named_exprs and not Config.with_columns_kwargs:
raise RuntimeError(
Expand Down
2 changes: 1 addition & 1 deletion py-polars/polars/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ def read_csv(

if columns and not has_header:
for column in columns:
if isinstance(column, str) and not column.startswith("column_"):
if not column.startswith("column_"):
raise ValueError(
'Specified column names do not start with "column_", '
"but autogenerated header names were requested."
Expand Down
5 changes: 4 additions & 1 deletion py-polars/polars/testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,10 @@ def assert_series_equal(
"""
obj = "Series"

if not (isinstance(left, Series) and isinstance(right, Series)):
if not (
isinstance(left, Series) # type: ignore[redundant-expr]
and isinstance(right, Series)
):
raise_assert_detail(obj, "Type mismatch", type(left), type(right))

if left.shape != right.shape:
Expand Down
6 changes: 6 additions & 0 deletions py-polars/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ warn_redundant_casts = true
# no_implicit_reexport = true
# strict_equality = true
# TODO: When all flags are enabled, replace by strict = true
enable_error_code = [
"redundant-expr",
"truthy-bool",
# TODO: Uncomment error below and fix mypy errors
# "ignore-without-code",
]

[[tool.mypy.overrides]]
module = ["pyarrow.*", "polars.polars", "matplotlib.*", "fsspec.*", "connectorx", "IPython.*"]
Expand Down

0 comments on commit d707480

Please sign in to comment.