Skip to content

Commit

Permalink
More ruff rules!
Browse files Browse the repository at this point in the history
  • Loading branch information
ItsDrike committed May 14, 2024
1 parent e05fd2d commit 3eeeb8e
Show file tree
Hide file tree
Showing 19 changed files with 107 additions and 75 deletions.
4 changes: 2 additions & 2 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from __future__ import annotations

import sys
from datetime import date
import datetime
from pathlib import Path

from packaging.version import parse as parse_version
Expand All @@ -28,7 +28,7 @@
pkg_meta: dict[str, str] = toml_parse(f)["tool"]["poetry"]

project = str(pkg_meta["name"])
copyright = f"{date.today().year}, ItsDrike" # noqa: A001
copyright = f"{datetime.datetime.now(tz=datetime.timezone.utc).date().year}, ItsDrike" # noqa: A001
author = "ItsDrike"

parsed_version = parse_version(pkg_meta["version"])
Expand Down
4 changes: 2 additions & 2 deletions mcproto/auth/account.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@

__all__ = [
"Account",
"MismatchedAccountInfoError",
"InvalidAccountAccessTokenError",
"MismatchedAccountInfoError",
]


Expand Down Expand Up @@ -43,7 +43,7 @@ def __init__(self, access_token: str, status_error: httpx.HTTPStatusError) -> No
class Account:
"""Base class for an authenticated Minecraft account."""

__slots__ = ("username", "uuid", "access_token")
__slots__ = ("access_token", "username", "uuid")

def __init__(self, username: str, uuid: McUUID, access_token: str) -> None:
self.username = username
Expand Down
8 changes: 4 additions & 4 deletions mcproto/auth/microsoft/oauth.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@
from typing_extensions import override

__all__ = [
"MicrosoftOauthResponseErrorType",
"MicrosoftOauthResponseError",
"MicrosoftOauthRequestData",
"MicrosoftOauthResponseData",
"microsoft_oauth_request",
"microsoft_oauth_authenticate",
"MicrosoftOauthResponseError",
"MicrosoftOauthResponseErrorType",
"full_microsoft_oauth",
"microsoft_oauth_authenticate",
"microsoft_oauth_request",
]

MICROSOFT_OAUTH_URL = "https://login.microsoftonline.com/consumers/oauth2/v2.0"
Expand Down
2 changes: 1 addition & 1 deletion mcproto/auth/msa.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
from mcproto.types.uuid import UUID as McUUID # noqa: N811

__all__ = [
"MSAAccount",
"ServicesAPIError",
"ServicesAPIErrorType",
"MSAAccount",
]

MC_SERVICES_API_URL = "https://api.minecraftservices.com"
Expand Down
2 changes: 1 addition & 1 deletion mcproto/auth/yggdrasil.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
from mcproto.types.uuid import UUID as McUUID # noqa: N811

__all__ = [
"AuthServerApiErrorType",
"AuthServerApiError",
"AuthServerApiErrorType",
"YggdrasilAccount",
]

Expand Down
8 changes: 4 additions & 4 deletions mcproto/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
class SyncConnection(BaseSyncReader, BaseSyncWriter, ABC):
"""Base class for all classes handling synchronous connections."""

__slots__ = ("closed", "encryption_enabled", "shared_secret", "cipher", "encryptor", "decryptor")
__slots__ = ("cipher", "closed", "decryptor", "encryption_enabled", "encryptor", "shared_secret")

def __init__(self):
self.closed = False
Expand Down Expand Up @@ -147,7 +147,7 @@ def __exit__(self, *a, **kw) -> None:
class AsyncConnection(BaseAsyncReader, BaseAsyncWriter, ABC):
"""Base class for all classes handling asynchronous connections."""

__slots__ = ("closed", "encryption_enabled", "shared_secret", "cipher", "encryptor", "decryptor")
__slots__ = ("cipher", "closed", "decryptor", "encryption_enabled", "encryptor", "shared_secret")

def __init__(self):
self.closed = False
Expand Down Expand Up @@ -312,7 +312,7 @@ def _close(self) -> None:
class TCPAsyncConnection(AsyncConnection, Generic[T_STREAMREADER, T_STREAMWRITER]):
"""Asynchronous TCP connection using :class:`~asyncio.StreamWriter` and :class:`~asyncio.StreamReader`."""

__slots__ = ("reader", "writer", "timeout")
__slots__ = ("reader", "timeout", "writer")

def __init__(self, reader: T_STREAMREADER, writer: T_STREAMWRITER, timeout: float):
super().__init__()
Expand Down Expand Up @@ -363,7 +363,7 @@ def socket(self) -> socket.socket:
class UDPSyncConnection(SyncConnection, Generic[T_SOCK]):
"""Synchronous connection using a UDP :class:`~socket.socket`."""

