-
Notifications
You must be signed in to change notification settings - Fork 45
Description
Description
When using strict static type checking (like strict = true in Mypy or strict mode in Pyright/Pylance), importing core classes directly from pyrate_limiter throws attribute errors.
Specifically, this import:
from pyrate_limiter import Duration, Limiter, Rate
Results in errors like:
Module "pyrate_limiter" has no attribute "Duration"
Module "pyrate_limiter" has no attribute "Rate"
Cause
This occurs because strict type checkers enforce rules against "implicit re-exports." While Python executes the __init__.py file perfectly at runtime, static analyzers treat the imported classes as private, internal variables of the module unless they are explicitly exposed.
Proposed Solution
This can be resolved without changing any functional code by explicitly re-exporting the public API in the root __init__.py.
This can be done using the __all__ dunder method:
# __init__.py
from .limit import Limiter
from .rate import Rate, Duration
__all__ = ["Limiter", "Rate", "Duration"]Or by using explicit aliases (as):
# __init__.py
from .limit import Limiter as Limiter
from .rate import Rate as Rate, Duration as DurationEnvironment
- Python Version: 3.13
- pyrate-limiter Version: 4.0.2
- IDE: VS Code (Version: 1.109.4)
- Type Checkers: Mypy (strict), Pyright (strict)
Currently, downstream users must either add # type: ignore comments to their imports or globally weaken their type-checking configurations (e.g., implicit_reexport = true). Explicitly exporting these in the library would allow for seamless strict type checking out of the box!