Skip to content

Commit

Permalink
Removed nmslib installation on Win Python3.9 (#36)
Browse files Browse the repository at this point in the history
* removed nmslib installation on Win Python3.9

* fixes for int32 problem

* nmslib moved to extras, reversed type specifications

* added  message for nmslib ImportError

* changed ImportError description

* refactored nmslib ImportError handling
  • Loading branch information
yukeeul authored May 21, 2023
1 parent bb5ae2a commit eee3ba5
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 7 deletions.
5 changes: 3 additions & 2 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -55,20 +55,21 @@ pandas = ">=0.25.3, <2.0.0"
scipy = "^1.5.4"
tqdm = "^4.27.0"
implicit = "0.4.4"
nmslib = "^2.0.4"
attrs = ">=19.1.0,<22.0.0"
typeguard = "^2.0.1"
lightfm = "^1.16"

# To avoid problems with resolving dependencies
Markdown = "~3.2"

nmslib = {version = "^2.0.4", optional = true}
torch = {version = "^1.6", optional = true}
pytorch-lightning = {version = "^1.6", optional = true}

[tool.poetry.extras]
nmslib = ["nmslib"]
nn = ["torch", "pytorch-lightning"]
all = ["torch", "pytorch-lightning"]
all = ["nmslib", "torch", "pytorch-lightning"]

[tool.poetry.dev-dependencies]
black = "22.3.0"
Expand Down
20 changes: 20 additions & 0 deletions rectools/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,25 @@ def __new__(cls, *args: tp.Any, **kwargs: tp.Any) -> tp.Any:
)


class NmslibModelUnavailable:
"""Dummy class the instance of which is returned in case a model provided lacks any libraries required"""

def __new__(cls, *args: tp.Any, **kwargs: tp.Any) -> tp.Any:
"""Raise ImportError when an attempt to instantiate an unavailable model is made"""
raise ImportError(
f"Cannot initialize {cls.__name__}: "
f"run `pip install rectools[nmslib]` to install extra requirements before accessing {cls.__name__} "
f"(see `extras/requirements-nn.txt)"
)


class DSSMModel(NNModelUnavailable):
"""Dummy class the instance of which is returned in case DSSMModel lacks any libraries required"""


class ItemToItemAnnRecommender(NmslibModelUnavailable):
"""Dummy class the instance of which is returned in case ItemToItemAnnRecommender lacks any libraries required"""


class UserToItemAnnRecommender(NmslibModelUnavailable):
"""Dummy class the instance of which is returned in case UserToItemAnnRecommender lacks any libraries required"""
5 changes: 4 additions & 1 deletion rectools/tools/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@
`tools.UserToItemAnnRecommender`
"""

from .ann import ItemToItemAnnRecommender, UserToItemAnnRecommender
try:
from .ann import ItemToItemAnnRecommender, UserToItemAnnRecommender
except ImportError: # pragma: no cover
from ..compat import ItemToItemAnnRecommender, UserToItemAnnRecommender # type: ignore

__all__ = (
"ItemToItemAnnRecommender",
Expand Down
4 changes: 2 additions & 2 deletions tests/test_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@

import pytest

from rectools.compat import DSSMModel
from rectools.compat import DSSMModel, ItemToItemAnnRecommender, UserToItemAnnRecommender


@pytest.mark.parametrize(
"model",
(DSSMModel,),
(DSSMModel, ItemToItemAnnRecommender, UserToItemAnnRecommender),
)
def test_raise_when_model_not_available(
model: tp.Any,
Expand Down

0 comments on commit eee3ba5

Please sign in to comment.