Skip to content

Commit

Permalink
feat(lint): enable ruff TCH (flake8-type-checking) rules (#1125)
Browse files Browse the repository at this point in the history
  • Loading branch information
shiftinv authored Oct 26, 2023
1 parent c2a8dde commit ffb7526
Show file tree
Hide file tree
Showing 12 changed files with 48 additions and 32 deletions.
3 changes: 1 addition & 2 deletions disnake/ext/commands/common_bot_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import asyncio
import collections.abc
import importlib.machinery
import importlib.util
import logging
import os
Expand All @@ -19,8 +20,6 @@
from .cog import Cog

if TYPE_CHECKING:
import importlib.machinery

from ._types import CoroFunc
from .bot import AutoShardedBot, AutoShardedInteractionBot, Bot, InteractionBot
from .help import HelpCommand
Expand Down
3 changes: 2 additions & 1 deletion docs/extensions/attributetable.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@
from sphinx.util.docutils import SphinxDirective

if TYPE_CHECKING:
from _types import SphinxExtensionMeta
from sphinx.application import Sphinx
from sphinx.environment import BuildEnvironment
from sphinx.util.typing import OptionSpec
from sphinx.writers.html import HTMLTranslator

from ._types import SphinxExtensionMeta


class attributetable(nodes.General, nodes.Element):
pass
Expand Down
3 changes: 2 additions & 1 deletion docs/extensions/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@
from sphinx.environment.adapters.indexentries import IndexEntries

if TYPE_CHECKING:
from _types import SphinxExtensionMeta
from docutils import nodes
from sphinx.application import Sphinx
from sphinx.config import Config
from sphinx.writers.html5 import HTML5Translator

from ._types import SphinxExtensionMeta

if TYPE_CHECKING:
translator_base = HTML5Translator
else:
Expand Down
3 changes: 2 additions & 1 deletion docs/extensions/exception_hierarchy.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@
from docutils.parsers.rst import Directive

if TYPE_CHECKING:
from _types import SphinxExtensionMeta
from sphinx.application import Sphinx
from sphinx.writers.html import HTMLTranslator

from ._types import SphinxExtensionMeta


class exception_hierarchy(nodes.General, nodes.Element):
pass
Expand Down
3 changes: 2 additions & 1 deletion docs/extensions/fulltoc.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@

from typing import TYPE_CHECKING, List, cast

from _types import SphinxExtensionMeta
from docutils import nodes
from sphinx import addnodes

Expand All @@ -40,6 +39,8 @@
from sphinx.builders.html import StandaloneHTMLBuilder
from sphinx.environment import BuildEnvironment

from ._types import SphinxExtensionMeta

# {prefix: index_doc} mapping
# Any document that matches `prefix` will use `index_doc`'s toctree instead.
GROUPED_SECTIONS = {"api/": "api/index", "ext/commands/api/": "ext/commands/api/index"}
Expand Down
3 changes: 2 additions & 1 deletion docs/extensions/nitpick_file_ignorer.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@
from sphinx.util import logging as sphinx_logging

if TYPE_CHECKING:
from _types import SphinxExtensionMeta
from sphinx.application import Sphinx

from ._types import SphinxExtensionMeta


class NitpickFileIgnorer(logging.Filter):
def __init__(self, app: Sphinx) -> None:
Expand Down
7 changes: 5 additions & 2 deletions docs/extensions/redirects.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
# SPDX-License-Identifier: MIT
from __future__ import annotations

import json
from pathlib import Path
from typing import Dict
from typing import TYPE_CHECKING, Dict

from _types import SphinxExtensionMeta
from sphinx.application import Sphinx
from sphinx.util.fileutil import copy_asset_file

if TYPE_CHECKING:
from ._types import SphinxExtensionMeta

SCRIPT_PATH = "_templates/api_redirect.js_t"


Expand Down
3 changes: 2 additions & 1 deletion docs/extensions/resourcelinks.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@
from sphinx.util.nodes import split_explicit_title

if TYPE_CHECKING:
from _types import SphinxExtensionMeta
from docutils.nodes import Node, system_message
from docutils.parsers.rst.states import Inliner
from sphinx.application import Sphinx
from sphinx.util.typing import RoleFunction

from ._types import SphinxExtensionMeta


def make_link_role(resource_links: Dict[str, str]) -> RoleFunction:
def role(
Expand Down
9 changes: 8 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ select = [
# "RET", # flake8-return
# "SIM", # flake8-simplify
"TID251", # flake8-tidy-imports, replaces S404
# "TCH", # flake8-type-checking
"TCH", # flake8-type-checking
"RUF", # ruff specific exceptions
"PT", # flake8-pytest-style
"Q", # flake8-quotes
Expand Down Expand Up @@ -198,6 +198,13 @@ ignore = [
# outer loop variables are overwritten by inner assignment target, these are mostly intentional
"PLW2901",

# ignore imports that could be moved into type-checking blocks
# (no real advantage other than possibly avoiding cycles,
# but can be dangerous in places where we need to parse signatures)
"TCH001",
"TCH002",
"TCH003",

# temporary disables, to fix later
"D205", # blank line required between summary and description
"D401", # first line of docstring should be in imperative mood
Expand Down
12 changes: 1 addition & 11 deletions tests/ext/commands/test_core.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,10 @@
# SPDX-License-Identifier: MIT

from typing import TYPE_CHECKING
from typing_extensions import assert_type

from disnake.ext import commands
from tests.helpers import reveal_type

if TYPE_CHECKING:
from typing_extensions import assert_type

# NOTE: using undocumented `expected_text` parameter of pyright instead of `assert_type`,
# as `assert_type` can't handle bound ParamSpecs
reveal_type(
42, # type: ignore
expected_text="str", # type: ignore
)


class CustomContext(commands.Context):
...
Expand Down
9 changes: 9 additions & 0 deletions tests/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,15 @@ def reveal_type(*args, **kwargs) -> None:
raise RuntimeError


if TYPE_CHECKING:
# NOTE: using undocumented `expected_text` parameter of pyright instead of `assert_type`,
# as `assert_type` can't handle bound ParamSpecs
reveal_type(
42, # type: ignore # suppress "revealed type is ..." output
expected_text="str", # type: ignore # ensure the functionality we want still works as expected
)


CallableT = TypeVar("CallableT", bound=Callable)


Expand Down
22 changes: 12 additions & 10 deletions tests/ui/test_action_row.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,20 @@
from unittest import mock

import pytest
from typing_extensions import assert_type

import disnake
from disnake.ui import ActionRow, Button, StringSelect, TextInput, WrappedComponent
from disnake.ui import (
ActionRow,
Button,
MessageUIComponent,
ModalUIComponent,
StringSelect,
TextInput,
WrappedComponent,
)
from disnake.ui.action_row import components_to_dict, components_to_rows

if TYPE_CHECKING:
from typing_extensions import assert_type

from disnake.ui import MessageUIComponent, ModalUIComponent


button1 = Button()
button2 = Button()
button3 = Button()
Expand Down Expand Up @@ -133,9 +136,8 @@ def test_with_components(self) -> None:
row_msg = ActionRow.with_message_components()
assert list(row_msg.children) == []

if TYPE_CHECKING:
assert_type(row_modal, ActionRow[ModalUIComponent])
assert_type(row_msg, ActionRow[MessageUIComponent])
assert_type(row_modal, ActionRow[ModalUIComponent])
assert_type(row_msg, ActionRow[MessageUIComponent])

def test_rows_from_message(self) -> None:
rows = [
Expand Down

0 comments on commit ffb7526

Please sign in to comment.