Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 52 additions & 5 deletions pyunitwizard/__init__.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
"""
PyUnitWizard
Quantities and units assistant
"""
"""PyUnitWizard
Quantities and units assistant."""

from __future__ import annotations

import sys
import types
import warnings

# versioningit
from ._version import __version__

def __print_version__():

def __print_version__() -> None:
print("PyUnitWizard version " + __version__)


from . import api as _api

# Add imports here
from .api import (
are_close,
Expand Down Expand Up @@ -39,6 +46,46 @@ def __print_version__():
from . import constants
from . import utils

__all__ = list(_api.__all__)


def _create_main_compat_module() -> types.ModuleType:
module = types.ModuleType(
"pyunitwizard.main",
"""Compatibility module for historic imports.

Deprecated in favour of importing from :mod:`pyunitwizard` directly.
""".strip(),
)
warned = False

def _emit_warning() -> None:
nonlocal warned
if not warned:
warnings.warn(
"Importing from 'pyunitwizard.main' is deprecated; import from 'pyunitwizard' instead.",
DeprecationWarning,
stacklevel=3,
)
warned = True

def __getattr__(name: str) -> object:
if name in _api.__all__:
_emit_warning()
return getattr(_api, name)
raise AttributeError(f"module 'pyunitwizard.main' has no attribute {name!r}")

def __dir__() -> list[str]:
return sorted(__all__)

module.__getattr__ = __getattr__ # type: ignore[assignment]
module.__dir__ = __dir__ # type: ignore[assignment]
module.__all__ = tuple(__all__)
return module


sys.modules.setdefault("pyunitwizard.main", _create_main_compat_module())

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Attach compatibility shim to package namespace

The new shim registers a pyunitwizard.main module with sys.modules.setdefault but never binds that module to the pyunitwizard package object. Because the entry already exists in sys.modules, import pyunitwizard.main completes without adding an attribute, so subsequent access still raises AttributeError. This breaks code that previously did import pyunitwizard.main followed by pyunitwizard.main.some_api. After creating the module you need to assign it to globals()["main"] (or otherwise expose it via the package) to retain the pre‑refactor behavior【F:pyunitwizard/init.py†L87-L87】

Useful? React with 👍 / 👎.


_kernel.initialize()

try:
Expand Down
47 changes: 0 additions & 47 deletions pyunitwizard/main.py

This file was deleted.

6 changes: 5 additions & 1 deletion pyunitwizard/utils/sequences/is_sequence.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
from ...main import is_quantity
"""Helpers to determine whether objects behave like sequences."""

from __future__ import annotations

from ... import is_quantity
import numpy as np

def is_sequence(item):
Expand Down