@@ -1341,6 +1341,39 @@ def __alias_shadowed_imports( # noqa: PLR6301
13411341 reference_path = model_field .data_type .import_ .reference_path ,
13421342 )
13431343
1344+ @classmethod
1345+ def _collect_used_names_from_models (cls , models : list [DataModel ]) -> set [str ]:
1346+ """Collect identifiers referenced by models before rendering."""
1347+ names : set [str ] = set ()
1348+
1349+ def add (name : str | None ) -> None :
1350+ if not name :
1351+ return
1352+ # first segment is sufficient to match import target or alias
1353+ names .add (name .split ("." )[0 ])
1354+
1355+ def walk_data_type (data_type : DataType ) -> None :
1356+ add (data_type .alias or data_type .type )
1357+ if data_type .reference :
1358+ add (data_type .reference .short_name )
1359+ for child in data_type .data_types :
1360+ walk_data_type (child )
1361+ if data_type .dict_key :
1362+ walk_data_type (data_type .dict_key )
1363+
1364+ for model in models :
1365+ add (model .class_name )
1366+ add (model .duplicate_class_name )
1367+ for base in model .base_classes :
1368+ add (base .type_hint )
1369+ for import_ in model .imports :
1370+ add (import_ .alias or import_ .import_ .split ("." )[- 1 ])
1371+ for field in model .fields :
1372+ add (field .name )
1373+ add (field .alias )
1374+ walk_data_type (field .data_type )
1375+ return names
1376+
13441377 def parse ( # noqa: PLR0912, PLR0914, PLR0915
13451378 self ,
13461379 with_import : bool | None = True , # noqa: FBT001, FBT002
@@ -1466,12 +1499,14 @@ class Processed(NamedTuple):
14661499
14671500 for processed_model in processed_models :
14681501 # postprocess imports to remove unused imports.
1469- model_code = str ( " \n " . join ([ str ( m ) for m in processed_model .models ]) )
1502+ used_names = self . _collect_used_names_from_models ( processed_model .models )
14701503 unused_imports = [
14711504 (from_ , import_ )
14721505 for from_ , imports_ in processed_model .imports .items ()
14731506 for import_ in imports_
1474- if import_ not in model_code
1507+ if not {processed_model .imports .alias .get (from_ , {}).get (import_ , import_ ), import_ }.intersection (
1508+ used_names
1509+ )
14751510 ]
14761511 for from_ , import_ in unused_imports :
14771512 processed_model .imports .remove (Import (from_ = from_ , import_ = import_ ))
0 commit comments