Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Add flake8-type-checking extension #4230

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions py-polars/.flake8
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[flake8]
max-line-length = 88
type-checking-exempt-modules = typing_extensions
docstring-convention=all
extend-ignore =
# Satisfy black: https://black.readthedocs.io/en/stable/guides/using_black_with_other_tools.html#flake8
Expand All @@ -11,6 +12,8 @@ extend-ignore =
D1, D400, D205,
# flake8-simplify
SIM102, SIM117,
# flake8-type-checking
TC006,

per-file-ignores =
__init__.py:F401
Expand Down
1 change: 1 addition & 0 deletions py-polars/build.requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ flake8-bugbear==22.7.1
flake8-comprehensions==3.10.0
flake8-docstrings==1.6.0
flake8-simplify==0.19.3
flake8-type-checking==2.1.0
sphinx==4.2.0
pydata-sphinx-theme==0.6.3
sphinx-panels==0.6.0
Expand Down
6 changes: 4 additions & 2 deletions py-polars/polars/_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@

import os
from textwrap import dedent
from types import TracebackType
from typing import Iterable
from typing import TYPE_CHECKING, Iterable

from polars.datatypes import Object

if TYPE_CHECKING:
from types import TracebackType


class Tag:
"""Class for representing an HTML tag."""
Expand Down
6 changes: 4 additions & 2 deletions py-polars/polars/datatypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import ctypes
import sys
from datetime import date, datetime, time, timedelta
from typing import Any, Dict, Optional, Sequence, Tuple, Type, Union
from typing import TYPE_CHECKING, Any, Dict, Optional, Sequence, Tuple, Type, Union

try:
import pyarrow as pa
Expand All @@ -12,7 +12,6 @@
except ImportError:
_PYARROW_AVAILABLE = False

from _ctypes import _SimpleCData # type: ignore[import]

try:
from polars.polars import dtype_str_repr
Expand All @@ -22,6 +21,9 @@
except ImportError:
_DOCUMENTING = True

if TYPE_CHECKING:
from _ctypes import _SimpleCData # type: ignore[import]


def get_idx_type() -> Type[DataType]:
"""
Expand Down
5 changes: 3 additions & 2 deletions py-polars/polars/internals/datatypes.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from __future__ import annotations

from typing import Union
from typing import TYPE_CHECKING, Union

from polars import internals as pli
if TYPE_CHECKING:
from polars import internals as pli

IntoExpr = Union[int, float, str, "pli.Expr", "pli.Series"]
12 changes: 4 additions & 8 deletions py-polars/polars/internals/expr.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import math
import random
from datetime import date, datetime, timedelta
from typing import Any, Callable, List, Sequence
from typing import TYPE_CHECKING, Any, Callable, List, Sequence

from polars import internals as pli
from polars.datatypes import (
Expand All @@ -21,20 +21,16 @@
)
from polars.utils import _timedelta_to_pl_duration

try:
from polars.polars import PyExpr

_DOCUMENTING = False
except ImportError:
_DOCUMENTING = True

try:
import numpy as np

_NUMPY_AVAILABLE = True
except ImportError:
_NUMPY_AVAILABLE = False

if TYPE_CHECKING:
from polars.polars import PyExpr


def selection_to_pyexpr_list(
exprs: str | Expr | Sequence[str | Expr | pli.Series] | pli.Series,
Expand Down
14 changes: 12 additions & 2 deletions py-polars/polars/internals/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,17 @@
from contextlib import contextmanager
from io import BytesIO, StringIO
from pathlib import Path
from typing import Any, BinaryIO, ContextManager, Iterator, TextIO, overload
from typing import (
TYPE_CHECKING,
Any,
BinaryIO,
ContextManager,
Iterator,
TextIO,
overload,
)
from urllib.request import urlopen

from polars.datatypes import DataType
from polars.utils import format_path

try:
Expand All @@ -24,6 +31,9 @@
except ImportError:
pass

if TYPE_CHECKING:
from polars.datatypes import DataType


def _process_http_file(path: str) -> BytesIO:
with urlopen(path) as f:
Expand Down
5 changes: 4 additions & 1 deletion py-polars/polars/string_cache.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from __future__ import annotations

from types import TracebackType
from typing import TYPE_CHECKING

try:
from polars.polars import toggle_string_cache as pytoggle_string_cache
Expand All @@ -9,6 +9,9 @@
except ImportError:
_DOCUMENTING = True

if TYPE_CHECKING:
from types import TracebackType


class StringCache:
"""
Expand Down
6 changes: 4 additions & 2 deletions py-polars/polars/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
import sys
import warnings
from datetime import date, datetime, time, timedelta, timezone
from pathlib import Path
from typing import Any, Callable, Iterable, Sequence
from typing import TYPE_CHECKING, Any, Callable, Iterable, Sequence

from polars.datatypes import DataType, Date, Datetime

Expand All @@ -31,6 +30,9 @@
else:
from typing_extensions import TypeGuard

if TYPE_CHECKING:
from pathlib import Path


def _process_null_values(
null_values: None | str | list[str] | dict[str, str] = None,
Expand Down
5 changes: 4 additions & 1 deletion py-polars/tests/io/test_csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,16 @@
import zlib
from datetime import date
from pathlib import Path
from typing import TYPE_CHECKING

import pytest

import polars as pl
from polars import DataType
from polars.testing import assert_frame_equal_local_categoricals

if TYPE_CHECKING:
from polars import DataType


def test_quoted_date() -> None:
csv = textwrap.dedent(
Expand Down
6 changes: 5 additions & 1 deletion py-polars/tests/test_lazy.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
from __future__ import annotations

from typing import TYPE_CHECKING

import numpy as np
import pytest
from _pytest.capture import CaptureFixture

import polars as pl
from polars import col, lit, map_binary, when
from polars.testing import assert_frame_equal

if TYPE_CHECKING:
from _pytest.capture import CaptureFixture


def test_lazy() -> None:
df = pl.DataFrame({"a": [1, 2, 3], "b": [1.0, 2.0, 3.0]})
Expand Down