Skip to content

Commit

Permalink
Clean up linting and typing
Browse files Browse the repository at this point in the history
  • Loading branch information
linuxdaemon committed Apr 2, 2024
1 parent 44a5e73 commit 17cdff2
Show file tree
Hide file tree
Showing 18 changed files with 465 additions and 446 deletions.
3 changes: 2 additions & 1 deletion .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@
"ms-python.vscode-pylance",
"EditorConfig.EditorConfig",
"GitHub.vscode-pull-request-github",
"github.vscode-github-actions"
"github.vscode-github-actions",
"redhat.vscode-yaml"
],
"settings": {
"python.pythonPath": "~/.virtualenvs/py-irclib/bin/python",
Expand Down
119 changes: 68 additions & 51 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,55 +1,72 @@
ci:
skip: [pylint, mypy]
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: c4a0b883114b00d8d76b479c820ce7950211c99b # frozen: v4.5.0
hooks:
- id: trailing-whitespace
args: ['--markdown-linebreak-ext=md,markdown']
- id: end-of-file-fixer
- id: check-yaml
- id: check-added-large-files
- id: check-ast
- id: check-byte-order-marker
- id: check-merge-conflict
- id: debug-statements
- id: detect-private-key
- id: check-builtin-literals
- id: check-case-conflict
- id: check-docstring-first
- id: check-executables-have-shebangs
- id: check-json
exclude: '^\.(vscode|devcontainer)/'
- id: pretty-format-json
args:
- --indent
- '4'
- --autofix
- --no-sort-keys
exclude: '^\.(vscode|devcontainer)/'
- id: check-toml
- id: fix-encoding-pragma
args:
- --remove
- repo: https://github.com/psf/black
rev: 552baf822992936134cbd31a38f69c8cfe7c0f05 # frozen: 24.3.0
hooks:
- id: black
- repo: https://github.com/pycqa/isort
rev: c235f5e450b4b84e58d114ed4c589cbf454175a3 # frozen: 5.13.2
hooks:
- id: isort
- repo: https://github.com/pre-commit/pygrep-hooks
rev: 3a6eb0fadf60b3cccfd80bad9dbb6fae7e47b316 # frozen: v1.10.0
hooks:
- id: python-no-eval
- id: python-no-log-warn
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: c4a0b883114b00d8d76b479c820ce7950211c99b # frozen: v4.5.0
hooks:
- id: trailing-whitespace
args: ["--markdown-linebreak-ext=md,markdown"]
- id: end-of-file-fixer
- id: check-yaml
- id: check-added-large-files
- id: check-ast
- id: check-byte-order-marker
- id: check-merge-conflict
- id: debug-statements
- id: detect-private-key
- id: check-builtin-literals
- id: check-case-conflict
- id: check-docstring-first
- id: check-executables-have-shebangs
- id: check-json
exclude: '^\.(vscode|devcontainer)/'
- id: pretty-format-json
args:
- --indent
- "4"
- --autofix
- --no-sort-keys
exclude: '^\.(vscode|devcontainer)/'
- id: check-toml
- id: fix-encoding-pragma
args:
- --remove
- repo: https://github.com/psf/black
rev: 552baf822992936134cbd31a38f69c8cfe7c0f05 # frozen: 24.3.0
hooks:
- id: black
- repo: https://github.com/pycqa/isort
rev: c235f5e450b4b84e58d114ed4c589cbf454175a3 # frozen: 5.13.2
hooks:
- id: isort
- repo: https://github.com/pre-commit/pygrep-hooks
rev: 3a6eb0fadf60b3cccfd80bad9dbb6fae7e47b316 # frozen: v1.10.0
hooks:
- id: python-no-eval
- id: python-no-log-warn

- repo: local
hooks:
- id: mypy
name: mypy
entry: hatch run python3 -m mypy
language: system
types: [python]
exclude: tests/.*
- repo: https://github.com/ikamensh/flynt/
rev: "651c822fdcf45fffcf9743dc755085f32acb65e3" # frozen: 1.0.1
hooks:
- id: flynt

- repo: https://github.com/asottile/pyupgrade
rev: 12af25eb252deaaecb6b259df40d01f42e716dc3 # frozen: v3.15.2
hooks:
- id: pyupgrade
args:
- "--py38-plus"

- repo: https://github.com/MarcoGorelli/auto-walrus
rev: e95a3f5b9cd6a6808b803aef0c68a24c35b5891c # frozen: v0.2.2
hooks:
- id: auto-walrus

- repo: local
hooks:
- id: mypy
name: mypy
entry: hatch run python3 -m mypy
language: system
types: [python]
exclude: tests/.*
7 changes: 1 addition & 6 deletions irclib/__init__.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
"""IRC parser and utility library"""

__all__ = (
"__version__",
"errors",
"parser",
"util",
)
__all__ = ("__version__", "errors", "parser", "util")

__version__ = "0.3.0"
56 changes: 27 additions & 29 deletions irclib/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,20 @@
import re
from abc import ABCMeta, abstractmethod
from typing import (
Any,
Dict,
Iterable,
Iterator,
List,
Optional,
Sequence,
Tuple,
Type,
TypeVar,
Union,
cast,
)

from typing_extensions import Self

from irclib.errors import ParseError

__all__ = (
Expand Down Expand Up @@ -78,7 +78,7 @@ def __str__(self) -> str:

@classmethod
@abstractmethod
def parse(cls: Type[SelfT], text: str) -> SelfT:
def parse(cls, text: str) -> Self:
"""Parse the object from a string"""
raise NotImplementedError

Expand All @@ -104,7 +104,7 @@ def as_tuple(self) -> Tuple[str, Optional[str]]:
"""Get data as a tuple of values"""
return self.name, self.value

def __eq__(self, other: Any) -> bool:
def __eq__(self, other: object) -> bool:
if isinstance(other, str):
return self == self.parse(other)

Expand All @@ -113,7 +113,7 @@ def __eq__(self, other: Any) -> bool:

return NotImplemented

def __ne__(self, other: Any) -> bool:
def __ne__(self, other: object) -> bool:
if isinstance(other, str):
return self != self.parse(other)

Expand All @@ -138,7 +138,7 @@ def parse(cls, text: str) -> "Cap":
class CapList(Parseable, List[Cap]):
"""Represents a list of CAP entities"""

def __eq__(self, other: Any) -> bool:
def __eq__(self, other: object) -> bool:
if isinstance(other, str):
return self == self.parse(other)

Expand All @@ -147,7 +147,7 @@ def __eq__(self, other: Any) -> bool:

return NotImplemented

def __ne__(self, other: Any) -> bool:
def __ne__(self, other: object) -> bool:
if isinstance(other, str):
return self != self.parse(other)

Expand All @@ -170,10 +170,9 @@ def parse(cls, text: str) -> "CapList":
stripped = text.strip()

caps: Iterable[Cap]
if not text:
caps = []
else:
caps = (Cap.parse(s) for s in stripped.split(CAP_SEP))
caps = (
[] if not text else (Cap.parse(s) for s in stripped.split(CAP_SEP))
)

return CapList(caps)

Expand Down Expand Up @@ -212,7 +211,7 @@ def unescape(value: str) -> str:
escaped = False
for char in value:
if escaped:
new_value += TAG_VALUE_ESCAPES.get("\\{}".format(char), char)
new_value += TAG_VALUE_ESCAPES.get(f"\\{char}", char)
escaped = False
elif char == "\\":
escaped = True
Expand All @@ -236,13 +235,11 @@ def __repr__(self) -> str:

def __str__(self) -> str:
if self.value or self._has_value:
return "{}{}{}".format(
self.name, TAG_VALUE_SEP, self.escape(self.value)
)
return f"{self.name}{TAG_VALUE_SEP}{self.escape(self.value)}"

return self.name

def __eq__(self, other: Any) -> bool:
def __eq__(self, other: object) -> bool:
if isinstance(other, str):
return self == self.parse(other)

Expand All @@ -251,7 +248,7 @@ def __eq__(self, other: Any) -> bool:

return NotImplemented

def __ne__(self, other: Any) -> bool:
def __ne__(self, other: object) -> bool:
if isinstance(other, str):
return self != self.parse(other)

Expand Down Expand Up @@ -285,7 +282,7 @@ def __str__(self) -> str:
return TAGS_SEP.join(map(str, self.values()))

@staticmethod
def _cmp_type_map(obj: Any) -> Dict[str, MessageTag]:
def _cmp_type_map(obj: object) -> Dict[str, MessageTag]:
if isinstance(obj, str):
return TagList.parse(obj)

Expand All @@ -303,14 +300,14 @@ def _cmp_type_map(obj: Any) -> Dict[str, MessageTag]:

return NotImplemented

def __eq__(self, other: Any) -> bool:
def __eq__(self, other: object) -> bool:
obj = self._cmp_type_map(other)
if obj is NotImplemented:
return NotImplemented

return dict(self) == dict(obj)

def __ne__(self, other: Any) -> bool:
def __ne__(self, other: object) -> bool:
obj = self._cmp_type_map(other)
if obj is NotImplemented:
return NotImplemented
Expand Down Expand Up @@ -397,7 +394,7 @@ def __str__(self) -> str:
def __bool__(self) -> bool:
return any(self)

def __eq__(self, other: Any) -> bool:
def __eq__(self, other: object) -> bool:
if isinstance(other, str):
return self == self.parse(other)

Expand All @@ -406,7 +403,7 @@ def __eq__(self, other: Any) -> bool:

return NotImplemented

def __ne__(self, other: Any) -> bool:
def __ne__(self, other: object) -> bool:
if isinstance(other, str):
return self != self.parse(other)

Expand All @@ -429,7 +426,8 @@ def parse(cls, text: str) -> "Prefix":
match = PREFIX_RE.match(text)
if not match: # pragma: no cover
# This should never trip, we are pretty lenient with prefixes
raise ParseError("Invalid IRC prefix format")
msg = "Invalid IRC prefix format"
raise ParseError(msg)

nick, user, host = match.groups()
return Prefix(nick, user, host)
Expand Down Expand Up @@ -464,7 +462,7 @@ def __str__(self) -> str:

return PARAM_SEP.join(self)

def __eq__(self, other: Any) -> bool:
def __eq__(self, other: object) -> bool:
if isinstance(other, str):
return self == self.parse(other)

Expand All @@ -473,7 +471,7 @@ def __eq__(self, other: Any) -> bool:

return NotImplemented

def __ne__(self, other: Any) -> bool:
def __ne__(self, other: object) -> bool:
if isinstance(other, str):
return self != self.parse(other)

Expand Down Expand Up @@ -522,7 +520,7 @@ def parse(cls, text: str) -> "ParamList":


def _parse_tags(
tags: Union[TagList, Dict[str, str], str, None, List[str]]
tags: Union[TagList, Dict[str, str], str, None, List[str]],
) -> MsgTagList:
if isinstance(tags, TagList):
return tags
Expand Down Expand Up @@ -553,7 +551,7 @@ def _parse_prefix(prefix: Union[Prefix, str, None, Iterable[str]]) -> MsgPrefix:


def _parse_params(
parameters: Tuple[Union[str, List[str], ParamList], ...]
parameters: Tuple[Union[str, List[str], ParamList], ...],
) -> ParamList:
if len(parameters) == 1 and not isinstance(parameters[0], str):
# This seems to be a list
Expand Down Expand Up @@ -621,7 +619,7 @@ def __str__(self) -> str:
def __bool__(self) -> bool:
return any(self.as_tuple())

def __eq__(self, other: Any) -> bool:
def __eq__(self, other: object) -> bool:
if isinstance(other, (str, bytes)):
return self == Message.parse(other)

Expand All @@ -630,7 +628,7 @@ def __eq__(self, other: Any) -> bool:

return NotImplemented

def __ne__(self, other: Any) -> bool:
def __ne__(self, other: object) -> bool:
if isinstance(other, (str, bytes)):
return self != Message.parse(other)

Expand Down
8 changes: 1 addition & 7 deletions irclib/util/__init__.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
"""IRC utils"""

__all__ = (
"commands",
"compare",
"frozendict",
"numerics",
"string",
)
__all__ = ("commands", "compare", "frozendict", "numerics", "string")
2 changes: 1 addition & 1 deletion irclib/util/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class Command:
class LookupDict(Mapping[str, Command]):
"""Command lookup dictionary"""

def __init__(self, *commands: Command):
def __init__(self, *commands: Command) -> None:
for command in commands:
setattr(self, command.name.lower(), command)
setattr(self, command.name.upper(), command)
Expand Down
Loading

0 comments on commit 17cdff2

Please sign in to comment.