Skip to content

Commit 50bf269

Browse files
authored
Enable ruff PLR rule (#37)
1 parent b7b545d commit 50bf269

File tree

9 files changed

+31
-15
lines changed

9 files changed

+31
-15
lines changed

pyproject.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,14 @@ line-length = 88
4848
target-version = "py311"
4949

5050
[tool.ruff.lint]
51-
select = ["E4", "E7", "E9", "F", "UP", "S", "ANN", "C4", "I", "RUF", "SIM"]
51+
select = ["E4", "E7", "E9", "F", "UP", "S", "ANN", "C4", "I", "RUF", "SIM", "PLR"]
5252
# select = ["ALL"]
5353
ignore = ["ANN003", "ANN401", "D", "COM"]
5454

5555

5656
[tool.ruff.lint.per-file-ignores]
57-
"**/scripts/*" = ["UP", "S104", "ALL"]
58-
"tests/*" = ["S101"]
57+
"**/scripts/*" = ["UP", "S104"]
58+
"tests/*" = ["S101", "PLR2004"]
5959
"src/pythonxbox/authentication/xal.py" = ["S101"]
6060

6161
[tool.ruff.lint.isort]

src/pythonxbox/api/provider/cqs/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ async def get_channel_list(
4848
resp.raise_for_status()
4949
return CqsChannelListResponse(**resp.json())
5050

51-
async def get_schedule(
51+
async def get_schedule( # noqa: PLR0913
5252
self,
5353
locale_info: str,
5454
headend_id: str,

src/pythonxbox/api/provider/message/__init__.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
TODO: Support group messaging
55
"""
66

7+
from http import HTTPStatus
78
from typing import ClassVar
89

910
from pythonxbox.api.provider.baseprovider import BaseProvider
@@ -13,6 +14,8 @@
1314
SendMessageResponse,
1415
)
1516

17+
MESSAGE_MAX_LEN = 256
18+
1619

1720
class MessageProvider(BaseProvider):
1821
MSG_URL = "https://xblmessaging.xboxlive.com"
@@ -82,7 +85,7 @@ async def delete_conversation(
8285
resp = await self.client.session.put(
8386
url, json=post_data, headers=self.HEADERS_HORIZON, **kwargs
8487
)
85-
return resp.status_code == 200
88+
return resp.status_code == HTTPStatus.OK
8689

8790
async def delete_message(
8891
self, conversation_id: str, message_id: str, **kwargs
@@ -102,7 +105,7 @@ async def delete_message(
102105
resp = await self.client.session.delete(
103106
url, headers=self.HEADERS_MESSAGE, **kwargs
104107
)
105-
return resp.status_code == 200
108+
return resp.status_code == HTTPStatus.OK
106109

107110
async def send_message(
108111
self, xuid: str, message_text: str, **kwargs
@@ -117,8 +120,10 @@ async def send_message(
117120
Returns:
118121
:class:`SendMessageResponse`: Send Message Response
119122
"""
120-
if len(message_text) > 256:
121-
raise ValueError("Message text exceeds max length of 256 chars")
123+
if len(message_text) > MESSAGE_MAX_LEN:
124+
raise ValueError(
125+
f"Message text exceeds max length of {MESSAGE_MAX_LEN} chars"
126+
)
122127

123128
url = f"{self.MSG_URL}/network/Xbox/users/me/conversations/users/xuid({xuid})"
124129
post_data = {

src/pythonxbox/api/provider/presence/__init__.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
Presence - Get online status of friends
33
"""
44

5+
from http import HTTPStatus
56
from typing import ClassVar
67

78
from pythonxbox.api.provider.baseprovider import BaseProvider
@@ -61,7 +62,9 @@ async def get_presence_batch(
6162
6263
Returns: List[:class:`PresenceItem`]: List of presence items
6364
"""
64-
if len(xuids) > 1100:
65+
MAX_XUIDS = 1100
66+
67+
if len(xuids) > MAX_XUIDS:
6568
raise Exception("Xuid list length is > 1100")
6669

6770
url = self.PRESENCE_URL + "/users/batch"
@@ -112,4 +115,4 @@ async def set_presence_own(self, presence_state: PresenceState, **kwargs) -> boo
112115
resp = await self.client.session.put(
113116
url, json=data, headers=self.HEADERS_PRESENCE, **kwargs
114117
)
115-
return resp.status_code == 200
118+
return resp.status_code == HTTPStatus.OK

src/pythonxbox/api/provider/userstats/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"""
44

55
from typing import ClassVar
6+
67
from pythonxbox.api.provider.ratelimitedprovider import RateLimitedProvider
78
from pythonxbox.api.provider.userstats.models import (
89
GeneralStatsField,

src/pythonxbox/authentication/manager.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
Authenticate with Windows Live Server and Xbox Live.
55
"""
66

7+
from http import HTTPStatus
78
import logging
89

910
import httpx
@@ -152,7 +153,7 @@ async def request_xsts_token(
152153
}
153154

154155
resp = await self.session.post(url, json=data, headers=headers)
155-
if resp.status_code == 401: # if unauthorized
156+
if resp.status_code == HTTPStatus.UNAUTHORIZED: # if unauthorized
156157
print(
157158
"Failed to authorize you! Your password or username may be wrong or you are trying to use child account (< 18 years old)"
158159
)

src/pythonxbox/authentication/xal.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,12 +95,16 @@ def _get_random_bytes(length: int) -> bytes:
9595
@staticmethod
9696
def _generate_code_verifier() -> str:
9797
# https://tools.ietf.org/html/rfc7636
98+
MIN_LEN = 43
99+
MAX_LEN = 128
100+
98101
code_verifier = (
99102
base64.urlsafe_b64encode(XALManager._get_random_bytes(32))
100103
.decode()
101104
.rstrip("=")
102105
)
103-
assert len(code_verifier) >= 43 and len(code_verifier) <= 128
106+
if not (MIN_LEN <= len(code_verifier) <= MAX_LEN):
107+
raise ValueError(f"Invalid code_verifier length: {len(code_verifier)}")
104108

105109
return code_verifier
106110

src/pythonxbox/common/request_signer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ def _hash(data: bytes) -> bytes:
151151
return hash.digest()
152152

153153
@staticmethod
154-
def _concat_data_to_sign(
154+
def _concat_data_to_sign( # noqa: PLR0913
155155
signature_version: bytes,
156156
method: str,
157157
path_and_query: str,

src/pythonxbox/scripts/change_gamertag.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
from pythonxbox.common.signed_session import SignedSession
2020
from pythonxbox.scripts import CLIENT_ID, CLIENT_SECRET, TOKENS_FILE
2121

22+
GAMERTAG_MAX_LEN = 15
23+
2224

2325
async def async_main() -> None:
2426
parser = argparse.ArgumentParser(description="Change your gamertag")
@@ -44,8 +46,8 @@ async def async_main() -> None:
4446

4547
args = parser.parse_args()
4648

47-
if len(args.gamertag) > 15:
48-
print("Desired gamertag exceedes limit of 15 chars")
49+
if len(args.gamertag) > GAMERTAG_MAX_LEN:
50+
print(f"Desired gamertag exceedes limit of {GAMERTAG_MAX_LEN} chars")
4951
sys.exit(-1)
5052

5153
if not os.path.exists(args.tokens):

0 commit comments

Comments
 (0)