@@ -38,6 +38,8 @@ def _field_annotation(field: FieldDef) -> str:
3838_JINJA_ENV .filters ["repr" ] = repr
3939_JINJA_ENV .filters ["field_annotation" ] = _field_annotation
4040
41+ _IDENTIFIER = re .compile (r"\b[A-Za-z_][A-Za-z0-9_]*\b" )
42+
4143
4244def _render_template (name : str , ** context : object ) -> str :
4345 return _JINJA_ENV .get_template (name ).render (** context )
@@ -97,24 +99,22 @@ def _format_enum(defn: EnumDef) -> str:
9799 )
98100
99101
102+ def _annotation_dependencies (annotation : str , names : set [str ]) -> set [str ]:
103+ return names .intersection (_IDENTIFIER .findall (annotation ))
104+
105+
100106def _typed_dict_dependencies (defn : TypedDictDef , names : set [str ]) -> set [str ]:
101107 dependencies : set [str ] = set ()
102108 for field in defn .fields :
103- dependencies .update (
104- name
105- for name in names
106- if name != defn .name
107- and re .search (rf"\b{ re .escape (name )} \b" , field .annotation )
108- )
109+ dependencies .update (_annotation_dependencies (field .annotation , names ))
110+ dependencies .discard (defn .name )
109111 return dependencies
110112
111113
112114def _alias_dependencies (defn : TypeAliasDef , names : set [str ]) -> set [str ]:
113- return {
114- name
115- for name in names
116- if name != defn .name and re .search (rf"\b{ re .escape (name )} \b" , defn .annotation )
117- }
115+ dependencies = _annotation_dependencies (defn .annotation , names )
116+ dependencies .discard (defn .name )
117+ return dependencies
118118
119119
120120def _order_aliases (defns : tuple [TypeAliasDef , ...]) -> list [TypeAliasDef ]:
0 commit comments