Skip to content

Commit

Permalink
chore: use ruff over flake8 and isort (#56)
Browse files Browse the repository at this point in the history
* chore: add ruff for linting over flake8

* chore: eliminate isort with ruff

* chore: add pydocstyle plugin to ruff

* chore: add pyupgrade

* chore: add annotation plugin for ruff

* chore: add bandit plugin to ruff

* chore: add blind exception to ruff

* chore: add boolean trap plugin to ruff

* chore: add bugbear plugin to ruff

* chore: add comprehensions plugin to ruff

* chore: add time plugin to ruff

* chore: add errmsg plugin to ruff

* chore: add even more plugins

* chore: add simplify plugin to ruff

* chore: add type checking block plugin to ruff

* chore: add unused arg plugin to ruff

* chore: add tiny plugins to ruff

* chore: add pylint plugins to ruff

* chore: add ruff specific rules
  • Loading branch information
ooliver1 authored Feb 22, 2023
1 parent 77f7bf1 commit e1741fa
Show file tree
Hide file tree
Showing 34 changed files with 474 additions and 388 deletions.
7 changes: 0 additions & 7 deletions .flake8

This file was deleted.

16 changes: 5 additions & 11 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,12 @@ repos:
- id: black
name: Running black in all files.

- repo: https://github.com/pycqa/isort
rev: 5.12.0
- repo: https://github.com/charliermarsh/ruff-pre-commit
rev: v0.0.251
hooks:
- id: isort
name: Running isort in all files.

- repo: https://github.com/PyCQA/flake8
rev: 6.0.0
hooks:
- id: flake8
name: Running flake8 in all files.
additional_dependencies: [flake8-isort, Flake8-pyproject]
- id: ruff
args: [--fix, --exit-non-zero-on-fix]
name: Running ruff in all files.

- repo: https://github.com/ariebovenberg/slotscheck
rev: v0.16.4
Expand Down
7 changes: 4 additions & 3 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@

import os
import sys
from typing import Any, Optional
from typing import TYPE_CHECKING, Any, Optional

from sphinx.config import Config
if TYPE_CHECKING:
from sphinx.config import Config

os.environ["MAFIC_IGNORE_LIBRARY_CHECK"] = "1"

Expand Down Expand Up @@ -89,7 +90,7 @@
}


def typehints_formatter(annotation: Any, _: Config) -> str | None:
def typehints_formatter(annotation: Any, _: Config) -> str | None: # noqa: ANN401
return aliases.get(annotation, None)


Expand Down
85 changes: 52 additions & 33 deletions docs/extensions/attributetable.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,18 @@
import inspect
import re
from collections import OrderedDict
from typing import Callable, NamedTuple, Optional
from typing import TYPE_CHECKING, NamedTuple

from docutils import nodes
from docutils.nodes import document
from sphinx import addnodes
from sphinx.application import Sphinx
from sphinx.environment import BuildEnvironment
from sphinx.locale import _
from sphinx.util.docutils import SphinxDirective
from sphinx.writers.html5 import HTML5Translator

if TYPE_CHECKING:
from docutils.nodes import document
from sphinx.application import Sphinx
from sphinx.environment import BuildEnvironment
from sphinx.writers.html5 import HTML5Translator


class attributetable(nodes.General, nodes.Element):
Expand All @@ -44,48 +46,62 @@ class attributetable_item(nodes.Part, nodes.Element):
pass


def visit_attributetable_node(self: HTML5Translator, node: attributetable):
def visit_attributetable_node(self: HTML5Translator, node: attributetable) -> None:
class_ = node["python-class"]
self.body.append(f'<div class="py-attribute-table" data-move-to-id="{class_}">')


def visit_attributetablecolumn_node(self: HTML5Translator, node: attributetablecolumn):
def visit_attributetablecolumn_node(
self: HTML5Translator, node: attributetablecolumn
) -> None:
self.body.append(self.starttag(node, "div", CLASS="py-attribute-table-column"))


def visit_attributetabletitle_node(self: HTML5Translator, node: attributetabletitle):
def visit_attributetabletitle_node(
self: HTML5Translator, node: attributetabletitle
) -> None:
self.body.append(self.starttag(node, "span"))


