Skip to content

Commit a81980f

Browse files
committed
feat: add factory support
1 parent fe8ca93 commit a81980f

File tree

2 files changed

+14
-19
lines changed

2 files changed

+14
-19
lines changed

src/fastapi_cli/cli.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,13 +137,14 @@ def _run(
137137
toolkit.print(root_tree, tag="module")
138138
toolkit.print_line()
139139

140+
imported_object_type = "factory" if is_factory else "app"
140141
toolkit.print(
141-
"Importing the FastAPI app object from the module with the following code:",
142+
f"Importing the FastAPI {imported_object_type} object from the module with the following code:",
142143
tag="code",
143144
)
144145
toolkit.print_line()
145146
toolkit.print(
146-
f"[underline]from [bold]{module_data.module_import_str}[/bold] import [bold]{import_data.app_name}[/bold]"
147+
f"[underline]from [bold]{module_data.module_import_str}[/bold] import [bold]{import_data.candidate_name}[/bold]"
147148
)
148149
toolkit.print_line()
149150

src/fastapi_cli/discover.py

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,7 @@ def get_module_data_from_path(path: Path) -> ModuleData:
6565
)
6666

6767

68-
def get_app_name(
69-
*, mod_data: ModuleData, app_name: Union[str, None] = None, is_factory: bool = False
70-
) -> str:
68+
def get_app_name(*, mod_data: ModuleData, app_name: Union[str, None] = None) -> str:
7169
try:
7270
mod = importlib.import_module(mod_data.module_import_str)
7371
except (ImportError, ValueError) as e:
@@ -88,26 +86,19 @@ def get_app_name(
8886
f"Could not find app name {app_name} in {mod_data.module_import_str}"
8987
)
9088
app = getattr(mod, app_name)
91-
if not isinstance(app, FastAPI) and not is_factory:
89+
if not isinstance(app, FastAPI):
9290
raise FastAPICLIException(
9391
f"The app name {app_name} in {mod_data.module_import_str} doesn't seem to be a FastAPI app"
9492
)
95-
else:
96-
if not callable(app) and is_factory:
97-
raise FastAPICLIException(
98-
f"The app factory {app_name} in {mod_data.module_import_str} doesn't seem to be a function"
99-
)
10093
return app_name
10194
for preferred_name in ["app", "api"]:
10295
if preferred_name in object_names_set:
10396
obj = getattr(mod, preferred_name)
104-
if isinstance(obj, FastAPI) and not is_factory:
97+
if isinstance(obj, FastAPI):
10598
return preferred_name
10699
for name in object_names:
107100
obj = getattr(mod, name)
108-
if isinstance(obj, FastAPI) and not is_factory:
109-
return name
110-
elif callable(name) and is_factory:
101+
if isinstance(obj, FastAPI):
111102
return name
112103
raise FastAPICLIException(
113104
"Could not find FastAPI app or app factory in module, try using --app"
@@ -116,13 +107,16 @@ def get_app_name(
116107

117108
@dataclass
118109
class ImportData:
119-
app_name: str
110+
# candidate is an app or a factory
111+
candidate_name: str
120112
module_data: ModuleData
121113
import_string: str
122114

123115

124116
def get_import_data(
125-
*, path: Union[Path, None] = None, app_name: Union[str, None] = None
117+
*,
118+
path: Union[Path, None] = None,
119+
app_name: Union[str, None] = None,
126120
) -> ImportData:
127121
if not path:
128122
path = get_default_path()
@@ -139,7 +133,7 @@ def get_import_data(
139133
import_string = f"{mod_data.module_import_str}:{use_app_name}"
140134

141135
return ImportData(
142-
app_name=use_app_name, module_data=mod_data, import_string=import_string
136+
candidate_name=use_app_name, module_data=mod_data, import_string=import_string
143137
)
144138

145139

@@ -156,7 +150,7 @@ def get_import_data_from_import_string(import_string: str) -> ImportData:
156150
sys.path.insert(0, str(here))
157151

158152
return ImportData(
159-
app_name=app_name,
153+
candidate_name=app_name,
160154
module_data=ModuleData(
161155
module_import_str=module_str,
162156
extra_sys_path=here,

0 commit comments

Comments
 (0)