__slots__ = ("socket", "address")
__slots__ = ("address", "socket")

BUFFER_SIZE = 65535

Expand Down
4 changes: 2 additions & 2 deletions mcproto/multiplayer.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
__all__ = [
"JoinAcknowledgeData",
"JoinAcknowledgeProperty",
"UserJoinRequestFailedError",
"UserJoinRequestErrorKind",
"UserJoinCheckFailedError",
"UserJoinRequestErrorKind",
"UserJoinRequestFailedError",
"compute_server_hash",
"join_check",
"join_request",
Expand Down
2 changes: 1 addition & 1 deletion mcproto/packets/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"ServerBoundPacket",
"async_read_packet",
"async_write_packet",
"generate_packet_map",
"sync_read_packet",
"sync_write_packet",
"generate_packet_map",
]
2 changes: 1 addition & 1 deletion mcproto/packets/handshaking/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
from mcproto.packets.handshaking.handshake import Handshake, NextState

__all__ = [
"NextState",
"Handshake",
"NextState",
]
4 changes: 2 additions & 2 deletions mcproto/packets/handshaking/handshake.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
from mcproto.protocol.base_io import StructFormat

__all__ = [
"NextState",
"Handshake",
"NextState",
]


Expand All @@ -29,7 +29,7 @@ class Handshake(ServerBoundPacket):
PACKET_ID: ClassVar[int] = 0x00
GAME_STATE: ClassVar[GameState] = GameState.HANDSHAKING

__slots__ = ("protocol_version", "server_address", "server_port", "next_state")
__slots__ = ("next_state", "protocol_version", "server_address", "server_port")

def __init__(
self,
Expand Down
6 changes: 3 additions & 3 deletions mcproto/packets/login/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@
)

__all__ = [
"LoginStart",
"LoginDisconnect",
"LoginEncryptionRequest",
"LoginEncryptionResponse",
"LoginSuccess",
"LoginDisconnect",
"LoginPluginRequest",
"LoginPluginResponse",
"LoginSetCompression",
"LoginStart",
"LoginSuccess",
]
14 changes: 7 additions & 7 deletions mcproto/packets/login/login.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@
from mcproto.types.uuid import UUID

__all__ = [
"LoginStart",
"LoginDisconnect",
"LoginEncryptionRequest",
"LoginEncryptionResponse",
"LoginSuccess",
"LoginDisconnect",
"LoginPluginRequest",
"LoginPluginResponse",
"LoginSetCompression",
"LoginStart",
"LoginSuccess",
]


Expand Down Expand Up @@ -61,7 +61,7 @@ def _deserialize(cls, buf: Buffer, /) -> Self:
class LoginEncryptionRequest(ClientBoundPacket):
"""Used by the server to ask the client to encrypt the login process. (Server -> Client)."""

__slots__ = ("server_id", "public_key", "verify_token")
__slots__ = ("public_key", "server_id", "verify_token")

PACKET_ID: ClassVar[int] = 0x01
GAME_STATE: ClassVar[GameState] = GameState.LOGIN
Expand Down Expand Up @@ -141,7 +141,7 @@ def _deserialize(cls, buf: Buffer, /) -> Self:
class LoginSuccess(ClientBoundPacket):
"""Sent by the server to denote a successful login. (Server -> Client)."""

__slots__ = ("uuid", "username")
__slots__ = ("username", "uuid")

PACKET_ID: ClassVar[int] = 0x02
GAME_STATE: ClassVar[GameState] = GameState.LOGIN
Expand Down Expand Up @@ -201,7 +201,7 @@ def _deserialize(cls, buf: Buffer, /) -> Self:
class LoginPluginRequest(ClientBoundPacket):
"""Sent by the server to implement a custom handshaking flow. (Server -> Client)."""

__slots__ = ("message_id", "channel", "data")
__slots__ = ("channel", "data", "message_id")

PACKET_ID: ClassVar[int] = 0x04
GAME_STATE: ClassVar[GameState] = GameState.LOGIN
Expand Down Expand Up @@ -238,7 +238,7 @@ def _deserialize(cls, buf: Buffer, /) -> Self:
class LoginPluginResponse(ServerBoundPacket):
"""Response to LoginPluginRequest from client. (Client -> Server)."""

__slots__ = ("message_id", "data")
__slots__ = ("data", "message_id")

PACKET_ID: ClassVar[int] = 0x02
GAME_STATE: ClassVar[GameState] = GameState.LOGIN
Expand Down
6 changes: 3 additions & 3 deletions mcproto/packets/packet.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@
from mcproto.utils.abc import RequiredParamsABCMixin, Serializable

