Skip to content

Commit fa83f06

Browse files
authored
fix: linter issues & add additionnal test (#13)
1 parent 0b9dca3 commit fa83f06

File tree

10 files changed

+105
-50
lines changed

10 files changed

+105
-50
lines changed

.github/workflows/build.yml

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,11 @@ jobs:
2121
- name: Checkout
2222
uses: actions/checkout@v4
2323

24-
- name: Install Python ${{ matrix.python-version }}
25-
uses: actions/setup-python@v5
26-
with:
27-
python-version: ${{ matrix.python-version }}
28-
29-
- name: Install PDM
24+
- name: Setup Python ${{ matrix.python-version }} & PDM
3025
uses: pdm-project/setup-pdm@v4
31-
32-
- name: Cache the Virtual Env
33-
uses: actions/cache@v4
3426
with:
35-
path: ./.venv
36-
key: venv-${{ matrix.python-version }}-${{ hashFiles('pdm.lock') }}
27+
python-version: ${{ matrix.python-version }}
28+
cache: true
3729

3830
- name: Install the project dependencies
3931
run: pdm install -d

.github/workflows/release.yml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ on:
55
tags:
66
- "*"
77

8+
env:
9+
PYTHON_VERSION: 3.13
10+
811
jobs:
912
release:
1013
runs-on: ubuntu-latest
@@ -18,13 +21,10 @@ jobs:
1821
- name: Checkout
1922
uses: actions/checkout@v4
2023

21-
- name: Install Python
22-
uses: actions/setup-python@v5
23-
with:
24-
python-version: "3.13"
25-
26-
- name: Install PDM
24+
- name: Setup Python & PDM
2725
uses: pdm-project/setup-pdm@v4
26+
with:
27+
python-version: ${{ env.PYTHON_VERSION }}
2828

2929
- name: Install the project dependencies
3030
run: pdm install -d

.github/workflows/tests.yml

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
name: Run tests
2+
on:
3+
push:
4+
branches:
5+
- master
6+
tags:
7+
- v*
8+
pull_request:
9+
branches:
10+
- master
11+
12+
env:
13+
PYTHON_VERSION: 3.13
14+
15+
jobs:
16+
lint:
17+
name: Validate Linter
18+
runs-on: ubuntu-latest
19+
steps:
20+
- name: Checkout
21+
uses: actions/checkout@v4
22+
23+
- name: Setup Python & PDM
24+
uses: pdm-project/setup-pdm@v4
25+
with:
26+
python-version: ${{ env.PYTHON_VERSION }}
27+
cache: true
28+
29+
- name: Install the project dependencies
30+
run: pdm install -d
31+
32+
- name: Lint the project
33+
run: pdm check

pyproject.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,13 @@ path = "src/tcgdexsdk/__init__.py"
7070
# Ruff #
7171
########
7272
[tool.ruff]
73-
line-length = 80
73+
line-length = 120
7474
respect-gitignore = true
7575
include = ["src/**/*.py"]
7676

77+
[tool.ruff.lint.pycodestyle]
78+
ignore-overlong-task-comments = true
79+
7780
[tool.ruff.lint]
7881
select = ["E", "F", "UP", "B", "SIM", "I"]
7982

src/tcgdexsdk/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from tcgdexsdk.models.Set import Set
1111
from tcgdexsdk.models.SetResume import SetResume
1212
from tcgdexsdk.models.StringEndpoint import StringEndpoint
13+
from tcgdexsdk.query import Query
1314
from tcgdexsdk.tcgdex import TCGdex
1415

1516
__all__ = [
@@ -23,4 +24,5 @@
2324
"Set",
2425
"SetResume",
2526
"StringEndpoint",
27+
"Query",
2628
]

src/tcgdexsdk/endpoints/Endpoint.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
from typing import Generic, List, Optional, Type, TypeVar, Union
22

33
from tcgdexsdk.models.Model import Model
4-
from tcgdexsdk.utils import fetch, fetch_list
54
from tcgdexsdk.query import Query
5+
from tcgdexsdk.utils import fetch, fetch_list
66

77
# Generic type variables
88
Item = TypeVar('Item', bound=Model)
@@ -20,7 +20,11 @@ def __init__(self,
2020
self.endpoint = endpoint
2121

2222
async def get(self, id: str) -> Optional[Item]:
23-
return fetch(self.tcgdex, f"{self.tcgdex.getEndpoint()}/{self.tcgdex.language}/{self.endpoint}/{id.replace(' ', '%20')}", self.item_model)
23+
return fetch(
24+
self.tcgdex,
25+
f"{self.tcgdex.getEndpoint()}/{self.tcgdex.language}/{self.endpoint}/{id.replace(' ', '%20')}",
26+
self.item_model
27+
)
2428

2529
async def list(self, query: Optional[Query] = None) -> List[ListModel]:
2630
url = f"{self.tcgdex.getEndpoint()}/{self.tcgdex.language}/{self.endpoint}"
@@ -29,7 +33,11 @@ async def list(self, query: Optional[Query] = None) -> List[ListModel]:
2933
return fetch_list(self.tcgdex, url, self.list_model)
3034

3135
def getSync(self, id: str) -> Optional[Item]:
32-
return fetch(self.tcgdex, f"{self.tcgdex.getEndpoint()}/{self.tcgdex.language}/{self.endpoint}/{id.replace(' ', '%20')}", self.item_model)
36+
return fetch(
37+
self.tcgdex,
38+
f"{self.tcgdex.getEndpoint()}/{self.tcgdex.language}/{self.endpoint}/{id.replace(' ', '%20')}",
39+
self.item_model
40+
)
3341

3442
def listSync(self, query: Optional[Query] = None) -> List[ListModel]:
3543
url = f"{self.tcgdex.getEndpoint()}/{self.tcgdex.language}/{self.endpoint}"

src/tcgdexsdk/query.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from urllib.parse import quote
22

3+
34
class Query:
45
def __init__(self):
56
self.params = []
@@ -12,7 +13,9 @@ def encode(self, value):
1213
)
1314

1415
def build(self):
15-
return '?' + '&'.join(f"{self.encode(item['key'])}={self.encode(item['value'])}" for item in self.params)
16+
return '?' + '&'.join(
17+
f"{self.encode(item['key'])}={self.encode(item['value'])}" for item in self.params
18+
)
1619

1720
def includes(self, key: str, value: str):
1821
return self.contains(key, value)
@@ -67,7 +70,10 @@ def isNull(self, key: str):
6770

6871
def paginate(self, page: int, itemsPerPage: int):
6972
self.params.append({'key': 'pagination:page', 'value': page})
70-
self.params.append({'key': 'pagination:itemsPerPage', 'value': itemsPerPage})
73+
self.params.append({
74+
'key': 'pagination:itemsPerPage',
75+
'value': itemsPerPage
76+
})
7177
return self
7278

7379
def notEqual(self, key: str, value: str):

src/tcgdexsdk/tcgdex.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
from typing import Union
2-
from warnings import deprecated
32

43
from tcgdexsdk.endpoints.Endpoint import Endpoint
54
from tcgdexsdk.enums import Language
@@ -10,6 +9,7 @@
109
from tcgdexsdk.models.Set import Set
1110
from tcgdexsdk.models.SetResume import SetResume
1211
from tcgdexsdk.models.StringEndpoint import StringEndpoint
12+
from tcgdexsdk.utils import deprecated
1313

1414

1515
class TCGdex:
@@ -28,7 +28,7 @@ def getEndpoint(self) -> str:
2828
@deprecated("use (get|set)endpoint instead")
2929
def URI(self):
3030
"""
31-
@Deprecated: use `getEndpoint()` or `setEndpoint()` instead.
31+
@Deprecated: use `getEndpoint()` or `setEndpoint()` instead.
3232
"""
3333
return self.getEndpoint()
3434

@@ -58,9 +58,7 @@ def __init__(self, language: Union[str, Language] = Language.EN):
5858
self.trainerType = Endpoint(self, StringEndpoint, str, "trainer-types")
5959
self.suffix = Endpoint(self, StringEndpoint, str, "suffixes")
6060
self.stage = Endpoint(self, StringEndpoint, str, "stages")
61-
self.regulationMark = Endpoint(
62-
self, StringEndpoint, str, "regulation-marks"
63-
)
61+
self.regulationMark = Endpoint(self, StringEndpoint, str, "regulation-marks")
6462
self.energyType = Endpoint(self, StringEndpoint, str, "energy-types")
6563
self.dexId = Endpoint(self, StringEndpoint, int, "dex-ids")
6664
self.type = Endpoint(self, StringEndpoint, str, "types")
@@ -69,4 +67,3 @@ def __init__(self, language: Union[str, Language] = Language.EN):
6967
self.illustrator = Endpoint(self, StringEndpoint, str, "illustrators")
7068
self.hp = Endpoint(self, StringEndpoint, int, "hp")
7169
self.category = Endpoint(self, StringEndpoint, str, "categories")
72-

src/tcgdexsdk/utils.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import json
22
import time
3-
from functools import lru_cache
3+
import warnings
4+
from functools import lru_cache, wraps
45
from http.client import HTTPResponse
56
from typing import List, Optional, Type, TypeVar
67
from urllib.request import Request, urlopen
@@ -62,3 +63,17 @@ def fetch_list(tcgdex, url: str, cls: Type[_T]) -> List[_T]:
6263

6364
def download_image(url: str) -> HTTPResponse:
6465
return urlopen(_request(url))
66+
67+
68+
def deprecated(reason):
69+
def decorator(func):
70+
message = f"{func.__name__} is deprecated: {reason}"
71+
72+
@wraps(func)
73+
def wrapper(*args, **kwargs):
74+
warnings.warn(message, category=DeprecationWarning, stacklevel=2)
75+
return func(*args, **kwargs)
76+
77+
return wrapper
78+
79+
return decorator

tests/tests.py

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import unittest
22
from typing import Callable
33
from unittest.mock import patch
4-
from warnings import deprecated
4+
55
import vcr
66

77
from tcgdexsdk import Language, TCGdex
@@ -14,26 +14,25 @@
1414
from tcgdexsdk.models.StringEndpoint import StringEndpoint
1515
from tcgdexsdk.query import Query
1616

17+
1718
def _use_cassette(test: Callable) -> Callable:
1819
return vcr.use_cassette(f"tests/.fixtures/{test.__name__}.yaml")(test)
1920

2021

2122
class APITest(unittest.IsolatedAsyncioTestCase):
2223
def setUp(self):
2324
self.api = TCGdex(Language.EN)
24-
25-
26-
@deprecated("this test is deprecated")
25+
2726
@patch("tcgdexsdk.endpoints.Endpoint.fetch")
2827
@patch("tcgdexsdk.endpoints.Endpoint.fetch_list")
2928
async def test_uri(self, mock_fetch_list, mock_fetch):
3029
api = TCGdex(Language.EN)
31-
30+
3231
api.URI = "http://localhost:3000/v2"
33-
32+
3433
await api.card.get("swsh1-136")
3534
mock_fetch.assert_called_once_with(api, "http://localhost:3000/v2/en/cards/swsh1-136", Card)
36-
35+
3736
await api.card.list()
3837
mock_fetch_list.assert_called_once_with(api, "http://localhost:3000/v2/en/cards", CardResume)
3938

@@ -55,17 +54,17 @@ async def test_endpoint(self, mock_fetch_list, mock_fetch):
5554
@patch("tcgdexsdk.endpoints.Endpoint.fetch_list")
5655
async def test_language(self, mock_fetch_list, mock_fetch):
5756
api = TCGdex()
58-
57+
5958
# Default language should be english
6059
self.assertEqual(api.getLanguage(), Language.EN)
61-
60+
6261
# Card should be fetched in english
6362
await api.card.get("swsh1-136")
6463
mock_fetch.assert_called_once_with(api, f"{api.getEndpoint()}/en/cards/swsh1-136", Card)
6564

6665
# Card should be fetched in french
6766
api.setLanguage(Language.FR)
68-
67+
6968
# Test that the language is set correctly
7069
self.assertEqual(api.getLanguage(), Language.FR)
7170
await api.card.get("swsh1-136")
@@ -74,25 +73,25 @@ async def test_language(self, mock_fetch_list, mock_fetch):
7473
@_use_cassette
7574
async def test_fr(self):
7675
tcg = TCGdex(Language.FR)
77-
res = await tcg.card.get('swsh3-136')
78-
self.assertEqual(res.name, 'Fouinar')
79-
tcg2 = TCGdex('fr')
80-
res = await tcg2.card.get('swsh3-136')
81-
self.assertEqual(res.name, 'Fouinar')
76+
res = await tcg.card.get("swsh3-136")
77+
self.assertEqual(res.name, "Fouinar")
78+
tcg2 = TCGdex("fr")
79+
res = await tcg2.card.get("swsh3-136")
80+
self.assertEqual(res.name, "Fouinar")
8281

8382
@_use_cassette
8483
async def test_query_equal(self):
8584
tcg = TCGdex(Language.EN)
86-
res = await tcg.card.list(Query().equal('name', 'Furret'))
85+
res = await tcg.card.list(Query().equal("name", "Furret"))
8786
for card in res:
88-
self.assertEqual(card.name, 'Furret')
89-
87+
self.assertEqual(card.name, "Furret")
88+
9089
@_use_cassette
9190
async def test_query_not_equal(self):
9291
tcg = TCGdex()
93-
res = await tcg.card.list(Query().notEqual('name', 'Furret'))
92+
res = await tcg.card.list(Query().notEqual("name", "Furret"))
9493
for card in res:
95-
self.assertNotEqual(card.name, 'Furret')
94+
self.assertNotEqual(card.name, "Furret")
9695

9796
@_use_cassette
9897
async def test_card_resume(self):

0 commit comments

Comments
 (0)