def visit_attributetablebadge_node(self: HTML5Translator, node: attributetablebadge):
def visit_attributetablebadge_node(
self: HTML5Translator, node: attributetablebadge
) -> None:
attributes = {
"class": "py-attribute-table-badge",
"title": node["badge-type"],
}
self.body.append(self.starttag(node, "span", **attributes))


def visit_attributetable_item_node(self: HTML5Translator, node: attributetable):
def visit_attributetable_item_node(self: HTML5Translator, node: attributetable) -> None:
self.body.append(self.starttag(node, "li", CLASS="py-attribute-table-entry"))


def depart_attributetable_node(self: HTML5Translator, node: attributetable):
def depart_attributetable_node(self: HTML5Translator, _node: attributetable) -> None:
self.body.append("</div>")


def depart_attributetablecolumn_node(self: HTML5Translator, node: attributetablecolumn):
def depart_attributetablecolumn_node(
self: HTML5Translator, _node: attributetablecolumn
) -> None:
self.body.append("</div>")


def depart_attributetabletitle_node(self: HTML5Translator, node: attributetabletitle):
def depart_attributetabletitle_node(
self: HTML5Translator, _node: attributetabletitle
) -> None:
self.body.append("</span>")


def depart_attributetablebadge_node(self: HTML5Translator, node: attributetablebadge):
def depart_attributetablebadge_node(
self: HTML5Translator, _node: attributetablebadge
) -> None:
self.body.append("</span>")


def depart_attributetable_item_node(self: HTML5Translator, node: attributetable_item):
def depart_attributetable_item_node(
self: HTML5Translator, _node: attributetable_item
) -> None:
self.body.append("</li>")


Expand All @@ -99,10 +115,11 @@ class PyAttributeTable(SphinxDirective):
final_argument_whitespace = False
option_spec = {}

def parse_name(self, content: str):
def parse_name(self, content: str) -> tuple[str, str]:
match = _name_parser_regex.match(content)
if match is None:
raise RuntimeError("could not find module name somehow")
msg = "could not find module name somehow"
raise RuntimeError(msg)

path, name = match.groups()
if path:
Expand All @@ -112,13 +129,12 @@ def parse_name(self, content: str):
if not modulename:
modulename = self.env.ref_context.get("py:module")
if modulename is None:
raise RuntimeError(
"modulename somehow None for %s in %s." % (content, self.env.docname)
)
msg = f"modulename somehow None for {content} in {self.env.docname}."
raise RuntimeError(msg)

return modulename, name

