Skip to content

Commit dd1aa93

Browse files
authored
Merge pull request #17 from sreyassabbani/feat/8-alt-subcat-support
fix(lint): resolve errors from merged PR #14
2 parents 615e710 + 6ffd5da commit dd1aa93

File tree

4 files changed

+49
-41
lines changed

4 files changed

+49
-41
lines changed

qbreader/_api_utils.py

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,17 @@
44

55
import warnings
66
from enum import Enum, EnumType
7-
from typing import Iterable, Optional, Union, Tuple
7+
from typing import Iterable, Optional, Tuple, Union
88

99
from qbreader.types import (
10-
Difficulty,
10+
AlternateSubcategory,
1111
Category,
12+
Difficulty,
1213
Subcategory,
13-
AlternateSubcategory,
14+
UnnormalizedAlternateSubcategory,
1415
UnnormalizedCategory,
1516
UnnormalizedDifficulty,
1617
UnnormalizedSubcategory,
17-
UnnormalizedAlternateSubcategory,
1818
)
1919

2020

@@ -106,7 +106,8 @@ def normalize_subcat(unnormalized_subcats: UnnormalizedCategory):
106106

107107
def category_correspondence(
108108
typed_alt_subcat: AlternateSubcategory,
109-
) -> Tuple[Category, Subcategory]:
109+
) -> Tuple[Category | None, Subcategory | None]:
110+
"""Return the corresponding category/subcategory for a alternate_subcategory."""
110111
if typed_alt_subcat in [
111112
AlternateSubcategory.ASTRONOMY,
112113
AlternateSubcategory.COMPUTER_SCIENCE,
@@ -147,18 +148,19 @@ def category_correspondence(
147148
]:
148149
return (Category.LITERATURE, None)
149150

151+
# Accounts for AlternateSubcategory.PRACTICES and AlternateSubcategory.BELIEFS
152+
return (None, None)
153+
150154

151155
def normalize_cats(
152156
unnormalized_cats: UnnormalizedCategory,
153157
unnormalized_subcats: UnnormalizedSubcategory,
154158
unnormalized_alt_subcats: UnnormalizedAlternateSubcategory,
155-
) -> Tuple[Category, Subcategory, AlternateSubcategory]:
156-
"""
157-
Normalize a single or list of categories, subcategories, and alternate_subcategories
158-
to their corresponding comma-separated strings, taking into account categories and
159-
subcategories that must be added for the alternate_subcategories to work.
160-
"""
161-
159+
) -> Tuple[str, str, str]:
160+
"""Normalize a single or list of categories, subcategories, and\
161+
alternate_subcategories to their corresponding comma-separated strings, taking into\
162+
account categories and subcategories that must be added for the\
163+
alternate_subcategories to work."""
162164
typed_alt_subcats: list[AlternateSubcategory] = []
163165

