Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Mandera committed Jun 2, 2023
1 parent 5f6804e commit 0891405
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 57 deletions.
2 changes: 1 addition & 1 deletion examples/4how_it_works.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
- When `generalimport` is instantiated it creates a new importer for `sys.meta_path`.
- This importer will return 'fake' modules for matching names and scope.
- The scope ensures only your own imports are faked.
- The fake module will recursively return itself when asked for an attribute.
- The fake module will recursively return a FakeModule instance when asked for an attribute.
- When used in any way (\_\_call\_\_, \_\_add\_\_, \_\_str\_\_ etc) it raises `generalimport.MissingDependencyException`.
- This exception has the 'skip-exceptions' from `unittest` and `pytest` as bases, which means that tests will automatically be skipped.
"""
12 changes: 4 additions & 8 deletions generalimport/__init__.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@

from generalimport.generalimport_bottom import get_installed_modules_names, module_is_installed, import_module, module_name_is_namespace, module_is_namespace, spec_is_namespace, _get_previous_frame_filename, _get_top_name, _get_scope_from_filename, get_spec, fake_module_check
from generalimport.generalimport_bottom import get_installed_modules_names, module_is_installed, import_module, module_name_is_namespace, module_is_namespace, spec_is_namespace, get_spec, fake_module_check
from generalimport.exception import MissingDependencyException, MissingOptionalDependency
from generalimport.fake_module import FakeModule, is_imported
from generalimport.dunders import DynamicDunder, NON_CALLABLE_DUNDERS, CALLABLE_CLASS_DUNDERS, CALLABLE_DUNDERS
from generalimport.fake_module import FakeModule
from generalimport.general_importer import GeneralImporter
from generalimport.import_catcher import ImportCatcher
from generalimport.top import generalimport, get_importer, reset_generalimport





from generalimport.top import generalimport, get_importer, reset_generalimport, is_imported
12 changes: 0 additions & 12 deletions generalimport/fake_module.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from typing import Optional
import sys
import logging
from functools import partialmethod

Expand Down Expand Up @@ -100,14 +99,3 @@ def __getattr__(self, item):



def is_imported(module_name: str) -> bool:
"""
Returns True if the module was actually imported, False, if generalimport mocked it.
"""
module = sys.modules.get(module_name)
try:
return bool(module and not isinstance(module, FakeModule))
except MissingDependencyException as exc:
# isinstance() raises MissingDependencyException: fake module
pass
return False
3 changes: 2 additions & 1 deletion generalimport/general_importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
import sys
from logging import getLogger

from generalimport import FakeModule, spec_is_namespace, _get_top_name, get_spec, fake_module_check, module_is_namespace
from generalimport.fake_module import FakeModule
from generalimport.generalimport_bottom import spec_is_namespace, _get_top_name, get_spec, fake_module_check, module_is_namespace


class GeneralImporter:
Expand Down
1 change: 0 additions & 1 deletion generalimport/generalimport_bottom.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ def fake_module_check(obj, error=True):
else:
return False

# ['__class__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getstate__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'clear', 'f_back', 'f_builtins', 'f_code', 'f_globals', 'f_lasti', 'f_lineno', 'f_locals', 'f_trace', 'f_trace_lines', 'f_trace_opcodes']

def _get_previous_frame_filename(depth):
frame = sys._getframe(depth)
Expand Down
2 changes: 2 additions & 0 deletions generalimport/min.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
""" Standalone minimal example """

import sys
import importlib
from unittest.case import SkipTest
Expand Down
48 changes: 14 additions & 34 deletions generalimport/top.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import sys

from generalimport import GeneralImporter, ImportCatcher
from generalimport import FakeModule, MissingDependencyException
from generalimport.general_importer import GeneralImporter
from generalimport.import_catcher import ImportCatcher


def _assert_no_dots(names):
Expand Down Expand Up @@ -38,36 +40,14 @@ def reset_generalimport():
_clear_importer()



































def is_imported(module_name: str) -> bool:
"""
Returns True if the module was actually imported, False, if generalimport mocked it.
"""
module = sys.modules.get(module_name)
try:
return bool(module and not isinstance(module, FakeModule))
except MissingDependencyException as exc:
# isinstance() raises MissingDependencyException: fake module
pass
return False

0 comments on commit 0891405

Please sign in to comment.