Skip to content

Commit

Permalink
Merge pull request #65 from gkumbhat/add_fix_for_typing
Browse files Browse the repository at this point in the history
Add fix for typing
  • Loading branch information
gabe-l-hart authored Aug 12, 2022
2 parents 1f5cc1c + 192f78e commit 36af643
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 5 deletions.
12 changes: 7 additions & 5 deletions import_tracker/lazy_import_errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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__")
Expand Down Expand Up @@ -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()
Expand All @@ -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()
Expand Down
Empty file.
12 changes: 12 additions & 0 deletions test/sample_libs/type_check_deps/type_check_dict.py
Original file line number Diff line number Diff line change
@@ -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
12 changes: 12 additions & 0 deletions test/sample_libs/type_check_deps/type_check_union.py
Original file line number Diff line number Diff line change
@@ -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
18 changes: 18 additions & 0 deletions test/test_lazy_import_errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 36af643

Please sign in to comment.