def run(self):
def run(self) -> list[attributetableplaceholder]:
"""If you're curious on the HTML this is meant to generate:
<div class="py-attribute-table">
Expand Down Expand Up @@ -155,7 +171,7 @@ def run(self):
return [node]


def build_lookup_table(env: BuildEnvironment):
def build_lookup_table(env: BuildEnvironment) -> dict[str, list[str]]:
# Given an environment, load up a lookup table of
# full-class-name: objects
result: dict[str, list[str]] = {}
Expand Down Expand Up @@ -184,10 +200,10 @@ def build_lookup_table(env: BuildEnvironment):
class TableElement(NamedTuple):
fullname: str
label: str
badge: Optional[attributetablebadge]
badge: attributetablebadge | None


def process_attributetable(app: Sphinx, doctree: document, fromdocname: str):
def process_attributetable(app: Sphinx, doctree: document, _fromdocname: str) -> None:
env = app.builder.env

lookup = build_lookup_table(env)
Expand All @@ -197,18 +213,19 @@ def process_attributetable(app: Sphinx, doctree: document, fromdocname: str):
node["python-class"],
node["python-full-name"],
)
assert isinstance(modulename, str)
assert isinstance(classname, str)
assert isinstance(fullname, str)
assert isinstance(modulename, str) # noqa: S101
assert isinstance(classname, str) # noqa: S101
assert isinstance(fullname, str) # noqa: S101

groups = get_class_results(lookup, modulename, classname, fullname)
table = attributetable("")
for label, subitems in groups.items():
if not subitems:
continue

key: Callable[[TableElement], str] = lambda c: c.label
table.append(class_results_to_node(label, sorted(subitems, key=key)))
table.append(
class_results_to_node(label, sorted(subitems, key=lambda c: c.label))
)

table["python-class"] = fullname

Expand All @@ -220,7 +237,7 @@ def process_attributetable(app: Sphinx, doctree: document, fromdocname: str):

def get_class_results(
lookup: dict[str, list[str]], modulename: str, name: str, fullname: str
):
) -> OrderedDict[str, list[TableElement]]:
module = importlib.import_module(modulename)
cls = getattr(module, name)

Expand Down Expand Up @@ -275,17 +292,19 @@ def get_class_results(
return groups


def class_results_to_node(key: str, elements: list[TableElement]):
def class_results_to_node(
key: str, elements: list[TableElement]
) -> attributetablecolumn:
title = attributetabletitle(key, key)
ul = nodes.bullet_list("")
for element in elements:
ref = nodes.reference(
"",
"",
*[nodes.Text(element.label)],
internal=True,
refuri="#" + element.fullname,
anchorname="",
*[nodes.Text(element.label)],
)
para = addnodes.compact_paragraph("", "", ref)
if element.badge is not None:
Expand All @@ -296,7 +315,7 @@ def class_results_to_node(key: str, elements: list[TableElement]):
return attributetablecolumn("", title, ul)


def setup(app: Sphinx):
def setup(app: Sphinx) -> None:
app.add_directive("attributetable", PyAttributeTable)
app.add_node(
attributetable, html=(visit_attributetable_node, depart_attributetable_node)
Expand Down
20 changes: 14 additions & 6 deletions docs/extensions/exception_hierarchy.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,43 @@
# pyright: reportUnknownArgumentType=false
from __future__ import annotations

from typing import TYPE_CHECKING

from docutils import nodes
from docutils.parsers.rst import Directive
from sphinx.application import Sphinx
from sphinx.writers.html5 import HTML5Translator

if TYPE_CHECKING:
from sphinx.application import Sphinx
from sphinx.writers.html5 import HTML5Translator


class exception_hierarchy(nodes.General, nodes.Element):
pass


def visit_exception_hierarchy_node(self: HTML5Translator, node: exception_hierarchy):
def visit_exception_hierarchy_node(
self: HTML5Translator, node: exception_hierarchy
) -> None:
self.body.append(self.starttag(node, "div", CLASS="exception-hierarchy-content"))


def depart_exception_hierarchy_node(self: HTML5Translator, node: exception_hierarchy):
def depart_exception_hierarchy_node(
self: HTML5Translator, _node: exception_hierarchy
) -> None:
self.body.append("</div>\n")


class ExceptionHierarchyDirective(Directive):
has_content = True

def run(self):
def run(self) -> list[exception_hierarchy]:
self.assert_has_content()
node = exception_hierarchy("\n".join(self.content))
self.state.nested_parse(self.content, self.content_offset, node)
return [node]


def setup(app: Sphinx):
def setup(app: Sphinx) -> None:
app.add_node(
exception_hierarchy,
html=(visit_exception_hierarchy_node, depart_exception_hierarchy_node),
Expand Down
8 changes: 4 additions & 4 deletions examples/simple.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@
import traceback
from logging import DEBUG, getLogger
from os import getenv
from typing import Any
from typing import TYPE_CHECKING, Any

from nextcord import Intents, Interaction, Member
from nextcord.abc import Connectable
from nextcord.ext import commands

from mafic import NodePool, Player, Playlist, Track, TrackEndEvent

if TYPE_CHECKING:
from nextcord.abc import Connectable

getLogger("mafic").setLevel(DEBUG)


Expand Down Expand Up @@ -51,7 +53,6 @@ def __init__(self, client: Bot, channel: Connectable) -> None:
@bot.slash_command(dm_permission=False)
async def join(inter: Interaction[Bot]):
"""Join your voice channel."""

assert isinstance(inter.user, Member)

if not inter.user.voice or not inter.user.voice.channel:
Expand All @@ -71,7 +72,6 @@ async def play(inter: Interaction[Bot], query: str):
query:
The song to search or play.
"""

assert inter.guild is not None

if not inter.guild.voice_client:
Expand Down
5 changes: 1 addition & 4 deletions mafic/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
# SPDX-License-Identifier: MIT
"""
Mafic
=====
A properly typehinted lavalink client for discord.py, nextcord, disnake and py-cord.
"""A properly typehinted lavalink client for discord.py, nextcord, disnake and py-cord.
:copyright: (c) 2022-present ooliver1
:license: MIT, see LICENSE for more details.
Expand Down
Loading

1 comment on commit e1741fa

@ooliver1
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Weebhook test.

Please sign in to comment.