Skip to content

Commit

Permalink
More linting fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
FasterSpeeding committed Dec 1, 2024
1 parent d96bdfb commit 307cc51
Show file tree
Hide file tree
Showing 33 changed files with 414 additions and 269 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]
### Changed
- Renamed the `case_sensntive` argument to `case_sensitive` in `MessageCommand.find_command`.

### Deprecated
- The old `case_sensntive` argument alias in `MessageCommand.find_command`.

## [2.17.7] - 2024-11-24
### Fixed
- Moved away from using `typing.runtime_checkable` as this is unreliable in
Expand Down
2 changes: 1 addition & 1 deletion docs_src/usage.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ async def message_command(ctx: tanjun.abc.MessageContext) -> None: ...


def message_command_group_example() -> None:
# prefixes=["!"]
# . prefixes=["!"]

@tanjun.as_message_command_group("groupy")
async def groupy_group(ctx: tanjun.abc.MessageContext) -> None: ...
Expand Down
46 changes: 36 additions & 10 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -112,40 +112,61 @@ select = ["ALL"]
ignore = [
"ANN401", # Dynamically typed expressions (typing.Any) are disallowed in ``
"COM812", # Trailing comma missing (incompatible with black)
"D102", # Missing docstring in public method
"D105", # Missing docstring in magic method
"FIX002", # Line contains TODO, consider resolving the issue
"I001", # [*] Import block is un-sorted or un-formatted
"N818", # Exception name `FailedModuleUnload` should be named with an Error suffix
"PYI041", # Use `float` instead of `int | float`
"S101", # Use of `assert` detected
"SIM105", # Use `contextlib.suppress(Error)` instead of `try`-`except`-`pass`
"SIM108", # Use ternary operator `` instead of `if`-`else`-block
"TD002", # Missing author in TODO; try: `# TODO(<author_name>): ...` or `# TODO @<author_name>: ...`
"TD003", # Missing issue link on the line following this TODO
]

