Repository navigation
perf: lazy loading - #59
Conversation
Signed-off-by: Jan Kowalleck <jan.kowalleck@gmail.com>
There was a problem hiding this comment.
Pull request overview
This PR adjusts the package’s public exports to preserve lazy-loading behavior when importing the top-level rfc3987_syntax2 module, avoiding eager initialization work from the main entrypoint.
Changes:
- Replaced
from .syntax_helpers import *in the package__init__.pywith explicit lazy attribute forwarding via__getattr__/__dir__. - Removed
__all__fromsyntax_helpers.pyand simplified its lazy attribute handling forgrammarandsyntax_parser.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| src/rfc3987_syntax2/syntax_helpers.py | Adjusts lazy attribute plumbing and removes __all__, impacting export/star-import behavior. |
| src/rfc3987_syntax2/init.py | Implements package-level lazy exports to prevent eager loading from the main module. |
Comments suppressed due to low confidence (1)
src/rfc3987_syntax2/syntax_helpers.py:275
__all__was removed from this module. That changesfrom rfc3987_syntax2.syntax_helpers import *to export a large number of unintended names (e.g.,Path,Lock,Lark,warn,load_grammar, etc.), which is a breaking API/namespace change and can leak implementation details.
Consider restoring a constrained __all__ (can be derived from RFC3987_SYNTAX_TERMS) so star-import behavior remains stable while keeping the lazy attributes implementation unchanged.
if name == 'grammar':
return _get_grammar()
if name == 'syntax_parser':
return _get_syntax_parser()
raise AttributeError(f"module {__name__!r} failed to implement attribute {name!r}")
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Signed-off-by: Jan Kowalleck <jan.kowalleck@gmail.com>
Signed-off-by: Jan Kowalleck <jan.kowalleck@gmail.com>
Signed-off-by: Jan Kowalleck <jan.kowalleck@gmail.com>
Signed-off-by: Jan Kowalleck <jan.kowalleck@gmail.com>
Signed-off-by: Jan Kowalleck <jan.kowalleck@gmail.com>
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 2 out of 2 changed files in this pull request and generated no new comments.
Comments suppressed due to low confidence (1)
src/rfc3987_syntax2/init.py:12
__all__is currently assigned to the same mutable list object assyntax_helpers.__all__. External code mutatingrfc3987_syntax2.__all__would also mutatesyntax_helpers.__all__(and vice versa), which can lead to surprising export behavior. Consider copying__all__locally and deriving_EXPORTS_SHfrom that copy.
__all__ = _syntax_helpers.__all__
_EXPORTS_SH = frozenset(_syntax_helpers.__all__)
Signed-off-by: Jan Kowalleck <jan.kowalleck@gmail.com>
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 2 out of 2 changed files in this pull request and generated no new comments.
Comments suppressed due to low confidence (2)
src/rfc3987_syntax2/syntax_helpers.py:326
__getattr__uses conditionalglobalassignments and single-quoted attribute names, which is inconsistent with the rest of this module (e.g.,make_syntax_validator("iri")at syntax_helpers.py:188) and makes the lazy-cache logic harder to follow. Consider usingglobals()[name] = valuewith a single cache path and double-quoted names for consistency.
def __getattr__(name: str) -> Any:
if name == 'grammar':
global grammar
grammar = _get_grammar()
return grammar
src/rfc3987_syntax2/init.py:19
- There’s still no regression test asserting that
import rfc3987_syntax2does not populategrammar/syntax_parserin the module__dict__before first access. Without this, a future refactor could reintroduce eager initialization while keeping the current API surface intact (e.g., viafrom .syntax_helpers import *).
def __getattr__(name: str) -> Any:
if name in _EXPORTS_SH:
value = getattr(_syntax_helpers, name)
globals()[name] = value
return value
raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
Signed-off-by: Jan Kowalleck <jan.kowalleck@gmail.com>
fixed lazy loading from main module