Skip to content

Commit c22b4b0

Browse files
committed
Add support for 'Self' type in type hints for compatibility with Python 3.10+
1 parent 91d1f58 commit c22b4b0

6 files changed

Lines changed: 54 additions & 6 deletions

File tree

rtrec/models/base.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,15 @@
22
import pickle
33
from abc import ABC, abstractmethod
44
from io import BytesIO
5-
from typing import Any, Iterable, List, Optional, Self, Tuple, Union
5+
from typing import Any, Iterable, List, Optional, Tuple, Union, TYPE_CHECKING
6+
7+
if TYPE_CHECKING: # static analyzers
8+
from typing import Self
9+
else: # runtime on 3.10
10+
try:
11+
from typing import Self # 3.11+
12+
except ImportError:
13+
from typing_extensions import Self
614

715
from rtrec.utils.features import FeatureStore
816
from rtrec.utils.identifiers import Identifier

rtrec/models/hybrid.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
import logging
22
from collections import defaultdict
3-
from typing import Any, Dict, Iterable, List, Optional, Self, Tuple, override
3+
from typing import Any, Dict, Iterable, List, Optional, Tuple, override, TYPE_CHECKING
4+
5+
if TYPE_CHECKING: # static analyzers
6+
from typing import Self
7+
else: # runtime on 3.10
8+
try:
9+
from typing import Self # 3.11+
10+
except ImportError:
11+
from typing_extensions import Self
412

513
import implicit.cpu.topk as implicit
614
import numpy as np

rtrec/models/internal/slim_elastic.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,15 @@
33
import warnings
44
from functools import partial
55
from multiprocessing import Pool, shared_memory
6-
from typing import Any, Dict, List, Optional, Self, Tuple
6+
from typing import Any, Dict, List, Optional, Tuple, TYPE_CHECKING
7+
8+
if TYPE_CHECKING: # static analyzers
9+
from typing import Self
10+
else: # runtime on 3.10
11+
try:
12+
from typing import Self # 3.11+
13+
except ImportError:
14+
from typing_extensions import Self
715

816
import numpy as np
917
import scipy.sparse as sp

rtrec/models/lightfm.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
import logging
2-
from typing import Any, Iterable, List, Optional, Self, Tuple, override
2+
from typing import Any, Iterable, List, Optional, Tuple, override, TYPE_CHECKING
3+
4+
if TYPE_CHECKING: # static analyzers
5+
from typing import Self
6+
else: # runtime on 3.10
7+
try:
8+
from typing import Self # 3.11+
9+
except ImportError:
10+
from typing_extensions import Self
311

412
import implicit.cpu.topk as implicit
513
import numpy as np

rtrec/models/slim.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
import logging
2-
from typing import Any, Iterable, List, Optional, Self, Tuple, override
2+
from typing import Any, Iterable, List, Optional, Tuple, override, TYPE_CHECKING
3+
4+
if TYPE_CHECKING: # static analyzers
5+
from typing import Self
6+
else: # runtime on 3.10
7+
try:
8+
from typing import Self # 3.11+
9+
except ImportError:
10+
from typing_extensions import Self
311

412
from ..models.internal.slim_elastic import SLIMElastic
513
from .base import BaseModel

rtrec/recommender.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
import math
22
import time
3-
from typing import Any, Dict, Iterable, Iterator, List, Optional, Self, Tuple
3+
from typing import Any, Dict, Iterable, Iterator, List, Optional, Tuple, TYPE_CHECKING
4+
5+
if TYPE_CHECKING: # static analyzers
6+
from typing import Self
7+
else: # runtime on 3.10
8+
try:
9+
from typing import Self # 3.11+
10+
except ImportError:
11+
from typing_extensions import Self
412

513
import pandas as pd
614
from tqdm import tqdm

0 commit comments

Comments
 (0)