[tool.ruff.lint.per-file-ignores]
"docs_src/**/*.py" = [
"ARG001", # Unused function argument: ``
"B008", # Do not perform function call `` in argument defaults; instead, perform the call within the function, or read the default from a module-level singleton variable
"D100", # Missing docstring in public module
"D101", # Missing docstring in public class
"D102", # Missing docstring in public method
"D103", # Missing docstring in public function
"FBT002", # Boolean default positional argument in function definition
"F841", # Local variable `` is assigned to but never used
"INP001", # File `` is part of an implicit namespace package. Add an `__init__.py`.
"N806", # Variable `` in function should be lowercase
"PIE790", # [*] Unnecessary `...` literal
"PYI013", # [*] Non-empty class body must not contain `...`
"T201", # `print` found
]
"noxfile.py" = [
"F403", # `from noxfile import *` used; unable to detect undefined name
]
"tests/**/*.py" = [
"examples/**/*.py" = [
"B008", # Do not perform function call `` in argument defaults; instead, perform the call within the function, or read the default from a module-level singleton variable
"D100", # Missing docstring in public module
"D101", # Missing docstring in public class
"D102", # Missing docstring in public method
"D103", # Missing docstring in public function
"D104", # Missing docstring in public package
"FBT001", # Boolean-typed positional argument in function definition
"PGH003", # Use specific rule codes when ignoring type issues
"PLR2004", # Magic value used in comparison, consider replacing `` with a constant variable
"SLF001", # Private member accessed: ``
"PIE790", # [*] Unnecessary `...` literal
"PYI013", # [*] Non-empty class body must not contain `...`
]
"noxfile.py" = [
"F403", # `from noxfile import *` used; unable to detect undefined name
]
"tests/**/*.py" = [
"D100", # Missing docstring in public module
"D101", # Missing docstring in public class
"D102", # Missing docstring in public method
"D103", # Missing docstring in public function
"D104", # Missing docstring in public package
"FBT001", # Boolean-typed positional argument in function definition
"FBT002", # Boolean default positional argument in function definition
"FBT003", # Boolean positional value in function call
"PLR0915", # Too many statements
"PGH003", # Use specific rule codes when ignoring type issues
"PLR2004", # Magic value used in comparison, consider replacing `` with a constant variable
"SLF001", # Private member accessed: ``
]
"tests/test_annotations_future_annotations.py" = [
"B008", # Do not perform function call `` in argument defaults; instead, perform the call within the function, or read the default from a module-level singleton variable
Expand All @@ -156,6 +177,9 @@ ignore = [
"B008", # Do not perform function call `` in argument defaults; instead, perform the call within the function, or read the default from a module-level singleton variable
"UP007", # [*] Use `X | Y` for type annotations
]
"tests/test_checks.py" = [
"ARG002", # Unused method argument: ``
]
"tests/test_clients_future_annotations.py" = [
"B008", # Do not perform function call `` in argument defaults; instead, perform the call within the function, or read the default from a module-level singleton variable
"PLW0603", # Using the global statement to update `` is discouraged
Expand Down Expand Up @@ -192,8 +216,10 @@ max-line-length = 130
convention = "numpy"

[tool.ruff.lint.pylint]
max-args = 12
max-branches = 15
max-args = 20
max-branches = 20
max-returns = 15
max-statements = 100

[tool.isort]
profile = "black"
Expand Down
15 changes: 8 additions & 7 deletions tanjun/_internal/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ class _WrappedProto(typing.Protocol):

def _has_wrapped(value: typing.Any, /) -> typing.TypeGuard[_WrappedProto]:
try:
value.wrapped_command
value.wrapped_command # noqa: B018

except AttributeError:
return False
Expand Down Expand Up @@ -467,7 +467,7 @@ def cmp_command(

opts = cmd.options or ()
other_opts = other.options or ()
return len(opts) == len(other_opts) and all(itertools.starmap(operator.eq, zip(opts, other_opts)))
return len(opts) == len(other_opts) and all(itertools.starmap(operator.eq, zip(opts, other_opts, strict=True)))

return True

Expand All @@ -493,9 +493,8 @@ class MessageCommandIndex:

def __init__(
self,
strict: bool,
/,
*,
strict: bool,
commands: list[tanjun.MessageCommand[typing.Any]] | None = None,
names_to_commands: dict[str, tuple[str, tanjun.MessageCommand[typing.Any]]] | None = None,
search_tree: _TreeT | None = None,
Expand Down Expand Up @@ -553,7 +552,9 @@ def add(self, command: tanjun.MessageCommand[typing.Any], /) -> bool:

# Case insensitive keys are used here as a subsequent check against the original
# name can be used for case-sensitive lookup.
self.names_to_commands.update((key, (name, command)) for key, name in zip(insensitive_names, names))
self.names_to_commands.update(
(key, (name, command)) for key, name in zip(insensitive_names, names, strict=True)
)

else: # strict indexes avoid using the search tree all together.
# This needs to be explicitly typed for MyPy.
Expand Down Expand Up @@ -602,7 +603,7 @@ def copy(self, *, parent: tanjun.MessageCommandGroup[typing.Any] | None = None)
commands = {command: command.copy(parent=parent) for command in self.commands}
memo = {id(command): new_command for command, new_command in commands.items()}
return MessageCommandIndex(
self.is_strict,
strict=self.is_strict,
commands=list(commands.values()),
names_to_commands={
key: (name, commands[command]) for key, (name, command) in self.names_to_commands.items()
Expand All @@ -611,7 +612,7 @@ def copy(self, *, parent: tanjun.MessageCommandGroup[typing.Any] | None = None)
)

def find(
self, content: str, case_sensitive: bool, /
self, content: str, /, *, case_sensitive: bool
) -> collections.Iterator[tuple[str, tanjun.MessageCommand[typing.Any]]]:
"""Find commands in the index.
Expand Down
10 changes: 6 additions & 4 deletions tanjun/_internal/localisation.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,8 @@ def assert_length(self, min_length: int, max_length: int, /) -> Self:
_UnnamedFields = typing.Literal["description", "name"]
_FieldType = NamedFields | _UnnamedFields

_FIELD_NAME_EXCLUDE = {"name", "description"}


@typing.overload
def to_localise_id(
Expand Down Expand Up @@ -254,13 +256,13 @@ def to_localise_id(
The generated localied field ID.
"""
if field_name:
if field_type == "name" or field_type == "description":
if field_type in _FIELD_NAME_EXCLUDE:
error_message = f"Field_name must not be provided for {field_type} fields"
raise RuntimeError(error_message)

return f"{command_type}:{command_name}:{field_type}:{field_name}"

if field_type != "name" and field_type != "description":
if field_type not in _FIELD_NAME_EXCLUDE:
error_message = f"Field_name must be provided for {field_type} fields"
raise RuntimeError(error_message)

Expand Down Expand Up @@ -317,5 +319,5 @@ def _localise_slash_option(
choice.name_localizations.update(name_variants)

if option.options:
for option in option.options:
_localise_slash_option(option, name, localiser)
for opt in option.options:
_localise_slash_option(opt, name, localiser)
6 changes: 3 additions & 3 deletions tanjun/abc.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@

class _DefaultFlag(enum.Enum):
NO_DEFAULT = object()
NO_PASS = object()
NO_PASS = object() # noqa: PIE796


NO_DEFAULT: NoDefault = _DefaultFlag.NO_DEFAULT
Expand Down Expand Up @@ -1356,7 +1356,7 @@ def type(self) -> hikari.CommandType:
"""Type of application command this context is for."""

@abc.abstractmethod
def set_ephemeral_default(self, state: bool, /) -> Self:
def set_ephemeral_default(self, state: bool, /) -> Self: # noqa: FBT001
"""Set the ephemeral default state for this context.
Parameters
Expand Down Expand Up @@ -4493,7 +4493,7 @@ def iter_menu_commands(
def iter_menu_commands(
self, *, global_only: bool = False, type: hikari.CommandType | None = None # noqa: A002
) -> collections.Iterator[MenuCommand[typing.Any, typing.Any]]:
"""Iterator over the menu commands registered to this client.
"""Iterate over the menu commands registered to this client.
Parameters
----------
Expand Down
11 changes: 8 additions & 3 deletions tanjun/annotations.py
Original file line number Diff line number Diff line change
Expand Up @@ -1730,7 +1730,9 @@ def __init__(
"""
if default is not tanjun.NO_DEFAULT:
warnings.warn(
"Flag.__init__'s `default` argument is deprecated, use Default instead", category=DeprecationWarning
"Flag.__init__'s `default` argument is deprecated, use Default instead",
stacklevel=2,
category=DeprecationWarning,
)

self._aliases = aliases
Expand Down Expand Up @@ -2519,8 +2521,11 @@ def add_to_slash_cmds(self, commands: collections.Sequence[slash.SlashCommand[ty

return self

SLASH_OPTION_ADDER: dict[
typing.Any, collections.Callable[[Self, slash.SlashCommand[typing.Any], str], slash.SlashCommand[typing.Any]]
SLASH_OPTION_ADDER: typing.ClassVar[
dict[
typing.Any,
collections.Callable[[Self, slash.SlashCommand[typing.Any], str], slash.SlashCommand[typing.Any]],
]
] = {
hikari.Attachment: lambda self, c, d: c.add_attachment_option(
self.slash_name, d, default=self.default, key=self.key
Expand Down
Loading

0 comments on commit 307cc51

Please sign in to comment.