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

[Splink 4] Move key user-facing classes/functions to __init__.py? #2040

Open
RobinL opened this issue Mar 8, 2024 · 1 comment
Open

[Splink 4] Move key user-facing classes/functions to __init__.py? #2040

RobinL opened this issue Mar 8, 2024 · 1 comment

Comments

@RobinL
Copy link
Member

RobinL commented Mar 8, 2024

If __init__.py contains:

__version__ = "4.0.0.dev2"

from splink.blocking_rule_library import block_on
from splink.datasets import splink_datasets
from splink.linker import Linker
from splink.settings_creator import SettingsCreator

from splink.sqlite.database_api import SQLiteAPI

try:
    from splink.duckdb.database_api import DuckDBAPI
except ImportError:
    pass

try:
    from splink.postgres.database_api import PostgresAPI
except ImportError:
    pass

try:
    from splink.spark.database_api import SparkAPI
except ImportError:
    pass

We could then write

from splink import DuckDBAPI, Linker, SettingsCreator, block_on, splink_datasets
@RobinL
Copy link
Member Author

RobinL commented Mar 8, 2024

You can do:

def __getattr__(name):
    if name == "SparkAPI":
        try:
            from splink.spark.database_api import SparkAPI

            return SparkAPI
        except ImportError:
            raise ImportError(
                "SparkAPI cannot be imported because its dependencies are not "
                "installed. Please `pip install pyspark`."
            )
    raise AttributeError(f"module 'splink' has no attribute '{name}'")

If you want a custom error message that is generated at the point the user tries to from splink import SparkAPI

Otherwise the user would just get ImportError: cannot import name 'SparkAPI' from 'splink'

This was referenced Mar 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant