Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add fix for typing #65

Merged
merged 4 commits into from
Aug 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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():
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should return a bool if _is_import_time is true

Copy link
Member Author

@gkumbhat gkumbhat Aug 12, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yes, good point! Like the function __eq__ would need to return bool?

I guess currently it would return None which would be falsey, so may be we should return False if _is_import_time is True?

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