164166
if isinstance(unnormalized_alt_subcats, str):
@@ -183,8 +185,8 @@ def normalize_cats(
183185
elif isinstance(unnormalized_cats, str):
184186
final_cats = [Category(unnormalized_cats), *to_be_pushed_cats]
185187
elif isinstance(unnormalized_cats, Iterable):
186-
for subcat in unnormalized_cats:
187-
final_cats.append(Subcategory(subcat))
188+
for unnormalized_cat in unnormalized_cats:
189+
final_cats.append(Category(unnormalized_cat))
188190
final_cats.append(*to_be_pushed_cats)
189191

190192
final_subcats = []
@@ -193,8 +195,8 @@ def normalize_cats(
193195
elif isinstance(unnormalized_subcats, str):
194196
final_subcats = [Subcategory(unnormalized_subcats), *to_be_pushed_subcats]
195197
elif isinstance(unnormalized_subcats, Iterable):
196-
for subcat in unnormalized_subcats:
197-
final_subcats.append(Subcategory(subcat))
198+
for unnormalized_subcat in unnormalized_subcats:
199+
final_subcats.append(Subcategory(unnormalized_subcat))
198200
final_subcats.append(*to_be_pushed_subcats)
199201

200202
return (

qbreader/asynchronous.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@
1616
QuestionType,
1717
SearchType,
1818
Tossup,
19+
UnnormalizedAlternateSubcategory,
1920
UnnormalizedCategory,
2021
UnnormalizedDifficulty,
2122
UnnormalizedSubcategory,
22-
UnnormalizedAlternateSubcategory,
2323
Year,
2424
)
2525

@@ -116,7 +116,8 @@ class type.
116116
The subcategories to search for. Can be a single or an array of
117117
`Subcategory` enums or strings. The API does not check for consistency
118118
between categories and subcategories.
119-
alternate_subcategories : qbreader.types.UnnormalizedAlternateSubcategory, optional
119+
alternate_subcategories : qbreader.types.UnnormalizedAlternateSubcategory,\
120+
optional
120121
The alternate subcategories to search for. Can be a single or an array of
121122
`AlternateSubcategory` enums or strings. The API does not check for
122123
consistency between categories and subcategories
@@ -244,7 +245,8 @@ async def random_tossup(
244245
The subcategories to search for. Can be a single or an array of
245246
`Subcategory` enums or strings. The API does not check for consistency
246247
between categories and subcategories.
247-
alternate_subcategories : qbreader.types.UnnormalizedAlternateSubcategory, optional
248+
alternate_subcategories : qbreader.types.UnnormalizedAlternateSubcategory,\
249+
optional
248250
The alternate subcategories to search for. Can be a single or an array of
249251
`AlternateSubcategory` enums or strings. The API does not check for
250252
consistency between categories and subcategories
@@ -327,10 +329,11 @@ async def random_bonus(
327329
The subcategories to search for. Can be a single or an array of
328330
`Subcategory` enums or strings. The API does not check for consistency
329331
between categories and subcategories.
330-
alternate_subcategories: qbreaader.types.UnnormalizedAlternateSubcategory, optional
332+
alternate_subcategories: qbreader.types.UnnormalizedAlternateSubcategory, \
333+
optional
331334
The alternates subcategories to search for. Can be a single or an array of
332-
`AlternateSubcategory` enum variants or strings. The API does not check for consistency
333-
between categories, subcategories, and alternate subcategories.
335+
`AlternateSubcategory` enum variants or strings. The API does not check for
336+
consistency between categories, subcategories, and alternate subcategories.
334337
number : int, default = 1
335338
The number of bonuses to return.
336339
min_year : int, default = Year.MIN_YEAR

qbreader/synchronous.py

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@
1616
QuestionType,
1717
SearchType,
1818
Tossup,
19-
UnnormalizedDifficulty,
19+
UnnormalizedAlternateSubcategory,
2020
UnnormalizedCategory,
21+
UnnormalizedDifficulty,
2122
UnnormalizedSubcategory,
22-
UnnormalizedAlternateSubcategory,
2323
Year,
2424
)
2525

@@ -81,10 +81,11 @@ class type.
8181
The subcategories to search for. Can be a single or an array of
8282
`Subcategory` enums or strings. The API does not check for consistency
8383
between categories and subcategories.
84-
alternate_subcategories: qbreaader.types.UnnormalizedAlternateSubcategory, optional
84+
alternate_subcategories: qbreaader.types.UnnormalizedAlternateSubcategory,\
85+
optional
8586
The alternates subcategories to search for. Can be a single or an array of
86-
`AlternateSubcategory` enum variants or strings. The API does not check for consistency
87-
between categories, subcategories, and alternate subcategories.
87+
`AlternateSubcategory` enum variants or strings. The API does not check for
88+
consistency between categories, subcategories, and alternate subcategories.
8889
maxReturnLength : int, default = 25
8990
The maximum number of questions to return.
9091
tossupPagination : int, default = 1
@@ -209,10 +210,11 @@ def random_tossup(
209210
The subcategories to search for. Can be a single or an array of
210211
`Subcategory` enums or strings. The API does not check for consistency
211212
between categories and subcategories.
212-
alternate_subcategories: qbreaader.types.UnnormalizedAlternateSubcategory, optional
213+
alternate_subcategories: qbreaader.types.UnnormalizedAlternateSubcategory,
214+
optional
213215
The alternates subcategories to search for. Can be a single or an array of
214-
`AlternateSubcategory` enum variants or strings. The API does not check for consistency
215-
between categories, subcategories, and alternate subcategories.
216+
`AlternateSubcategory` enum variants or strings. The API does not check for
217+
consistency between categories, subcategories, and alternate subcategories.
216218
number : int, default = 1
217219
The number of tossups to return.
218220
min_year : int, default = Year.MIN_YEAR
@@ -283,19 +285,20 @@ def random_bonus(
283285
Parameters
284286
----------
285287
difficulties : qbreader.types.UnnormalizedDifficulty, optional
286-
The difficulties to search for. Can be a single or an array of `Difficulty`
287-
enums, strings, or integers.
288+
The difficulties to search for. Can be a single or an array of
289+
`Difficulty` enums, strings, or integers.
288290
categories : qbreader.types.UnnormalizedCategory, optional
289-
The categories to search for. Can be a single or an array of `Category`
290-
enums or strings.
291+
The categories to search for. Can be a single or an array of
292+
`Category` enums or strings.
291293
subcategories : qbreader.types.UnnormalizedSubcategory, optional
292294
The subcategories to search for. Can be a single or an array of
293295
`Subcategory` enums or strings. The API does not check for consistency
294296
between categories and subcategories.
295-
alternate_subcategories: qbreaader.types.UnnormalizedAlternateSubcategory, optional
297+
alternate_subcategories : qbreaader.types.UnnormalizedAlternateSubcategory,
298+
optional
296299
The alternates subcategories to search for. Can be a single or an array of
297-
`AlternateSubcategory` enum variants or strings. The API does not check for consistency
298-
between categories, subcategories, and alternate subcategories.
300+
`AlternateSubcategory` enum variants or strings. The API does not check for
301+
consistency between categories, subcategories, and alternate subcategories.
299302
number : int, default = 1
300303
The number of bonuses to return.
301304
min_year : int, default = Year.MIN_YEAR

qbreader/types.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ def __init__(
291291
self.packet: PacketMetadata = packet
292292
self.set: SetMetadata = set
293293
self.number: int = number
294-
self.alternate_subcategory: AlternateSubcategory = alternate_subcategory
294+
self.alternate_subcategory: AlternateSubcategory | None = alternate_subcategory
295295

296296
@classmethod
297297
def from_json(cls: Type[Self], json: dict[str, Any]) -> Self:
@@ -385,7 +385,7 @@ def __init__(
385385
self.set: SetMetadata = set
386386
self.packet: PacketMetadata = packet
387387
self.number: int = number
388-
self.alternate_subcategory: AlternateSubcategory = alternate_subcategory
388+
self.alternate_subcategory: AlternateSubcategory | None = alternate_subcategory
389389
self.values: Optional[tuple[int, ...]] = tuple(values) if values else None
390390
self.difficultyModifiers: Optional[tuple[DifficultyModifier, ...]] = (
391391
tuple(difficultyModifiers) if difficultyModifiers else None
@@ -688,8 +688,8 @@ def __str__(self) -> str:
688688
UnnormalizedAlternateSubcategory: TypeAlias = Optional[
689689
Union[AlternateSubcategory, str, Iterable[Union[AlternateSubcategory, str]]]
690690
]
691-
"""Type alias for unnormalized alternate subcategories. Union of `AlternateSubcategory`, `str`, and
692-
`collections.abc.Iterable` containing either."""
691+
"""Type alias for unnormalized alternate subcategories. Union of `AlternateSubcategory`,
692+
`str`, and `collections.abc.Iterable` containing either."""
693693

694694

695695
__all__ = (

0 commit comments

Comments
 (0)