Skip to content

Commit c4a636e

Browse files
committed
[ruff] Add flake8-annotations rules
1 parent 967438b commit c4a636e

File tree

6 files changed

+61
-34
lines changed

6 files changed

+61
-34
lines changed

pyproject.toml

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,8 @@ select = [
202202
"PERF",
203203
# Ruff-specific rules
204204
"RUF",
205+
# flake8-annotations
206+
"ANN",
205207

206208
]
207209
ignore = [
@@ -218,7 +220,15 @@ ignore = [
218220
# Mutable class attributes should be annotated with `typing.ClassVar`
219221
"RUF012",
220222
# PEP 484 prohibits implicit `Optional`
221-
"RUF013"
223+
"RUF013",
224+
# Missing type annotation for {name} in method
225+
"ANN101",
226+
# Missing type annotation for {name} in classmethod
227+
"ANN102",
228+
# Missing return type annotation for private function {name}
229+
"ANN202",
230+
# Missing return type annotation for special method {name}
231+
"ANN204",
222232
]
223233

224234

tibiapy/builders/highscores.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from __future__ import annotations
22

3-
from typing import List, TYPE_CHECKING
3+
from typing import List, Set, TYPE_CHECKING
44

55
from tibiapy.models import Highscores
66

@@ -42,7 +42,7 @@ def battleye_filter(self, battleye_filter: HighscoresBattlEyeType) -> Self:
4242
self._battleye_filter = battleye_filter
4343
return self
4444

45-
def pvp_types_filter(self, pvp_types_filter: PvpTypeFilter) -> Self:
45+
def pvp_types_filter(self, pvp_types_filter: Set[PvpTypeFilter]) -> Self:
4646
self._pvp_types_filter = pvp_types_filter
4747
return self
4848

tibiapy/parsers/character.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ def _parse_house_column(cls, builder: CharacterBuilder, column: bs4.Tag):
209209
house_link = parse_link_info(house_link_tag)
210210
builder.add_house(
211211
CharacterHouse(
212-
id=house_link["query"]["houseid"],
212+
id=int(house_link["query"]["houseid"]),
213213
name=house_link["text"],
214214
town=house_link["query"]["town"],
215215
paid_until=paid_until_date,

tibiapy/parsers/highscores.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
import datetime
55
import re
66
from collections import OrderedDict
7-
from typing import Optional, TYPE_CHECKING
7+
from typing import Dict, Optional, TYPE_CHECKING
8+
9+
import bs4
810

911
from tibiapy.builders.highscores import HighscoresBuilder
1012
from tibiapy.enums import HighscoresBattlEyeType, HighscoresCategory, HighscoresProfession, PvpTypeFilter
@@ -34,7 +36,7 @@ def from_content(cls, content: str) -> Optional[Highscores]:
3436
3537
Notes
3638
-----
37-
Tibia.com only shows up to 50 entries per page, so in order to obtain the full highscores, all pages must be
39+
Tibia.com only shows up to 50 entries per page, so to obtain the full highscores, all pages must be
3840
obtained individually and merged into one.
3941
4042
Parameters
@@ -73,7 +75,7 @@ def from_content(cls, content: str) -> Optional[Highscores]:
7375

7476
# region Private methods
7577
@classmethod
76-
def _parse_entries_table(cls, builder: HighscoresBuilder, table):
78+
def _parse_entries_table(cls, builder: HighscoresBuilder, table: bs4.Tag) -> None:
7779
"""Parse the table containing the highscore entries.
7880
7981
Parameters
@@ -100,7 +102,7 @@ def _parse_entries_table(cls, builder: HighscoresBuilder, table):
100102
cls._parse_entry(builder, cols_raw)
101103

102104
@classmethod
103-
def _parse_filters_table(cls, builder, form):
105+
def _parse_filters_table(cls, builder: HighscoresBuilder, form: bs4.Tag) -> None:
104106
"""Parse the filters table found in a highscores page.
105107
106108
Parameters
@@ -120,7 +122,7 @@ def _parse_filters_table(cls, builder, form):
120122
builder.available_worlds([v for v in data.available_options["world"].values() if v])
121123

122124
@classmethod
123-
def _parse_tables(cls, parsed_content):
125+
def _parse_tables(cls, parsed_content: bs4.BeautifulSoup) -> Dict[str, bs4.Tag]:
124126
"""Parse the information tables found in a highscores page.
125127
126128
Parameters
@@ -145,7 +147,7 @@ def _parse_tables(cls, parsed_content):
145147
return output
146148

147149
@classmethod
148-
def _parse_entry(cls, builder, cols):
150+
def _parse_entry(cls, builder: HighscoresBuilder, cols: bs4.ResultSet) -> None:
149151
"""Parse an entry's row and adds the result to py:attr:`entries`.
150152
151153
Parameters

tibiapy/urls.py

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
import datetime
55
import urllib.parse
6-
from typing import Optional, Set, TYPE_CHECKING
6+
from typing import Optional, Set, TYPE_CHECKING, Tuple, Union
77

88
if TYPE_CHECKING:
99
from tibiapy.enums import (BazaarType, HighscoresBattlEyeType, HighscoresCategory, HighscoresProfession, HouseOrder,
@@ -12,22 +12,29 @@
1212
from tibiapy.models import AuctionFilters
1313

1414

15-
def get_tibia_url(section, subtopic=None, *args, anchor=None, test=False, **kwargs):
15+
def get_tibia_url(
16+
section: str,
17+
subtopic: str = None,
18+
*args: Tuple[str, Union[str, int]],
19+
anchor: str = None,
20+
test: bool = False,
21+
**kwargs: Union[str, int],
22+
) -> str:
1623
"""Build a URL to Tibia.com with the given parameters.
1724
1825
Parameters
1926
----------
2027
section: :class:`str`
21-
The desired section (e.g. community, abouttibia, manual, library)
28+
The desired section (e.g., community, abouttibia, manual, library)
2229
subtopic: :class:`str`, optional
23-
The desired subtopic (e.g. characters, guilds, houses, etc)
30+
The desired subtopic (e.g., characters, guilds, houses, etc.)
2431
anchor: :class:`str`
2532
A link anchor to add to the link.
2633
args:
2734
A list of key-value pairs to add as query parameters.
2835
This allows passing multiple parameters with the same name.
2936
kwargs:
30-
Additional parameters to pass to the url as query parameters (e.g name, world, houseid, etc)
37+
Additional parameters to pass to the url as query parameters (e.g., name, world, houseid, etc.)
3138
test: :class:`bool`
3239
Whether to use the test website or not.
3340
@@ -58,7 +65,7 @@ def get_tibia_url(section, subtopic=None, *args, anchor=None, test=False, **kwar
5865
if value is None:
5966
continue
6067

61-
params[key] = value
68+
params[key] = str(value)
6269

6370
url += urllib.parse.urlencode(params)
6471
if args:
@@ -104,7 +111,7 @@ def get_character_url(name: str) -> str:
104111

105112

106113
def get_world_guilds_url(world: str) -> str:
107-
"""Get the URL to guild list of a specific world.
114+
"""Get the URL to the guild list of a specific world.
108115
109116
Parameters
110117
----------
@@ -146,7 +153,7 @@ def get_guild_wars_url(name: str) -> str:
146153
Returns
147154
-------
148155
:class:`str`
149-
The URL to the guild's wars page.
156+
The URL to the guild's wars' page.
150157
"""
151158
return get_tibia_url("community", "guilds", page="guildwars", action="view", GuildName=name)
152159

@@ -260,7 +267,7 @@ def get_forum_section_url_by_name(section_name: str) -> str:
260267
return get_tibia_url("forum", section_name)
261268

262269

263-
def get_world_boards_url():
270+
def get_world_boards_url() -> str:
264271
"""Get the URL to the World Boards section in Tibia.com.
265272
266273
Returns
@@ -271,7 +278,7 @@ def get_world_boards_url():
271278
return get_tibia_url("forum", "worldboards")
272279

273280

274-
def get_trade_boards_url():
281+
def get_trade_boards_url() -> str:
275282
"""Get the URL to the Trade Boards section in Tibia.com.
276283
277284
Returns
@@ -282,7 +289,7 @@ def get_trade_boards_url():
282289
return get_tibia_url("forum", "tradeboards")
283290

284291

285-
def get_community_boards_url():
292+
def get_community_boards_url() -> str:
286293
"""Get the URL to the Community Boards section in Tibia.com.
287294
288295
Returns
@@ -293,7 +300,7 @@ def get_community_boards_url():
293300
return get_tibia_url("forum", "communityboards")
294301

295302

296-
def get_support_boards_url():
303+
def get_support_boards_url() -> str:
297304
"""Get the URL to the Support Boards section in Tibia.com.
298305
299306
Returns
@@ -360,7 +367,7 @@ def get_forum_thread_url(thread_id: int, page: int = 1) -> str:
360367
return get_tibia_url("forum", None, action="thread", threadid=thread_id, pagenumber=page)
361368

362369

363-
def get_forum_post_url(post_id):
370+
def get_forum_post_url(post_id: int) -> str:
364371
"""Get the URL to a specific post.
365372
366373
Parameters
@@ -380,7 +387,7 @@ def get_highscores_url(
380387
world: str = None,
381388
category: HighscoresCategory = None,
382389
vocation: HighscoresProfession = None,
383-
page=1,
390+
page: int = 1,
384391
battleye_type: HighscoresBattlEyeType = None,
385392
pvp_types: Set[PvpTypeFilter] = None,
386393
) -> str:
@@ -485,7 +492,7 @@ def get_houses_section_url(world: str, town: str, house_type: HouseType, status:
485492
return get_tibia_url("community", "houses", **{k: v for k, v in params.items() if v is not None})
486493

487494

488-
def get_auction_url(auction_id: int):
495+
def get_auction_url(auction_id: int) -> str:
489496
"""Get the URL to the Tibia.com detail page of an auction with a given id.
490497
491498
Parameters
@@ -501,7 +508,7 @@ def get_auction_url(auction_id: int):
501508
return get_tibia_url("charactertrade", "currentcharactertrades", page="details", auctionid=auction_id)
502509

503510

504-
def get_bazaar_url(type: BazaarType, page: int = 1, filters: AuctionFilters = None):
511+
def get_bazaar_url(type: BazaarType, page: int = 1, filters: AuctionFilters = None) -> str:
505512
"""Get the URL to the list of current auctions in Tibia.com.
506513
507514
Parameters
@@ -522,7 +529,7 @@ def get_bazaar_url(type: BazaarType, page: int = 1, filters: AuctionFilters = No
522529
return get_tibia_url("charactertrade", type.subtopic, currentpage=page, **query_params)
523530

524531

525-
def get_cm_post_archive_url(from_date: datetime.date, to_date: datetime.date, page=1):
532+
def get_cm_post_archive_url(from_date: datetime.date, to_date: datetime.date, page: int = 1) -> str:
526533
"""Get the URL to the CM Post Archive for the given date range.
527534
528535
Parameters
@@ -629,7 +636,7 @@ def get_boostable_bosses_url() -> str:
629636
return get_tibia_url("library", "boostablebosses")
630637

631638

632-
def _to_yes_no(value: Optional[bool]):
639+
def _to_yes_no(value: Optional[bool]) -> Optional[str]:
633640
if value is None:
634641
return None
635642

@@ -642,7 +649,7 @@ def get_spells_section_url(
642649
spell_type: SpellType = None,
643650
is_premium: bool = None,
644651
sort: SpellSorting = None,
645-
):
652+
) -> str:
646653
"""Get the URL to the spells section with the desired filtering parameters.
647654
648655
Parameters

tibiapy/utils.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import re
66
import urllib.parse
77
from collections import defaultdict
8-
from typing import Any, Callable, Dict, Iterable, List, Optional, Tuple, Type, TypeVar, Union
8+
from typing import Any, Callable, Dict, Iterable, List, Optional, Tuple, Type, TypeVar, TypedDict, Union
99

1010
import bs4
1111
from pydantic import BaseModel
@@ -54,7 +54,7 @@ def clean_text(tag: Union[bs4.PageElement, str]) -> str:
5454
return text.replace("\xa0", " ").strip()
5555

5656

57-
def convert_line_breaks(element: bs4.Tag):
57+
def convert_line_breaks(element: bs4.Tag) -> None:
5858
"""Convert the <br> tags in a HTML elements to actual line breaks.
5959
6060
Parameters
@@ -133,7 +133,7 @@ def parse_form_data(form: bs4.Tag) -> FormData:
133133
return form_data
134134

135135

136-
def parse_integer(number: str, default: Optional[int] = 0):
136+
def parse_integer(number: str, default: Optional[int] = 0) -> int:
137137
"""Parse a string representing an integer, ignoring commas or periods.
138138
139139
Parameters
@@ -159,7 +159,15 @@ def parse_integer(number: str, default: Optional[int] = 0):
159159
return default
160160

161161

162-
def parse_link_info(link_tag: bs4.Tag):
162+
class LinkInfo(TypedDict):
163+
"""Represent the dictionary containing link information."""
164+
165+
text: str
166+
url: str
167+
query: Dict[str, Union[List[str], str]]
168+
169+
170+
def parse_link_info(link_tag: bs4.Tag) -> LinkInfo:
163171
"""Parse the information of a link tag.
164172
165173
It will parse the link's content, target URL as well as the query parameters where applicable.
@@ -509,7 +517,7 @@ def parse_tibiacom_tables(parsed_content: bs4.BeautifulSoup) -> Dict[str, bs4.Ta
509517
return tables
510518

511519

512-
def try_enum(cls: Type[T], val: Any, default: D = None) -> Union[T, D]:
520+
def try_enum(cls: Type[T], val: Any, default: D = None) -> Union[T, D]: # noqa: ANN401
513521
"""Attempt to convert a value into their enum value.
514522
515523
Parameters

0 commit comments

Comments
 (0)