@@ -65,9 +65,7 @@ def get_module_data_from_path(path: Path) -> ModuleData:
65
65
)
66
66
67
67
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 :
71
69
try :
72
70
mod = importlib .import_module (mod_data .module_import_str )
73
71
except (ImportError , ValueError ) as e :
@@ -88,26 +86,19 @@ def get_app_name(
88
86
f"Could not find app name { app_name } in { mod_data .module_import_str } "
89
87
)
90
88
app = getattr (mod , app_name )
91
- if not isinstance (app , FastAPI ) and not is_factory :
89
+ if not isinstance (app , FastAPI ):
92
90
raise FastAPICLIException (
93
91
f"The app name { app_name } in { mod_data .module_import_str } doesn't seem to be a FastAPI app"
94
92
)
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
- )
100
93
return app_name
101
94
for preferred_name in ["app" , "api" ]:
102
95
if preferred_name in object_names_set :
103
96
obj = getattr (mod , preferred_name )
104
- if isinstance (obj , FastAPI ) and not is_factory :
97
+ if isinstance (obj , FastAPI ):
105
98
return preferred_name
106
99
for name in object_names :
107
100
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 ):
111
102
return name
112
103
raise FastAPICLIException (
113
104
"Could not find FastAPI app or app factory in module, try using --app"
@@ -116,13 +107,16 @@ def get_app_name(
116
107
117
108
@dataclass
118
109
class ImportData :
119
- app_name : str
110
+ # candidate is an app or a factory
111
+ candidate_name : str
120
112
module_data : ModuleData
121
113
import_string : str
122
114
123
115
124
116
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 ,
126
120
) -> ImportData :
127
121
if not path :
128
122
path = get_default_path ()
@@ -139,7 +133,7 @@ def get_import_data(
139
133
import_string = f"{ mod_data .module_import_str } :{ use_app_name } "
140
134
141
135
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
143
137
)
144
138
145
139
@@ -156,7 +150,7 @@ def get_import_data_from_import_string(import_string: str) -> ImportData:
156
150
sys .path .insert (0 , str (here ))
157
151
158
152
return ImportData (
159
- app_name = app_name ,
153
+ candidate_name = app_name ,
160
154
module_data = ModuleData (
161
155
module_import_str = module_str ,
162
156
extra_sys_path = here ,
0 commit comments