Metaculus / Metaculus/forecasting-tools

Improve exports of __init__.py

Open
#111 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Low Priority
Dominant language
Python
Stars
79
Forks
38
Avg merge
20m
Merged PRs (30d)
12

Description

Make it so that all of forecasting-tools is not imported all at once (or at least make the imports styled like normal packages). Right now the init.py imports everything at once on package load.

See discussion here

On a side note, I have one particular implementation objection to using Forecasting Tools, and that is how the initialization works.
It seems like when you import Forecasting Tools, it initializes all of its modules. This means if you try and load one thing, you have to "dump out the whole box" as it were. It's pretty easily fixable: https://pypi.org/project/pep562/
in short: you can expose submodules at the package top level without importing them before their first access using an override of the initialization's __getattr__ funcitonality.
(in __init__.py )
import importlib

__all__ = ["mod1", "mod2"]  # for * imports and tooling

# Optional: map names you want to expose lazily
_lazy = {"mod1": ".mod1", "mod2": ".mod2"}

def __getattr__(name):
    if name in _lazy:
        module = importlib.import_module(_lazy[name], __name__)
        globals()[name] = module        # cache so we don't import again
        return module
    raise AttributeError(name)

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the package init.py and inspect how importing forecasting-tools currently loads its modules. Use the linked PEP 562 discussion as guidance for the intended lazy-access behavior. Done means the package no longer eagerly imports everything while its exposed submodules remain accessible through the package.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
developer-experience
Issue type
Refactor
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.