diff --git a/import_tracker/lazy_import_errors.py b/import_tracker/lazy_import_errors.py index 9563cc4..dce5b49 100644 --- a/import_tracker/lazy_import_errors.py +++ b/import_tracker/lazy_import_errors.py @@ -90,7 +90,6 @@ def _make_extras_import_error( # Look through frames in the stack to see if there's an extras module extras_module = None - for frame in inspect.stack(): frame_module = frame.frame.f_globals["__name__"] if frame_module in extras_modules: @@ -153,7 +152,6 @@ def __new__( ): # When this is used as a base class, we need to pass __classcell__ # through to type.__new__ to avoid a runtime warning. - new_namespace = {} if isinstance(namespace, dict) and "__classcell__" in namespace: new_namespace["__classcell__"] = namespace.get("__classcell__") @@ -243,8 +241,10 @@ def __delete__(self, *_, **__): def __delitem__(self, *_, **__): self._raise() - def __eq__(self, *_, **__): - self._raise() + def __eq__(self, other, *_, **__): + if not _is_import_time(): + self._raise() + return id(self) == id(other) def __float__(self, *_, **__): self._raise() @@ -265,7 +265,9 @@ def __gt__(self, *_, **__): self._raise() def __hash__(self, *_, **__): - self._raise() + if not _is_import_time(): + self._raise() + return id(self) def __iadd__(self, *_, **__): self._raise() diff --git a/test/sample_libs/type_check_deps/__init__.py b/test/sample_libs/type_check_deps/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/test/sample_libs/type_check_deps/type_check_dict.py b/test/sample_libs/type_check_deps/type_check_dict.py new file mode 100644 index 0000000..4050273 --- /dev/null +++ b/test/sample_libs/type_check_deps/type_check_dict.py @@ -0,0 +1,12 @@ +# Standard +from typing import Dict + +# Local +import import_tracker + +with import_tracker.lazy_import_errors(): + # Third Party + from foo.bar import Bar + + def dummy_type_func(var) -> Dict[str, Bar]: + pass diff --git a/test/sample_libs/type_check_deps/type_check_union.py b/test/sample_libs/type_check_deps/type_check_union.py new file mode 100644 index 0000000..75bcdb6 --- /dev/null +++ b/test/sample_libs/type_check_deps/type_check_union.py @@ -0,0 +1,12 @@ +# Standard +from typing import Union + +# Local +import import_tracker + +with import_tracker.lazy_import_errors(): + # Third Party + from foo import Bar + + def dummy_type_func(var) -> Union[str, Bar]: + pass diff --git a/test/test_lazy_import_errors.py b/test/test_lazy_import_errors.py index 9cc4a30..c676582 100644 --- a/test/test_lazy_import_errors.py +++ b/test/test_lazy_import_errors.py @@ -433,3 +433,21 @@ def test_lazy_import_error_import_time_dep(): # calls out to a optional dependency via decorator (hence import time) # Third Party from decorator_deps import opt_decorator + + +def test_lazy_import_error_type_dict(): + """Test lazy import error for the case where the call to optional + dependency happens because of type check for Dict + """ + + # Third Party + from type_check_deps import type_check_dict + + +def test_lazy_import_error_type_union(): + """Test lazy import error for the case where the call to optional + dependency happens because of type check for Union + """ + + # Third Party + from type_check_deps import type_check_union