Handle all your optional dependencies with a single call!
Project description
generalimport
Handle all your optional dependencies with a single call!
Table of Contents
generalimport ├─ Dependency Diagram for ManderaGeneral ├─ Installation showing dependencies ├─ Information ├─ Examples │ ├─ Minimal Example │ ├─ Tests Showcase │ ├─ Recommended Setup │ └─ How It Works ├─ Attributes └─ Contributions
Dependency Diagram for ManderaGeneral
flowchart LR
0([import]) --> 3([file])
0([import]) --> 2([library])
1([tool]) --> 2([library])
3([file]) --> 4([packager])
2([library]) --> 4([packager])
2([library]) --> 3([file])
click 0 "https://github.com/ManderaGeneral/generalimport"
click 1 "https://github.com/ManderaGeneral/generaltool"
click 2 "https://github.com/ManderaGeneral/generallibrary"
click 3 "https://github.com/ManderaGeneral/generalfile"
click 4 "https://github.com/ManderaGeneral/generalpackager"
style 0 stroke:#482
Installation showing dependencies
pip install |
generalimport |
---|---|
No dependencies | ✔️ |
Information
Package | Ver | Latest Release | Python | Platform | Cover |
---|---|---|---|---|---|
generalimport | 0.5.2 | 2024-01-24 07:20 CET | 3.8, 3.9, 3.10, 3.11, 3.12 | Windows, Ubuntu | 97.2 % |
Examples
Minimal Example
Call generalimport
before importing any optional dependencies.
from generalimport import generalimport
generalimport("notinstalled")
from notinstalled import missing_func # No error
missing_func() # Error occurs here
MissingDependencyException: 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 Setup
Put this in your __init__.py
file to affect all imports inside the folder __init__.py
resides in.
from generalimport import generalimport
generalimport("your", "optional", "dependencies")
generalimport("*")
makes it handle all names (If missing of course)
:warning: generalimport("*")._scope = None
disables the scope
- Makes it handle missing imports anywhere
- For example it will override
pandas
internal custom optional dependency handling
How It Works
- When
generalimport
is instantiated it creates a new importer forsys.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 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
andpytest
as bases, which means that tests will automatically be skipped.
Attributes
Module: generalimport ├─ Class: DynamicDunder │ ├─ Method: result │ └─ Method: trigger ├─ Class: FakeModule │ ├─ Method: error_func │ └─ Method: error_func_class ├─ Class: GeneralImporter │ ├─ Method: catch │ ├─ Method: create_module │ ├─ Method: exec_module │ └─ Method: find_spec ├─ Class: ImportCatcher │ └─ Method: handle ├─ Class: MissingDependencyException ├─ Class: MissingOptionalDependency ├─ Function: fake_module_check ├─ Function: generalimport ├─ Function: get_importer ├─ Function: get_installed_modules_names ├─ Function: get_spec ├─ Function: import_module ├─ Function: is_imported ├─ Function: module_is_installed ├─ Function: module_is_namespace ├─ Function: module_name_is_namespace ├─ Function: reset_generalimport └─ Function: spec_is_namespace
Contributions
Issue-creation, discussions and pull requests are most welcome!
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
Built Distribution
File details
Details for the file generalimport-0.5.2.tar.gz
.
File metadata
- Download URL: generalimport-0.5.2.tar.gz
- Upload date:
- Size: 20.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.12.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 083f5e8feeada8a23cf3917edf299e71e71f2c7d2bd29e1795e11eeb759d7c62 |
|
MD5 | fff013945029980e428fa78efd72d942 |
|
BLAKE2b-256 | 7b01e036ec9b3bf234ce275d3f11287a2670da5fc3a3268c93403b59d7e547df |
File details
Details for the file generalimport-0.5.2-py3-none-any.whl
.
File metadata
- Download URL: generalimport-0.5.2-py3-none-any.whl
- Upload date:
- Size: 28.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.12.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ce9082be1e6cd48ef108fede3b13ccc308d6aff2b6f6d65f52310521e8332fcd |
|
MD5 | 883450c5142df1137a73e9a5d4892c66 |
|
BLAKE2b-256 | 68485586c61dee513068e72732fa9f5899289503a566e766bf99db9bb0f4589e |