__all__ = [
"ClientBoundPacket",
"GameState",
"PacketDirection",
"Packet",
"PacketDirection",
"ServerBoundPacket",
"ClientBoundPacket",
]


Expand Down Expand Up @@ -118,7 +118,7 @@ def from_packet_class(cls, packet_class: type[Packet], buffer: Buffer, message:
elif isinstance(packet_class, ClientBoundPacket):
direction = PacketDirection.CLIENTBOUND
else:
raise ValueError(
raise TypeError(
"Unable to determine the packet direction. Got a packet class which doesn't "
"inherit from ServerBoundPacket nor ClientBoundPacket class."
)
Expand Down
36 changes: 18 additions & 18 deletions mcproto/protocol/base_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,20 @@
from collections.abc import Awaitable, Callable
from enum import Enum
from itertools import count
from typing import Literal, TypeVar, Union, overload
from typing import Literal, TypeVar, overload

from typing_extensions import TypeAlias

from mcproto.protocol.utils import from_twos_complement, to_twos_complement

__all__ = [
"FLOAT_FORMATS_TYPE",
"INT_FORMATS_TYPE",
"BaseAsyncReader",
"BaseAsyncWriter",
"BaseSyncReader",
"BaseSyncWriter",
"StructFormat",
"INT_FORMATS_TYPE",
"FLOAT_FORMATS_TYPE",
]

T = TypeVar("T")
Expand Down Expand Up @@ -52,23 +52,23 @@ class StructFormat(str, Enum):
ULONGLONG = "Q"


INT_FORMATS_TYPE: TypeAlias = Union[
Literal[StructFormat.BYTE],
Literal[StructFormat.UBYTE],
Literal[StructFormat.SHORT],
Literal[StructFormat.USHORT],
Literal[StructFormat.INT],
Literal[StructFormat.UINT],
Literal[StructFormat.LONG],
Literal[StructFormat.ULONG],
Literal[StructFormat.LONGLONG],
Literal[StructFormat.ULONGLONG],
INT_FORMATS_TYPE: TypeAlias = Literal[
StructFormat.BYTE,
StructFormat.UBYTE,
StructFormat.SHORT,
StructFormat.USHORT,
StructFormat.INT,
StructFormat.UINT,
StructFormat.LONG,
StructFormat.ULONG,
StructFormat.LONGLONG,
StructFormat.ULONGLONG,
]

FLOAT_FORMATS_TYPE: TypeAlias = Union[
Literal[StructFormat.FLOAT],
Literal[StructFormat.DOUBLE],
Literal[StructFormat.HALFFLOAT],
FLOAT_FORMATS_TYPE: TypeAlias = Literal[
StructFormat.FLOAT,
StructFormat.DOUBLE,
StructFormat.HALFFLOAT,
]

# endregion
Expand Down
2 changes: 1 addition & 1 deletion mcproto/protocol/utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from __future__ import annotations

__all__ = ["to_twos_complement", "from_twos_complement"]
__all__ = ["from_twos_complement", "to_twos_complement"]


def to_twos_complement(number: int, bits: int) -> int:
Expand Down
2 changes: 1 addition & 1 deletion mcproto/types/chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@

__all__ = [
"ChatMessage",
"RawChatMessageDict",
"RawChatMessage",
"RawChatMessageDict",
]


Expand Down
29 changes: 13 additions & 16 deletions mcproto/types/nbt.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,22 @@
from mcproto.types.abc import MCType

__all__ = [
"NBTagConvertible",
"NBTagType",
"NBTag",
"EndNBT",
"ByteNBT",
"ShortNBT",
"IntNBT",
"LongNBT",
"FloatNBT",
"DoubleNBT",
"ByteArrayNBT",
"StringNBT",
"ListNBT",
"ByteNBT",
"CompoundNBT",
"DoubleNBT",
"EndNBT",
"FloatNBT",
"IntArrayNBT",
"IntNBT",
"ListNBT",
"LongArrayNBT",
"LongNBT",
"NBTag",
"NBTagConvertible",
"NBTagType",
"ShortNBT",
"StringNBT",
]

"""
Expand Down Expand Up @@ -770,10 +770,7 @@ def read_from(cls, buf: Buffer, with_type: bool = True, with_name: bool = True)
if buf.remaining < length:
raise IOError("Buffer does not contain enough data to read the string.")
data = buf.read(length)
try:
return StringNBT(data.decode("utf-8"), name=name)
except UnicodeDecodeError:
raise # We want to know it
return StringNBT(data.decode("utf-8"), name=name)

@override
def __str__(self) -> str:
Expand Down
Loading

0 comments on commit 3eeeb8e

Please sign in to comment.