Skip to content

Commit ecd8d3d

Browse files
authored
Merge pull request #6 from Minibrams/feature/route-aliases
fix: performance
2 parents 9ccc85b + 394c603 commit ecd8d3d

1 file changed

Lines changed: 11 additions & 11 deletions

File tree

openapi_python/generator/render.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -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

4244
def _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+
100106
def _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

112114
def _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

120120
def _order_aliases(defns: tuple[TypeAliasDef, ...]) -> list[TypeAliasDef]:

0 commit comments

Comments
 (0)