Skip to content

Commit 5eaa67f

Browse files
authored
✨ Feature: disable lazy loading with env flag (#262)
1 parent 321ce2d commit 5eaa67f

File tree

19 files changed

+68
-17
lines changed

19 files changed

+68
-17
lines changed

codegen/templates/latest/models.py.jinja

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44

55
from typing import TYPE_CHECKING
66

7-
if TYPE_CHECKING:
7+
from githubkit.lazy_module import is_lazy_disabled
8+
9+
if TYPE_CHECKING or is_lazy_disabled():
810
{% for name in model_names %}
911
from {{ output_module }}.{{ latest_version_module }}.models import {{ name }} as {{ name }}
1012
{% endfor %}

codegen/templates/latest/types.py.jinja

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44

55
from typing import TYPE_CHECKING
66

7-
if TYPE_CHECKING:
7+
from githubkit.lazy_module import is_lazy_disabled
8+
9+
if TYPE_CHECKING or is_lazy_disabled():
810
{% for name in model_names %}
911
from {{ output_module }}.{{ latest_version_module }}.types import {{ name }}Type as {{ name }}Type
1012
{% endfor %}

codegen/templates/latest/webhooks.py.jinja

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44

55
from typing import TYPE_CHECKING
66

7-
if TYPE_CHECKING:
7+
from githubkit.lazy_module import is_lazy_disabled
8+
9+
if TYPE_CHECKING or is_lazy_disabled():
810
{% for name in event_names %}
911
from {{ output_module }}.{{ latest_version_module }}.webhooks import {{ pascal_case(name) }}Event as {{ pascal_case(name) }}Event
1012
from {{ output_module }}.{{ latest_version_module }}.webhooks import {{ name }}_action_types as {{ name }}_action_types

codegen/templates/models/models.py.jinja

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44

55
from typing import TYPE_CHECKING
66

7-
if TYPE_CHECKING:
7+
from githubkit.lazy_module import is_lazy_disabled
8+
9+
if TYPE_CHECKING or is_lazy_disabled():
810
{% for group in groups %}
911
{% for model in group.models %}
1012
from .{{ group_name(group, groups) }} import {{ model.class_name }} as {{ model.class_name }}

codegen/templates/models/types.py.jinja

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44

55
from typing import TYPE_CHECKING
66

7-
if TYPE_CHECKING:
7+
from githubkit.lazy_module import is_lazy_disabled
8+
9+
if TYPE_CHECKING or is_lazy_disabled():
810
{% for group in groups %}
911
{% for model in group.models %}
1012
from .{{ group_name(group, groups) }} import {{ model.get_param_type_string() }} as {{ model.get_param_type_string() }}

codegen/templates/webhooks/__init__.py.jinja

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44

55
from typing import TYPE_CHECKING
66

7-
if TYPE_CHECKING:
7+
from githubkit.lazy_module import is_lazy_disabled
8+
9+
if TYPE_CHECKING or is_lazy_disabled():
810
{% for name in event_names %}
911
from .{{ name }} import {{ pascal_case(name) }}Event as {{ pascal_case(name) }}Event
1012
from .{{ name }} import {{ name }}_action_types as {{ name }}_action_types

githubkit/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from . import lazy_module
22

3-
lazy_module.apply()
3+
if not lazy_module.is_lazy_disabled():
4+
lazy_module.apply()
45

56
from .auth import ActionAuthStrategy as ActionAuthStrategy
67
from .auth import AppAuthStrategy as AppAuthStrategy

githubkit/lazy_module.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,14 @@
33
from importlib.abc import Loader
44
from importlib.machinery import ModuleSpec, PathFinder, SourceFileLoader
55
from itertools import chain
6+
import os
67
import re
78
import sys
89
from types import ModuleType
910
from typing import Any, Optional
1011

12+
GITHUBKIT_LAZY_DISABLE_FLAG = "GITHUBKIT_LAZY_DISABLE_FLAG"
13+
1114
LAZY_MODULES = (
1215
r"^githubkit\.rest$",
1316
r"^githubkit\.versions\.[^.]+\.models$",
@@ -142,5 +145,9 @@ def find_spec(
142145
return module_spec
143146

144147

148+
def is_lazy_disabled() -> bool:
149+
return bool(os.getenv(GITHUBKIT_LAZY_DISABLE_FLAG))
150+
151+
145152
def apply():
146153
sys.meta_path.insert(0, LazyModuleFinder())

githubkit/rest/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99

1010
from typing import TYPE_CHECKING
1111

12-
if TYPE_CHECKING:
12+
from githubkit.lazy_module import is_lazy_disabled
13+
14+
if TYPE_CHECKING or is_lazy_disabled():
1315
from githubkit.versions.v2022_11_28.models import (
1416
ActionsArtifactAndLogRetention as ActionsArtifactAndLogRetention,
1517
)

githubkit/versions/ghec_v2022_11_28/models/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99

1010
from typing import TYPE_CHECKING
1111

12-
if TYPE_CHECKING:
12+
from githubkit.lazy_module import is_lazy_disabled
13+
14+
if TYPE_CHECKING or is_lazy_disabled():
1315
from .group_0000 import Root as Root
1416
from .group_0001 import CvssSeverities as CvssSeverities
1517
from .group_0001 import CvssSeveritiesPropCvssV3 as CvssSeveritiesPropCvssV3

0 commit comments

Comments
 (0)