Skip to content

Commit

Permalink
do more ruffing
Browse files Browse the repository at this point in the history
this is a recreation of Tool's PR here, with a few minor tweaks.
I agree with his decisions about which checks to just keep ignoring,
as each of those has some reason for being less useful than annoying
in our codebase.

#1431
  • Loading branch information
srabraham committed Jan 14, 2025
1 parent 60079db commit c533b88
Show file tree
Hide file tree
Showing 24 changed files with 98 additions and 123 deletions.
6 changes: 0 additions & 6 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,12 @@
# add these directories to sys.path here. If the directory is relative to the
# documentation root, use os.path.abspath to make it absolute, like shown here.

import sys
from os import environ
from os.path import abspath, join
from pprint import pprint
from sys import path


path.insert(0, abspath(join("..", "src")))

pprint(environ)
pprint(sys.path)


# -- Project information -----------------------------------------------------

Expand Down
30 changes: 8 additions & 22 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,14 @@ ignore = [
"RSE102", # Unnecessary parentheses on raised exception
"S101", # Use of `assert` detected
"S608", # Possible SQL injection vector through string-based query construction
"SIM108", # Use ternary operator
"SIM118", # Use `key in dict` instead of `key in dict.keys()`
"TD001", # Invalid TODO tag: `FIXME`
"TD002", # Missing author in TODO
"TD003", # Missing issue link on the line following this TODO
"TRY003", # Avoid specifying long messages outside the exception class
"TRY301", # Abstract `raise` to an inner function
"TRY400", # Use `logging.exception` instead of `logging.error`

# Reconsider
"TID252", # Prefer absolute imports over relative imports from parent modules
Expand All @@ -130,30 +138,8 @@ ignore = [
"FBT001", # Boolean-typed positional argument in function definition
"FBT002", # Boolean default positional argument in function definition
"FBT003", # Boolean positional value in function call
"SIM102", # Use a single `if` statement instead of nested `if` statements
"SIM105", # Use `contextlib.suppress(NoSectionError, NoOptionError)` instead of `try`-`except`-`pass`
"SIM108", # Use ternary operator
"SIM114", # Combine `if` branches using logical `or` operator
"SIM115", # Use a context manager for opening files
"SIM118", # Use `key in dict` instead of `key in dict.keys()`
"SLF001", # Private member accessed
"T201", # `print` found
"T203", # `pprint` found
"TC001", # Move application import into a type-checking block
"TC003", # Move standard library import into a type-checking block
"TD001", # Invalid TODO tag: `FIXME`
"TD002", # Missing author in TODO
"TD003", # Missing issue link on the line following this TODO
"TRY003", # Avoid specifying long messages outside the exception class
"TRY301", # Abstract `raise` to an inner function
"TRY400", # Use `logging.exception` instead of `logging.error`
"UP006", # Use `collections.deque` instead of `Deque` for type annotation
"UP007", # Use `X | Y` for type annotations
"UP017", # Use `datetime.UTC` alias
"UP031", # Use format specifiers instead of percent format
"UP032", # Use f-string instead of `format` call
"UP035", # `typing.Deque` is deprecated, use `collections.deque` instead
"UP038", # Use `X | Y` in `isinstance` call instead of `(X, Y)`
]

[tool.ruff.lint.isort]
Expand Down
10 changes: 5 additions & 5 deletions src/ims/application/_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
Iterable,
Mapping,
)
from datetime import UTC
from datetime import datetime as DateTime
from datetime import timezone as TimeZone
from enum import Enum
from functools import partial
from json import JSONDecodeError
Expand Down Expand Up @@ -405,7 +405,7 @@ async def newIncidentResource(

user: IMSUser = request.user # type: ignore[attr-defined]
author = user.shortNames[0]
now = DateTime.now(TimeZone.utc)
now = DateTime.now(UTC)
jsonNow = jsonObjectFromModelObject(now)

# Set JSON incident number to 0
Expand Down Expand Up @@ -662,7 +662,7 @@ def _cast(obj: Any) -> Any:

jsonEntries = edits.get(IncidentJSONKey.reportEntries.value, UNSET)
if jsonEntries is not UNSET:
now = DateTime.now(TimeZone.utc)
now = DateTime.now(UTC)

entries = (
ReportEntry(
Expand Down Expand Up @@ -813,7 +813,7 @@ async def newFieldReportResource(

user: IMSUser = request.user # type: ignore[attr-defined]
author = user.shortNames[0]
now = DateTime.now(TimeZone.utc)
now = DateTime.now(UTC)
jsonNow = jsonObjectFromModelObject(now)

# Set JSON event id
Expand Down Expand Up @@ -1018,7 +1018,7 @@ def _cast(obj: Any) -> Any:

jsonEntries = edits.get(FieldReportJSONKey.reportEntries.value, UNSET)
if jsonEntries is not UNSET:
now = DateTime.now(TimeZone.utc)
now = DateTime.now(UTC)

entries = (
ReportEntry(
Expand Down
10 changes: 5 additions & 5 deletions src/ims/application/_external.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,9 @@ class HTTPPageGetter(http.HTTPClient):
def connectionMade(self):
method = _ensureValidMethod(getattr(self.factory, "method", b"GET"))
self.sendCommand(method, _ensureValidURI(self.factory.path))
if self.factory.scheme == b"http" and self.factory.port != 80: # noqa: PLR2004
host = b"%b:%d" % (self.factory.host, self.factory.port)
elif self.factory.scheme == b"https" and self.factory.port != 443: # noqa: PLR2004
if (self.factory.scheme == b"http" and self.factory.port != 80) or ( # noqa: PLR2004
self.factory.scheme == b"https" and self.factory.port != 443 # noqa: PLR2004
):
host = b"%b:%d" % (self.factory.host, self.factory.port)
else:
host = self.factory.host
Expand Down Expand Up @@ -259,8 +259,8 @@ def timeout(self):
self.transport.abortConnection()
self.factory.noPage(
defer.TimeoutError(
"Getting %s took longer than %s seconds."
% (self.factory.url, self.factory.timeout)
f"Getting {self.factory.url} took "
f"longer than {self.factory.timeout} seconds."
)
)

Expand Down
11 changes: 5 additions & 6 deletions src/ims/application/_klein.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

from collections.abc import Callable, Iterable, Sequence
from functools import wraps
from typing import Any, Optional, TypeVar, cast
from typing import Any, TypeVar, cast

from hyperlink import URL
from klein import Klein, KleinRenderable, KleinRouteHandler
Expand Down Expand Up @@ -309,7 +309,7 @@ def queryValue(request: IRequest, name: str, default: str | None = None) -> str
C{default} if there no such query parameter.
If more than one value is found, return the last value found.
"""
values = cast(Optional[Sequence[bytes]], request.args.get(name.encode("utf-8")))
values = cast(Sequence[bytes] | None, request.args.get(name.encode("utf-8")))

if values is None:
return default
Expand All @@ -336,7 +336,7 @@ def queryValues(
@return: The values of the query parameter specified by C{name}, or
C{default} if there no such query parameter.
"""
values = cast(Optional[Sequence[bytes]], request.args.get(name))
values = cast(Sequence[bytes] | None, request.args.get(name))

if values is None:
return default
Expand Down Expand Up @@ -474,9 +474,8 @@ def notAuthenticatedError(
Not authenticated.
"""
requestedWith = request.getHeader("X-Requested-With")
if requestedWith is not None:
if requestedWith == "XMLHttpRequest":
return forbiddenResponse(request)
if requestedWith == "XMLHttpRequest":
return forbiddenResponse(request)

element = redirect(request, URLs.login, origin="o")
return renderElement( # type: ignore[return-value]
Expand Down
6 changes: 3 additions & 3 deletions src/ims/auth/test/test_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
from datetime import timedelta as TimeDelta
from pathlib import Path
from string import ascii_letters, digits
from typing import Any, AnyStr, Optional
from typing import Any, AnyStr
from unittest.mock import patch

from attrs import evolve, frozen
Expand Down Expand Up @@ -869,11 +869,11 @@ def test_authorizeReqForFieldReport(self) -> None:


class MockReq(Request):
def __init__(self, user: Optional[TestUser], headers: Mapping[str, str]) -> None:
def __init__(self, user: TestUser | None, headers: Mapping[str, str]) -> None:
super().__init__(DummyChannel(), False)
self.user = user # type: ignore[assignment]
self.headers = headers
self.authorizations = Authorization.none

def getHeader(self, key: AnyStr) -> Optional[AnyStr]:
def getHeader(self, key: AnyStr) -> AnyStr | None:
return self.headers.get(str(key)) # type: ignore[return-value]
5 changes: 2 additions & 3 deletions src/ims/config/_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
IMS configuration
"""

import contextlib
from collections.abc import Callable, Sequence
from configparser import ConfigParser, NoOptionError, NoSectionError
from datetime import timedelta as TimeDelta
Expand Down Expand Up @@ -111,10 +112,8 @@ def valueFromConfig(
value = environ.get(f"IMS_{variable}")

if not value:
try:
with contextlib.suppress(NoSectionError, NoOptionError):
value = self._configParser.get(section, option)
except (NoSectionError, NoOptionError):
pass

if value:
return value
Expand Down
2 changes: 1 addition & 1 deletion src/ims/directory/clubhouse_db/_dms.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ async def personnel(self) -> Iterable[Ranger]:
self._state._dbpool = None
self._state._dbErrorCount += 1

if isinstance(e, (SQLDatabaseError, SQLOperationalError)):
if isinstance(e, SQLDatabaseError | SQLOperationalError):
if self._state._dbErrorCount < 2: # noqa: PLR2004
self._log.info(
"Retrying loading personnel from DMS "
Expand Down
7 changes: 5 additions & 2 deletions src/ims/directory/clubhouse_db/test/dummy.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,11 @@
Mock objects for Clubhouse directory.
"""

from collections.abc import MutableSequence
from typing import Any, cast
from typing import TYPE_CHECKING, Any, cast


if TYPE_CHECKING:
from collections.abc import MutableSequence

from twisted.internet.defer import Deferred, fail, succeed

Expand Down
5 changes: 3 additions & 2 deletions src/ims/ext/klein.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,9 @@ class HeaderName(Enum):
server = "Server"


if False:
_staticETag = version # type: ignore[unreachable]
staticETagForTest = False
if staticETagForTest:
_staticETag = version
_maxAge = 60 * 5 # 5 minutes
else:
# For debugging, change the ETag on app launch
Expand Down
6 changes: 3 additions & 3 deletions src/ims/ext/sqlite.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from sqlite3 import Row as BaseRow
from sqlite3 import connect as sqliteConnect
from types import TracebackType
from typing import Any, ClassVar, Optional, TextIO, Union, cast
from typing import Any, ClassVar, TextIO, cast

from attrs import frozen
from twisted.logger import Logger
Expand All @@ -35,7 +35,7 @@

CursorFactory = Callable[..., "Cursor"]

ParameterValue = Optional[Union[bytes, str, int, float]]
ParameterValue = bytes | str | int | float
Parameters = Mapping[str, ParameterValue]

SQLITE_MIN_INT = -(2**63) # 64 bits
Expand Down Expand Up @@ -112,7 +112,7 @@ def executeAndPrint(self, sql: str, parameters: Parameters | None = None) -> Non
"""

def emit(row: Iterable[object]) -> None:
print(" | ".join(str(i) for i in row))
print(" | ".join(str(i) for i in row)) # noqa: T201

printHeader = True

Expand Down
9 changes: 3 additions & 6 deletions src/ims/model/_address.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,9 @@ def _cmp(self, other: Any, methodName: str) -> bool:
if other is None:
return self._allNone()

if other.__class__ is TextOnlyAddress:
if self._allNone():
method = cast(
Callable[[str], bool], getattr(self.description, methodName)
)
return method(other.description)
if other.__class__ is TextOnlyAddress and self._allNone():
method = cast(Callable[[str], bool], getattr(self.description, methodName))
return method(other.description)

return ComparisonMixIn._cmp(self, other, methodName)

Expand Down
10 changes: 5 additions & 5 deletions src/ims/model/json/_address.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"""

from enum import Enum, unique
from typing import Any, Optional, cast
from typing import Any, cast

from .._address import Address, RodGarettAddress, TextOnlyAddress
from ._json import (
Expand Down Expand Up @@ -63,7 +63,7 @@ class AddressJSONType(Enum):
"""

addressType = str
description = cast(type[Any], Optional[str])
description = cast(type[Any], str | None)


@unique
Expand Down Expand Up @@ -100,9 +100,9 @@ class RodGarettAddressJSONType(Enum):
Rod Garett address JSON keys
"""

concentric = Optional[str]
radialHour = Optional[int]
radialMinute = Optional[int]
concentric = str | None
radialHour = int | None
radialMinute = int | None
description = AddressJSONType.description.value


Expand Down
4 changes: 2 additions & 2 deletions src/ims/model/json/_incident.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

from datetime import datetime as DateTime
from enum import Enum, unique
from typing import Any, Optional, cast
from typing import Any, cast

from .._entry import ReportEntry
from .._incident import Incident
Expand Down Expand Up @@ -67,7 +67,7 @@ class IncidentJSONType(Enum):
created = DateTime
state = IncidentState
priority = IncidentPriority
summary = Optional[str]
summary = str | None
location = Location
rangerHandles = set[str]
incidentTypes = set[str]
Expand Down
4 changes: 2 additions & 2 deletions src/ims/model/json/_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
from collections.abc import Callable, Iterable, Mapping
from datetime import datetime as DateTime
from enum import Enum
from typing import Any, Union, cast
from typing import Any, cast

from cattr import Converter
from twisted.logger import Logger
Expand All @@ -36,7 +36,7 @@
log = Logger()


JSON = Union[Mapping[str, Any], Iterable[Any], int, str, float, bool, None]
JSON = Mapping[str, Any] | Iterable[Any] | int | str | float | bool | None


class JSONCodecError(Exception):
Expand Down
4 changes: 2 additions & 2 deletions src/ims/model/json/_ranger.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"""

from enum import Enum, unique
from typing import Any, Optional, cast
from typing import Any, cast

from .._ranger import Ranger, RangerStatus
from ._json import (
Expand Down Expand Up @@ -58,7 +58,7 @@ class RangerJSONType(Enum):
status = RangerStatus
# email is intentionally not serialized, since no web client needs it
enabled = bool
directoryID = Optional[str]
directoryID = str | None
# password is intentionally not serialized, since no web client needs it


Expand Down
6 changes: 3 additions & 3 deletions src/ims/model/json/_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

from datetime import datetime as DateTime
from enum import Enum, unique
from typing import Any, Optional, cast
from typing import Any, cast

from .._entry import ReportEntry
from .._report import FieldReport
Expand Down Expand Up @@ -57,8 +57,8 @@ class FieldReportJSONType(Enum):
eventID = str
number = int
created = DateTime
summary = Optional[str]
incidentNumber = Optional[int]
summary = str | None
incidentNumber = str | None
reportEntries = list[ReportEntry]


Expand Down
Loading

0 comments on commit c533b88

Please sign in to comment.