Skip to content

perf: lazy loading - #59

Merged
jkowalleck merged 11 commits into
mainfrom
feat/lazy-loading-improv
Jul 21, 2026
Merged

jkowalleck merged 11 commits into
mainfrom
feat/lazy-loading-improv

Conversation

@jkowalleck

Copy link
Copy Markdown
Collaborator

fixed lazy loading from main module

Signed-off-by: Jan Kowalleck <jan.kowalleck@gmail.com>

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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__.py with explicit lazy attribute forwarding via __getattr__/__dir__.
  • Removed __all__ from syntax_helpers.py and simplified its lazy attribute handling for grammar and syntax_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 changes from 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.

Comment thread src/rfc3987_syntax2/syntax_helpers.py Outdated
Comment thread src/rfc3987_syntax2/__init__.py
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>

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

Comment thread src/rfc3987_syntax2/syntax_helpers.py Outdated
Comment thread src/rfc3987_syntax2/__init__.py
Signed-off-by: Jan Kowalleck <jan.kowalleck@gmail.com>

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

Comment thread src/rfc3987_syntax2/__init__.py
Comment thread src/rfc3987_syntax2/__init__.py
Comment thread src/rfc3987_syntax2/syntax_helpers.py Outdated
Signed-off-by: Jan Kowalleck <jan.kowalleck@gmail.com>

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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 as syntax_helpers.__all__. External code mutating rfc3987_syntax2.__all__ would also mutate syntax_helpers.__all__ (and vice versa), which can lead to surprising export behavior. Consider copying __all__ locally and deriving _EXPORTS_SH from that copy.
__all__ = _syntax_helpers.__all__

_EXPORTS_SH = frozenset(_syntax_helpers.__all__)

Signed-off-by: Jan Kowalleck <jan.kowalleck@gmail.com>

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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 conditional global assignments 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 using globals()[name] = value with 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_syntax2 does not populate grammar / syntax_parser in the module __dict__ before first access. Without this, a future refactor could reintroduce eager initialization while keeping the current API surface intact (e.g., via from .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}")

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

Comment thread tests/test_lazy_attrs.py
Signed-off-by: Jan Kowalleck <jan.kowalleck@gmail.com>
@jkowalleck
jkowalleck merged commit 0fce37d into main Jul 21, 2026
22 checks passed
@jkowalleck
jkowalleck deleted the feat/lazy-loading-improv branch July 21, 2026 14:09
@jkowalleck

Copy link
Copy Markdown
Collaborator Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants