Skip to content

Commit 5516174

Browse files
committed
feat: update type checker to 'ty', enhance type hints, and improve code formatting
1 parent 28f0ae8 commit 5516174

18 files changed

+134
-157
lines changed

.github/workflows/tests.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ jobs:
3838
run: poetry run pytest --cov=src/ --cov-report=xml --cov-fail-under 90 --cov-config=.coveragerc
3939

4040
- name: 🔍 Type checking with mypy
41-
run: poetry run mypy src/
41+
run: poetry run ty src/
4242

4343
- name: 🎨 Code formatting with black
4444
run: poetry run black --check .

mypy.ini

Lines changed: 0 additions & 14 deletions
This file was deleted.

poetry.lock

Lines changed: 38 additions & 85 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ keywords = ["OpenAPI", "Generator", "Python", "async"]
1717
Changelog = "https://github.com/MarcoMuellner/openapi-python-generator/releases"
1818

1919
[tool.poetry.dependencies]
20-
python = "^3.8"
20+
python = "^3.9"
2121
httpx = {extras = ["all"], version = "^0.28.0"}
2222
pydantic = "^2.10.2"
2323
orjson = "^3.9.15"
@@ -27,14 +27,15 @@ black = ">=21.10b0"
2727
isort = ">=5.10.1"
2828
openapi-pydantic = "^0.5.1"
2929
pyyaml = "^6.0.2"
30+
importlib-metadata = "^8.7.0"
3031

3132
[tool.poetry.group.dev.dependencies]
3233
Pygments = ">=2.10.0"
3334
coverage = {extras = ["toml"], version = "^6.4.1"}
3435
darglint = ">=1.8.1"
3536
ruff = ">=0.12.12"
3637
furo = ">=2021.11.12"
37-
mypy = ">=0.930"
38+
ty = "^0.0.1a20"
3839
pep8-naming = ">=0.10.1"
3940
pre-commit = ">=2.16.0"
4041
pre-commit-hooks = ">=4.1.0"

src/openapi_python_generator/__main__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ def main(
7272
generate_data(
7373
source,
7474
output,
75-
library,
75+
library if library is not None else HTTPLibrary.httpx,
7676
env_token_name,
7777
use_orjson,
7878
custom_template_path,

src/openapi_python_generator/language_converters/python/common.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from typing import Optional
44

55
_use_orjson: bool = False
6-
_custom_template_path: str = None
6+
_custom_template_path: str | None = None
77
_symbol_ascii_strip_re = re.compile(r"[^A-Za-z0-9_]")
88

99

src/openapi_python_generator/language_converters/python/model_generator.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ def type_converter( # noqa: C901
8989
conversions.append(type_converter(sub_schema, True))
9090
else:
9191
import_type = common.normalize_symbol(sub_schema.ref.split("/")[-1])
92-
if import_type == model_name:
92+
if import_type == model_name and model_name is not None:
9393
conversions.append(
9494
TypeConversion(
9595
original_type=sub_schema.ref,

src/openapi_python_generator/language_converters/python/service_generator.py

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,13 @@
4545
from openapi_python_generator.language_converters.python.model_generator import (
4646
type_converter,
4747
)
48-
from openapi_python_generator.models import LibraryConfig, OpReturnType, Service, ServiceOperation, TypeConversion
48+
from openapi_python_generator.models import (
49+
LibraryConfig,
50+
OpReturnType,
51+
Service,
52+
ServiceOperation,
53+
TypeConversion,
54+
)
4955

5056

5157
# Helper functions for isinstance checks across OpenAPI versions
@@ -54,16 +60,18 @@ def is_response_type(obj) -> bool:
5460
return isinstance(obj, (Response30, Response31))
5561

5662

57-
def create_media_type_for_reference(reference_obj):
63+
def create_media_type_for_reference(
64+
reference_obj: Response30 | Reference30 | Response31 | Reference31,
65+
):
5866
"""Create a MediaType wrapper for a reference object, using the correct version"""
5967
# Check which version the reference object belongs to
6068
if isinstance(reference_obj, Reference30):
61-
return MediaType30(schema=reference_obj)
69+
return MediaType30(schema=reference_obj) # type: ignore - pydantic issue with generics
6270
elif isinstance(reference_obj, Reference31):
63-
return MediaType31(schema=reference_obj)
71+
return MediaType31(schema=reference_obj) # type: ignore - pydantic issue with generics
6472
else:
6573
# Fallback to v3.0 for generic Reference
66-
return MediaType30(schema=reference_obj)
74+
return MediaType30(schema=reference_obj) # type: ignore - pydantic issue with generics
6775

6876

6977
def is_media_type(obj) -> bool:
@@ -185,13 +193,16 @@ def _generate_params_from_content(content: Any):
185193
if isinstance(rb_content, dict) and any(
186194
rb_content.get(i) is not None for i in operation_request_body_types
187195
):
188-
get_keyword = [i for i in operation_request_body_types if rb_content.get(i)][
189-
0
190-
]
196+
get_keyword = [
197+
i for i in operation_request_body_types if rb_content.get(i)
198+
][0]
191199
content = rb_content.get(get_keyword)
192200
if content is not None and hasattr(content, "media_type_schema"):
193201
mts = getattr(content, "media_type_schema", None)
194-
if isinstance(mts, (Reference, Reference30, Reference31, Schema, Schema30, Schema31)):
202+
if isinstance(
203+
mts,
204+
(Reference, Reference30, Reference31, Schema, Schema30, Schema31),
205+
):
195206
params += f"{_generate_params_from_content(mts)}, "
196207
else: # pragma: no cover
197208
raise Exception(
@@ -283,9 +294,8 @@ def generate_return_type(operation: Operation) -> OpReturnType:
283294
)
284295
elif is_schema_type(inner_schema):
285296
converted_result = type_converter(inner_schema, True) # type: ignore
286-
if (
287-
"array" in converted_result.original_type
288-
and isinstance(converted_result.import_types, list)
297+
if "array" in converted_result.original_type and isinstance(
298+
converted_result.import_types, list
289299
):
290300
matched = re.findall(r"List\[(.+)\]", converted_result.converted_type)
291301
if len(matched) > 0:
@@ -348,23 +358,28 @@ def generate_service_operation(
348358
op.parameters = [] # type: ignore
349359
op.parameters.append(p) # type: ignore
350360
except Exception: # pragma: no cover
351-
print(f"Error merging path-level parameters for {path_name}") # pragma: no cover
361+
print(
362+
f"Error merging path-level parameters for {path_name}"
363+
) # pragma: no cover
352364
pass
353365

354366
params = generate_params(op)
355367
# Fallback: ensure all {placeholders} in path are present as function params
356368
try:
357-
placeholder_names = [m.group(1) for m in re.finditer(r"\{([^}/]+)\}", path_name)]
369+
placeholder_names = [
370+
m.group(1) for m in re.finditer(r"\{([^}/]+)\}", path_name)
371+
]
358372
existing_param_names = {
359-
p.split(":")[0].strip()
360-
for p in params.split(",") if ":" in p
373+
p.split(":")[0].strip() for p in params.split(",") if ":" in p
361374
}
362375
for ph in placeholder_names:
363376
norm_ph = common.normalize_symbol(ph)
364377
if norm_ph not in existing_param_names and norm_ph:
365378
params = f"{norm_ph}: Any, " + params
366379
except Exception: # pragma: no cover
367-
print(f"Error ensuring path placeholders in params for {path_name}") # pragma: no cover
380+
print(
381+
f"Error ensuring path placeholders in params for {path_name}"
382+
) # pragma: no cover
368383
pass
369384
operation_id = generate_operation_id(op, http_operation, path_name)
370385
query_params = generate_query_params(op)

src/openapi_python_generator/parsers/openapi_30.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def parse_openapi_3_0(spec_data: dict) -> OpenAPI:
2626
Raises:
2727
ValidationError: If the specification is invalid
2828
"""
29-
return OpenAPI(**spec_data)
29+
return OpenAPI(**spec_data) # type: ignore - pydantic issue with extra fields
3030

3131

3232
def generate_code_3_0(

0 commit comments

Comments
 (0)