Skip to main content

Handle all your optional dependencies with a single call!

Project description

generalimport

Handle all your optional dependencies with a single call!

Contents

generalimport
├─ Dependency Diagram
├─ Installation showing dependencies
├─ Information
├─ Examples
│  ├─ Minimal Example
│  ├─ Tests Showcase
│  ├─ Recommended Installation
│  └─ How It Works
├─ Attributes
├─ Contributions
└─ Todo

Dependency Diagram

flowchart LR
2([file]) --> 4([packager])
1([library]) --> 3([vector])
1([library]) --> 4([packager])
0([import]) --> 1([library])
1([library]) --> 2([file])
click 0 "https://github.com/ManderaGeneral/generalimport"
click 1 "https://github.com/ManderaGeneral/generallibrary"
click 2 "https://github.com/ManderaGeneral/generalfile"
click 3 "https://github.com/ManderaGeneral/generalvector"
click 4 "https://github.com/ManderaGeneral/generalpackager"
style 0 fill:#482

Installation showing dependencies

pip install generalimport
No dependencies ✔️

Information

Package Ver Latest Release Python Platform Cover
generalimport 0.2.1 2022-10-07 16:07 CEST 3.8, 3.9, 3.10 Windows, Ubuntu 97.6 %

Examples

Minimal Example

Call generalimport before importing any optional dependencies.

Here is a minimal example:

from generalimport import generalimport
generalimport("notinstalled")

import notinstalled  # No error

def func():
    notinstalled.missing_func()  # Error occurs here

func()
MissingOptionalDependency: Optional dependency 'notinstalled' was used but it isn't installed.

Imports fail when they are used, not imported.

This means you don't need to keep checking if the package is installed before importing it. Simply import your optional package and use it like you would any package and let it fail wherever it fails, with a nice error message.

Tests Showcase

The beauty of this package is that the error raised isn't just any exception. It has two base classes: unittest.case.SkipTest and _pytest.outcomes.Skipped (If available).

This means that if a test method uses an uninstalled optional package then that test is automatically skipped. This means no more manual skip decorators for optional dependencies!

from generalimport import generalimport
generalimport("optional_uninstalled_package")

from optional_uninstalled_package import missing_func

from unittest import TestCase

class MyTest(TestCase):
    def test_missing_func(self):
        self.assertEqual(3, missing_func(1, 2))
Ran 1 test in 0.002s

OK (skipped=1)

Skipped: Optional dependency 'optional_uninstalled_package' was used but it isn't installed.

Recommended Installation

I recommend to put this at the top of your main __init__.py file.

from generalimport import generalimport
generalimport("your", "optional", "dependencies")

This is all you need to write to use this package.

You can also write generalimport("*") to make any package importable.

How It Works

  • When generalimport is instantiated it creates a new importer for sys.meta_path.
  • This importer will return 'fake' modules for specified names.
  • The fake module will recursively return itself when asked for an attribute.
  • When used in any way (__call__, __add__, __str__ etc) it raises generalimport.MissingOptionalDependency.
  • This exception has the 'skip-exceptions' from unittest and pytest as bases, which means that tests will automatically be skipped.

Attributes

Module: generalimport
├─ Class: FakeModule
│  └─ Method: error_func
├─ Class: GeneralImporter
│  ├─ Method: add_names (Untested)
│  ├─ Method: disable
│  ├─ Method: enable (Untested)
│  ├─ Method: find_module
│  ├─ Method: is_enabled (Untested)
│  ├─ Method: load_module
│  └─ Method: remove_names
├─ Class: MissingOptionalDependency
├─ Function: check_import
├─ Function: generalimport
├─ Function: get_importer
├─ Function: get_installed_modules_names
├─ Function: import_module
├─ Function: module_is_installed
├─ Function: module_is_namespace (Untested)
├─ Function: module_name_is_namespace
└─ Function: spec_is_namespace (Untested)

Contributions

Issue-creation and discussions are most welcome!

Pull requests are not wanted, please discuss with me before investing any time

Todo

Module Message
module_funcs.py Change back to find_spec if spec_is_namespace works.
Generated 2022-10-07 16:07 CEST for commit ba1b580.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

generalimport-0.2.1.tar.gz (13.9 kB view hashes)

Uploaded Source

Built Distribution

generalimport-0.2.1-py3-none-any.whl (20.3 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page