Skip to content

Commit 9f97cb3

Browse files
authored
Improve typehint (#92)
* Add pycln * Use typevar. * Embedding traits should not inherit from Baserecommender.
1 parent a1893be commit 9f97cb3

File tree

15 files changed

+37
-21
lines changed

15 files changed

+37
-21
lines changed

.pre-commit-config.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,8 @@ repos:
2626
rev: 20.8b1
2727
hooks:
2828
- id: black
29+
- repo: https://github.com/hadialqattan/pycln
30+
rev: v1.2.4 # Possible releases: https://github.com/hadialqattan/pycln/releases
31+
hooks:
32+
- id: pycln
33+
args: [--config=pyproject.toml]

docs/source/conf.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
# add these directories to sys.path here. If the directory is relative to the
33
# documentation root, use os.path.abspath to make it absolute, like shown here.
44
#
5-
import os
6-
import sys
75
from typing import List
86

97
# Configuration file for the Sphinx documentation builder.

examples/movielens/movielens_1m.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
import json
2-
import logging
3-
import os
42
from typing import List, Tuple, Type
53

64
from scipy import sparse as sps

irspack/dataset/downloader.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import os
21
import urllib.request
32
from abc import ABCMeta, abstractmethod
43
from io import BytesIO

irspack/dataset/movielens/base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from abc import ABCMeta, abstractmethod
1+
from abc import abstractmethod
22
from io import BytesIO
33

44
import pandas as pd

irspack/definitions.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Any, List, Optional, Union
1+
from typing import Union
22

33
import numpy as np
44
from scipy import sparse as sps

irspack/optimizers/_optimizers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import logging
2-
from typing import Any, Dict, List, Optional, Type
2+
from typing import Any, Dict, List, Optional
33

44
from irspack.definitions import InteractionMatrix
55

irspack/recommenders/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
)
66
from irspack.recommenders.base_earlystop import BaseRecommenderWithEarlyStopping
77
from irspack.recommenders.dense_slim import DenseSLIMConfig, DenseSLIMRecommender
8-
from irspack.recommenders.edlae import EDLAERecommender
8+
from irspack.recommenders.edlae import EDLAEConfig, EDLAERecommender
99
from irspack.recommenders.ials import IALSConfig, IALSRecommender
1010
from irspack.recommenders.knn import (
1111
AsymmetricCosineKNNConfig,
@@ -41,6 +41,8 @@
4141
"RP3betaRecommender",
4242
"DenseSLIMConfig",
4343
"DenseSLIMRecommender",
44+
"EDLAERecommender",
45+
"EDLAEConfig",
4446
"SLIMConfig",
4547
"SLIMRecommender",
4648
"IALSConfig",

irspack/recommenders/base.py

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
from abc import ABCMeta, abstractmethod
2-
from typing import TYPE_CHECKING, Any, Dict, Optional, Type, Union, no_type_check
2+
from typing import (
3+
TYPE_CHECKING,
4+
Any,
5+
Dict,
6+
Optional,
7+
Type,
8+
TypeVar,
9+
Union,
10+
no_type_check,
11+
)
312

413
import numpy as np
514
from optuna.trial import Trial
@@ -16,6 +25,8 @@
1625
UserIndexArray,
1726
)
1827

28+
R = TypeVar("R", bound="BaseRecommender")
29+
1930

2031
def _sparse_to_array(U: Any) -> np.ndarray:
2132
if sps.issparse(U):
@@ -78,15 +89,17 @@ def __init__(self, X_train_all: InteractionMatrix, **kwargs: Any) -> None:
7889

7990
@classmethod
8091
def from_config(
81-
cls, X_train_all: InteractionMatrix, config: RecommenderConfig
82-
) -> "BaseRecommender":
92+
cls: Type[R],
93+
X_train_all: InteractionMatrix,
94+
config: RecommenderConfig,
95+
) -> R:
8396
if not isinstance(config, cls.config_class):
8497
raise ValueError(
8598
f"Different config has been given. config must be {cls.config_class}"
8699
)
87100
return cls(X_train_all, **config.dict())
88101

89-
def learn(self) -> "BaseRecommender":
102+
def learn(self: R) -> R:
90103
"""Learns and returns itself.
91104
92105
Returns:
@@ -245,7 +258,7 @@ def get_score_block(self, begin: int, end: int) -> DenseScoreArray:
245258
return _sparse_to_array(self.U[begin:end].dot(self._X_csc))
246259

247260

248-
class BaseRecommenderWithUserEmbedding(BaseRecommender):
261+
class BaseRecommenderWithUserEmbedding:
249262
"""Defines a recommender with user embedding (e.g., matrix factorization.).
250263
These class can be a base CF estimator for CB2CF (with user profile -> user embedding NN).
251264
"""
@@ -276,7 +289,7 @@ def get_score_from_user_embedding(
276289
raise NotImplementedError("get_score_from_item_embedding must be implemtented.")
277290

278291

279-
class BaseRecommenderWithItemEmbedding(BaseRecommender):
292+
class BaseRecommenderWithItemEmbedding:
280293
"""Defines a recommender with item embedding (e.g., matrix factorization.).
281294
These class can be a base CF estimator for CB2CF (with item profile -> item embedding NN).
282295
"""

irspack/recommenders/edlae.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import gc
22

33
import numpy as np
4-
from scipy import linalg, sparse
4+
from scipy import linalg
55

66
from ..definitions import InteractionMatrix
77
from .base import BaseSimilarityRecommender, RecommenderConfig

irspack/recommenders/truncsvd.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import warnings
22
from typing import Optional
33

4-
from numpy import random
54
from sklearn.decomposition import TruncatedSVD
65

76
from ..definitions import (
@@ -11,6 +10,7 @@
1110
UserIndexArray,
1211
)
1312
from .base import (
13+
BaseRecommender,
1414
BaseRecommenderWithItemEmbedding,
1515
BaseRecommenderWithUserEmbedding,
1616
RecommenderConfig,
@@ -23,6 +23,7 @@ class TruncatedSVDConfig(RecommenderConfig):
2323

2424

2525
class TruncatedSVDRecommender(
26+
BaseRecommender,
2627
BaseRecommenderWithUserEmbedding,
2728
BaseRecommenderWithItemEmbedding,
2829
):

irspack/split/userwise.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import numpy as np
44
import pandas as pd
5-
from numpy.lib.arraysetops import unique
65
from scipy import sparse as sps
76

87
from irspack.definitions import InteractionMatrix, OptionalRandomState

pyproject.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,6 @@ include_trailing_comma = true
1313
line_length = 88
1414
multi_line_output = 3
1515
use_parentheses = true
16+
17+
[tool.pycln]
18+
all = true

tests/autopilot/mock_classes.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import pickle
22
import time
3-
from typing import IO, Any, Dict, List, Optional
3+
from typing import IO, Any, Dict, List
44

55
import numpy as np
66
import scipy.sparse as sps

tests/utils/test_utils.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import warnings
2-
31
import numpy as np
42
import pytest
53
import scipy.sparse as sps

0 commit comments

Comments
 (0)