diff --git a/.github/workflows/checks.yml b/.github/workflows/checks.yml index b777cde9f..4709d48bd 100644 --- a/.github/workflows/checks.yml +++ b/.github/workflows/checks.yml @@ -101,9 +101,6 @@ jobs: - name: Run mypy run: pdm mypy --show-error-codes - - name: Lint - run: pdm run ruff check . - - name: Run unit tests only # snapshots are expected to fail run: pdm unit_test diff --git a/openapi_python_client/cli.py b/openapi_python_client/cli.py index 92d91f943..4b55e4bc9 100644 --- a/openapi_python_client/cli.py +++ b/openapi_python_client/cli.py @@ -6,7 +6,7 @@ import typer -from openapi_python_client import MetaType +from openapi_python_client import MetaType, __version__ from openapi_python_client.config import Config, ConfigFile from openapi_python_client.parser.errors import ErrorLevel, GeneratorError, ParseError @@ -14,8 +14,6 @@ def _version_callback(value: bool) -> None: - from openapi_python_client import __version__ - if value: typer.echo(f"openapi-python-client version: {__version__}") raise typer.Exit() @@ -153,7 +151,7 @@ def generate( ), ) -> None: """Generate a new OpenAPI Client library""" - from . import generate + from . import generate # noqa: PLC0415 config = _process_config( url=url, diff --git a/openapi_python_client/parser/properties/any.py b/openapi_python_client/parser/properties/any.py index b760a1568..65fcf40c4 100644 --- a/openapi_python_client/parser/properties/any.py +++ b/openapi_python_client/parser/properties/any.py @@ -33,7 +33,7 @@ def build( @classmethod def convert_value(cls, value: Any) -> Value | None: - from .string import StringProperty + from .string import StringProperty # noqa: PLC0415 if value is None: return value diff --git a/openapi_python_client/parser/properties/list_property.py b/openapi_python_client/parser/properties/list_property.py index 7a4a4f209..06d773672 100644 --- a/openapi_python_client/parser/properties/list_property.py +++ b/openapi_python_client/parser/properties/list_property.py @@ -56,7 +56,7 @@ def build( `(result, schemas)` where `schemas` is an updated version of the input named the same including any inner classes that were defined and `result` is either the `ListProperty` or a `PropertyError`. """ - from . import property_from_data + from . import property_from_data # noqa: PLC0415 if data.items is None and not data.prefixItems: return ( diff --git a/openapi_python_client/parser/properties/model_property.py b/openapi_python_client/parser/properties/model_property.py index 687ef2542..805de1c7b 100644 --- a/openapi_python_client/parser/properties/model_property.py +++ b/openapi_python_client/parser/properties/model_property.py @@ -242,8 +242,8 @@ def _process_properties( # noqa: PLR0912, PLR0911 config: Config, roots: set[ReferencePath | utils.ClassName], ) -> _PropertyData | PropertyError: - from . import property_from_data - from .merge_properties import merge_properties + from . import property_from_data # noqa: PLC0415 + from .merge_properties import merge_properties # noqa: PLC0415 properties: dict[str, Property] = {} relative_imports: set[str] = set() @@ -353,7 +353,7 @@ def _get_additional_properties( config: Config, roots: set[ReferencePath | utils.ClassName], ) -> tuple[Property | None | PropertyError, Schemas]: - from . import property_from_data + from . import property_from_data # noqa: PLC0415 if schema_additional is None: return ANY_ADDITIONAL_PROPERTY, schemas diff --git a/openapi_python_client/parser/properties/protocol.py b/openapi_python_client/parser/properties/protocol.py index 7c1891545..327ba0a5e 100644 --- a/openapi_python_client/parser/properties/protocol.py +++ b/openapi_python_client/parser/properties/protocol.py @@ -174,7 +174,7 @@ def to_docstring(self) -> str: @property def is_base_type(self) -> bool: """Base types, represented by any other of `Property` than `ModelProperty` should not be quoted.""" - from . import ListProperty, ModelProperty, UnionProperty + from . import ListProperty, ModelProperty, UnionProperty # noqa: PLC0415 return self.__class__.__name__ not in { ModelProperty.__name__, diff --git a/openapi_python_client/parser/properties/schemas.py b/openapi_python_client/parser/properties/schemas.py index 40dbd7374..acfb21c8d 100644 --- a/openapi_python_client/parser/properties/schemas.py +++ b/openapi_python_client/parser/properties/schemas.py @@ -118,7 +118,7 @@ def update_schemas_with_data( See Also: - https://swagger.io/docs/specification/using-ref/ """ - from . import property_from_data + from . import property_from_data # noqa: PLC0415 prop: Union[PropertyError, Property] prop, schemas = property_from_data( diff --git a/openapi_python_client/parser/properties/union.py b/openapi_python_client/parser/properties/union.py index b562a07a8..bec9ca574 100644 --- a/openapi_python_client/parser/properties/union.py +++ b/openapi_python_client/parser/properties/union.py @@ -45,7 +45,7 @@ def build( `(result, schemas)` where `schemas` is the updated version of the input `schemas` and `result` is the constructed `UnionProperty` or a `PropertyError` describing what went wrong. """ - from . import property_from_data + from . import property_from_data # noqa: PLC0415 sub_properties: list[PropertyProtocol] = [] @@ -180,7 +180,7 @@ def get_lazy_imports(self, *, prefix: str) -> set[str]: def validate_location(self, location: oai.ParameterLocation) -> ParseError | None: """Returns an error if this type of property is not allowed in the given location""" - from ..properties import Property + from ..properties import Property # noqa: PLC0415 for inner_prop in self.inner_properties: if evolve(cast(Property, inner_prop), required=self.required).validate_location(location) is not None: diff --git a/pdm.lock b/pdm.lock index 5b285b711..469ceb256 100644 --- a/pdm.lock +++ b/pdm.lock @@ -5,7 +5,7 @@ groups = ["default", "dev"] strategy = ["inherit_metadata"] lock_version = "4.5.0" -content_hash = "sha256:af4a602e8e6cec54bdd45bf89526aa06cbfabd35864e2008a7d6c9d31f41e972" +content_hash = "sha256:aadc8a7c11176fc4fef967f0e34f059a3555abcd6626bd0a0b13b898db522860" [[metadata.targets]] requires_python = "~=3.9" @@ -829,29 +829,29 @@ files = [ [[package]] name = "ruff" -version = "0.11.13" +version = "0.12.0" requires_python = ">=3.7" summary = "An extremely fast Python linter and code formatter, written in Rust." groups = ["default"] files = [ - {file = "ruff-0.11.13-py3-none-linux_armv6l.whl", hash = "sha256:4bdfbf1240533f40042ec00c9e09a3aade6f8c10b6414cf11b519488d2635d46"}, - {file = "ruff-0.11.13-py3-none-macosx_10_12_x86_64.whl", hash = "sha256:aef9c9ed1b5ca28bb15c7eac83b8670cf3b20b478195bd49c8d756ba0a36cf48"}, - {file = "ruff-0.11.13-py3-none-macosx_11_0_arm64.whl", hash = "sha256:53b15a9dfdce029c842e9a5aebc3855e9ab7771395979ff85b7c1dedb53ddc2b"}, - {file = "ruff-0.11.13-py3-none-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:ab153241400789138d13f362c43f7edecc0edfffce2afa6a68434000ecd8f69a"}, - {file = "ruff-0.11.13-py3-none-manylinux_2_17_armv7l.manylinux2014_armv7l.whl", hash = "sha256:6c51f93029d54a910d3d24f7dd0bb909e31b6cd989a5e4ac513f4eb41629f0dc"}, - {file = "ruff-0.11.13-py3-none-manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:1808b3ed53e1a777c2ef733aca9051dc9bf7c99b26ece15cb59a0320fbdbd629"}, - {file = "ruff-0.11.13-py3-none-manylinux_2_17_ppc64.manylinux2014_ppc64.whl", hash = "sha256:d28ce58b5ecf0f43c1b71edffabe6ed7f245d5336b17805803312ec9bc665933"}, - {file = "ruff-0.11.13-py3-none-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:55e4bc3a77842da33c16d55b32c6cac1ec5fb0fbec9c8c513bdce76c4f922165"}, - {file = "ruff-0.11.13-py3-none-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:633bf2c6f35678c56ec73189ba6fa19ff1c5e4807a78bf60ef487b9dd272cc71"}, - {file = "ruff-0.11.13-py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:4ffbc82d70424b275b089166310448051afdc6e914fdab90e08df66c43bb5ca9"}, - {file = "ruff-0.11.13-py3-none-musllinux_1_2_aarch64.whl", hash = "sha256:4a9ddd3ec62a9a89578c85842b836e4ac832d4a2e0bfaad3b02243f930ceafcc"}, - {file = "ruff-0.11.13-py3-none-musllinux_1_2_armv7l.whl", hash = "sha256:d237a496e0778d719efb05058c64d28b757c77824e04ffe8796c7436e26712b7"}, - {file = "ruff-0.11.13-py3-none-musllinux_1_2_i686.whl", hash = "sha256:26816a218ca6ef02142343fd24c70f7cd8c5aa6c203bca284407adf675984432"}, - {file = "ruff-0.11.13-py3-none-musllinux_1_2_x86_64.whl", hash = "sha256:51c3f95abd9331dc5b87c47ac7f376db5616041173826dfd556cfe3d4977f492"}, - {file = "ruff-0.11.13-py3-none-win32.whl", hash = "sha256:96c27935418e4e8e77a26bb05962817f28b8ef3843a6c6cc49d8783b5507f250"}, - {file = "ruff-0.11.13-py3-none-win_amd64.whl", hash = "sha256:29c3189895a8a6a657b7af4e97d330c8a3afd2c9c8f46c81e2fc5a31866517e3"}, - {file = "ruff-0.11.13-py3-none-win_arm64.whl", hash = "sha256:b4385285e9179d608ff1d2fb9922062663c658605819a6876d8beef0c30b7f3b"}, - {file = "ruff-0.11.13.tar.gz", hash = "sha256:26fa247dc68d1d4e72c179e08889a25ac0c7ba4d78aecfc835d49cbfd60bf514"}, + {file = "ruff-0.12.0-py3-none-linux_armv6l.whl", hash = "sha256:5652a9ecdb308a1754d96a68827755f28d5dfb416b06f60fd9e13f26191a8848"}, + {file = "ruff-0.12.0-py3-none-macosx_10_12_x86_64.whl", hash = "sha256:05ed0c914fabc602fc1f3b42c53aa219e5736cb030cdd85640c32dbc73da74a6"}, + {file = "ruff-0.12.0-py3-none-macosx_11_0_arm64.whl", hash = "sha256:07a7aa9b69ac3fcfda3c507916d5d1bca10821fe3797d46bad10f2c6de1edda0"}, + {file = "ruff-0.12.0-py3-none-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:e7731c3eec50af71597243bace7ec6104616ca56dda2b99c89935fe926bdcd48"}, + {file = "ruff-0.12.0-py3-none-manylinux_2_17_armv7l.manylinux2014_armv7l.whl", hash = "sha256:952d0630eae628250ab1c70a7fffb641b03e6b4a2d3f3ec6c1d19b4ab6c6c807"}, + {file = "ruff-0.12.0-py3-none-manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:c021f04ea06966b02614d442e94071781c424ab8e02ec7af2f037b4c1e01cc82"}, + {file = "ruff-0.12.0-py3-none-manylinux_2_17_ppc64.manylinux2014_ppc64.whl", hash = "sha256:7d235618283718ee2fe14db07f954f9b2423700919dc688eacf3f8797a11315c"}, + {file = "ruff-0.12.0-py3-none-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:0c0758038f81beec8cc52ca22de9685b8ae7f7cc18c013ec2050012862cc9165"}, + {file = "ruff-0.12.0-py3-none-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:139b3d28027987b78fc8d6cfb61165447bdf3740e650b7c480744873688808c2"}, + {file = "ruff-0.12.0-py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:68853e8517b17bba004152aebd9dd77d5213e503a5f2789395b25f26acac0da4"}, + {file = "ruff-0.12.0-py3-none-musllinux_1_2_aarch64.whl", hash = "sha256:3a9512af224b9ac4757f7010843771da6b2b0935a9e5e76bb407caa901a1a514"}, + {file = "ruff-0.12.0-py3-none-musllinux_1_2_armv7l.whl", hash = "sha256:b08df3d96db798e5beb488d4df03011874aff919a97dcc2dd8539bb2be5d6a88"}, + {file = "ruff-0.12.0-py3-none-musllinux_1_2_i686.whl", hash = "sha256:6a315992297a7435a66259073681bb0d8647a826b7a6de45c6934b2ca3a9ed51"}, + {file = "ruff-0.12.0-py3-none-musllinux_1_2_x86_64.whl", hash = "sha256:1e55e44e770e061f55a7dbc6e9aed47feea07731d809a3710feda2262d2d4d8a"}, + {file = "ruff-0.12.0-py3-none-win32.whl", hash = "sha256:7162a4c816f8d1555eb195c46ae0bd819834d2a3f18f98cc63819a7b46f474fb"}, + {file = "ruff-0.12.0-py3-none-win_amd64.whl", hash = "sha256:d00b7a157b8fb6d3827b49d3324da34a1e3f93492c1f97b08e222ad7e9b291e0"}, + {file = "ruff-0.12.0-py3-none-win_arm64.whl", hash = "sha256:8cd24580405ad8c1cc64d61725bca091d6b6da7eb3d36f72cc605467069d7e8b"}, + {file = "ruff-0.12.0.tar.gz", hash = "sha256:4d047db3662418d4a848a3fdbfaf17488b34b62f527ed6f10cb8afd78135bc5c"}, ] [[package]] diff --git a/pyproject.toml b/pyproject.toml index d2ff2c87a..1ef9dc507 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -14,7 +14,7 @@ dependencies = [ "python-dateutil>=2.8.1,<3.0.0", "httpx>=0.23.0,<0.29.0", "ruamel.yaml>=0.18.6,<0.19.0", - "ruff>=0.2,<0.12", + "ruff>=0.2,<0.13", "typing-extensions>=4.8.0,<5.0.0", ] name = "openapi-python-client" diff --git a/tests/conftest.py b/tests/conftest.py index 969e57cbd..d27a2edef 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -56,7 +56,6 @@ def model_property_factory() -> ModelFactory: You can pass the same params into this as the ModelProperty constructor to override defaults. """ - from openapi_python_client.parser.properties import Class def _factory(**kwargs): kwargs = _common_kwargs(kwargs) @@ -130,7 +129,6 @@ def enum_property_factory() -> EnumFactory[EnumProperty]: You can pass the same params into this as the EnumProerty constructor to override defaults. """ - from openapi_python_client.parser.properties import Class return _simple_factory( EnumProperty, @@ -149,7 +147,6 @@ def literal_enum_property_factory() -> EnumFactory[LiteralEnumProperty]: You can pass the same params into this as the LiteralEnumProerty constructor to override defaults. """ - from openapi_python_client.parser.properties import Class return _simple_factory( LiteralEnumProperty, diff --git a/tests/test___init__.py b/tests/test___init__.py index 3d9b7a0f0..34ad3188f 100644 --- a/tests/test___init__.py +++ b/tests/test___init__.py @@ -1,3 +1,5 @@ +from unittest.mock import MagicMock + import pytest from openapi_python_client import Config, ErrorLevel, Project @@ -7,10 +9,6 @@ def make_project(config: Config) -> Project: - from unittest.mock import MagicMock - - from openapi_python_client import Project - return Project(openapi=MagicMock(title="My Test API"), config=config) diff --git a/tests/test_cli.py b/tests/test_cli.py index 8679584fd..0775ce5c1 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -1,11 +1,11 @@ from typer.testing import CliRunner +from openapi_python_client.cli import app + runner = CliRunner() def test_version() -> None: - from openapi_python_client.cli import app - result = runner.invoke(app, ["--version", "generate"]) assert result.exit_code == 0 @@ -13,8 +13,6 @@ def test_version() -> None: def test_bad_config() -> None: - from openapi_python_client.cli import app - config_path = "config/path" path = "cool/path" @@ -26,15 +24,11 @@ def test_bad_config() -> None: class TestGenerate: def test_generate_no_params(self) -> None: - from openapi_python_client.cli import app - result = runner.invoke(app, ["generate"]) assert result.exit_code == 1, result.output def test_generate_url_and_path(self) -> None: - from openapi_python_client.cli import app - result = runner.invoke(app, ["generate", "--path=blah", "--url=otherblah"]) assert result.exit_code == 1 @@ -43,8 +37,6 @@ def test_generate_url_and_path(self) -> None: def test_generate_encoding_errors(self) -> None: path = "cool/path" file_encoding = "error-file-encoding" - from openapi_python_client.cli import app - result = runner.invoke(app, ["generate", f"--path={path}", f"--file-encoding={file_encoding}"]) assert result.exit_code == 1 diff --git a/tests/test_parser/test_openapi.py b/tests/test_parser/test_openapi.py index 3c810e344..3d1391ae2 100644 --- a/tests/test_parser/test_openapi.py +++ b/tests/test_parser/test_openapi.py @@ -5,8 +5,8 @@ import openapi_python_client.schema as oai from openapi_python_client.parser.errors import ParseError -from openapi_python_client.parser.openapi import Endpoint, EndpointCollection -from openapi_python_client.parser.properties import IntProperty, Parameters, Schemas +from openapi_python_client.parser.openapi import Endpoint, EndpointCollection, import_string_from_class +from openapi_python_client.parser.properties import Class, IntProperty, Parameters, Schemas from openapi_python_client.schema import DataType MODULE_NAME = "openapi_python_client.parser.openapi" @@ -14,8 +14,6 @@ class TestEndpoint: def make_endpoint(self): - from openapi_python_client.parser.openapi import Endpoint - return Endpoint( path="path", method="method", @@ -28,8 +26,6 @@ def make_endpoint(self): @pytest.mark.parametrize("response_status_code", ["not_a_number", 499]) def test__add_responses_status_code_error(self, response_status_code, mocker): - from openapi_python_client.parser.openapi import Endpoint, Schemas - schemas = Schemas() response_1_data = mocker.MagicMock() data = { @@ -53,8 +49,6 @@ def test__add_responses_status_code_error(self, response_status_code, mocker): response_from_data.assert_not_called() def test__add_responses_error(self, mocker): - from openapi_python_client.parser.openapi import Endpoint, Schemas - schemas = Schemas() response_1_data = mocker.MagicMock() response_2_data = mocker.MagicMock() @@ -105,8 +99,6 @@ def test__add_responses_error(self, mocker): ] def test_add_parameters_handles_no_params(self): - from openapi_python_client.parser.openapi import Endpoint, Schemas - endpoint = self.make_endpoint() schemas = Schemas() parameters = Parameters() @@ -122,8 +114,6 @@ def test_add_parameters_handles_no_params(self): ) == (endpoint, schemas, parameters) def test_add_parameters_parse_error(self, mocker): - from openapi_python_client.parser.openapi import Endpoint - endpoint = self.make_endpoint() initial_schemas = mocker.MagicMock() initial_parameters = mocker.MagicMock() @@ -163,8 +153,6 @@ def test_add_parameters_parse_error(self, mocker): ], ) def test_add_parameters_header_types(self, data_type, allowed, config): - from openapi_python_client.parser.openapi import Endpoint - endpoint = self.make_endpoint() initial_schemas = Schemas() parameters = Parameters() @@ -331,8 +319,6 @@ def test__add_parameters_query_optionality(self, config): assert not param.required def test_add_parameters_duplicate_properties(self, config): - from openapi_python_client.parser.openapi import Endpoint, Schemas - endpoint = self.make_endpoint() param = oai.Parameter.model_construct( name="test", required=True, param_schema=oai.Schema.model_construct(type="string"), param_in="path" @@ -356,8 +342,6 @@ def test_add_parameters_duplicate_properties(self, config): ) def test_add_parameters_duplicate_properties_different_location(self, config): - from openapi_python_client.parser.openapi import Endpoint, Schemas - endpoint = self.make_endpoint() path_param = oai.Parameter.model_construct( name="test", required=True, param_schema=oai.Schema.model_construct(type="string"), param_in="path" @@ -380,8 +364,6 @@ def test_add_parameters_duplicate_properties_different_location(self, config): assert result.query_parameters[0].name == "test" def test_sort_parameters(self, string_property_factory): - from openapi_python_client.parser.openapi import Endpoint - endpoint = self.make_endpoint() endpoint.path = "/multiple-path-parameters/{param4}/{param2}/{param1}/{param3}" @@ -396,8 +378,6 @@ def test_sort_parameters(self, string_property_factory): assert result_names == expected_names def test_sort_parameters_missing_param(self, string_property_factory): - from openapi_python_client.parser.openapi import Endpoint - endpoint = self.make_endpoint() endpoint.path = "/multiple-path-parameters/{param1}/{param2}" param = string_property_factory(name="param1") @@ -410,8 +390,6 @@ def test_sort_parameters_missing_param(self, string_property_factory): assert endpoint.path in result.detail def test_sort_parameters_extra_param(self, string_property_factory): - from openapi_python_client.parser.openapi import Endpoint - endpoint = self.make_endpoint() endpoint.path = "/multiple-path-parameters" param = string_property_factory(name="param1") @@ -424,8 +402,6 @@ def test_sort_parameters_extra_param(self, string_property_factory): assert endpoint.path in result.detail def test_from_data_bad_params(self, mocker, config): - from openapi_python_client.parser.openapi import Endpoint - path = mocker.MagicMock() method = mocker.MagicMock() parse_error = ParseError(data=mocker.MagicMock()) @@ -456,8 +432,6 @@ def test_from_data_bad_params(self, mocker, config): assert result == (parse_error, return_schemas, return_parameters) def test_from_data_bad_responses(self, mocker, config): - from openapi_python_client.parser.openapi import Endpoint - path = mocker.MagicMock() method = mocker.MagicMock() parse_error = ParseError(data=mocker.MagicMock()) @@ -492,8 +466,6 @@ def test_from_data_bad_responses(self, mocker, config): assert result == (parse_error, response_schemas, return_parameters) def test_from_data_standard(self, mocker, config): - from openapi_python_client.parser.openapi import Endpoint - path = mocker.MagicMock() method = mocker.MagicMock() param_schemas = mocker.MagicMock() @@ -550,8 +522,6 @@ def test_from_data_standard(self, mocker, config): ) def test_from_data_no_operation_id(self, mocker, config): - from openapi_python_client.parser.openapi import Endpoint - path = "/path/with/{param}/" method = "get" add_parameters = mocker.patch.object( @@ -606,8 +576,6 @@ def test_from_data_no_operation_id(self, mocker, config): ) def test_from_data_no_security(self, mocker, config): - from openapi_python_client.parser.openapi import Endpoint - data = oai.Operation.model_construct( description=mocker.MagicMock(), operationId=mocker.MagicMock(), @@ -724,18 +692,12 @@ def test_response_type(self, response_types, expected): class TestImportStringFromReference: def test_import_string_from_reference_no_prefix(self, mocker): - from openapi_python_client.parser.openapi import import_string_from_class - from openapi_python_client.parser.properties import Class - class_ = mocker.MagicMock(autospec=Class) result = import_string_from_class(class_) assert result == f"from .{class_.module_name} import {class_.name}" def test_import_string_from_reference_with_prefix(self, mocker): - from openapi_python_client.parser.openapi import import_string_from_class - from openapi_python_client.parser.properties import Class - prefix = mocker.MagicMock(autospec=str) class_ = mocker.MagicMock(autospec=Class) result = import_string_from_class(class_=class_, prefix=prefix) diff --git a/tests/test_parser/test_properties/test_init.py b/tests/test_parser/test_properties/test_init.py index 3468700db..6db5c2752 100644 --- a/tests/test_parser/test_properties/test_init.py +++ b/tests/test_parser/test_properties/test_init.py @@ -5,9 +5,19 @@ import openapi_python_client.schema as oai from openapi_python_client.parser.errors import ParameterError, PropertyError from openapi_python_client.parser.properties import ( + Class, + Parameters, ReferencePath, Schemas, + _create_schemas, + _process_model_errors, + _process_models, + _propogate_removal, + build_parameters, + build_schemas, + property_from_data, ) +from openapi_python_client.schema import Parameter, Reference, Schema from openapi_python_client.utils import ClassName, PythonIdentifier MODULE_NAME = "openapi_python_client.parser.properties" @@ -137,8 +147,6 @@ def test_get_type_imports(self, union_property_factory, date_time_property_facto class TestPropertyFromData: def test_property_from_data_ref_model(self, model_property_factory, config): - from openapi_python_client.parser.properties import Class, Schemas, property_from_data - name = "new_name" required = False class_name = ClassName("MyModel", "") @@ -163,8 +171,6 @@ def test_property_from_data_ref_model(self, model_property_factory, config): assert schemas == new_schemas def test_property_from_data_ref_not_found(self, mocker): - from openapi_python_client.parser.properties import PropertyError, Schemas, property_from_data - data = oai.Reference.model_construct(ref="a/b/c") parse_reference_path = mocker.patch(f"{MODULE_NAME}.parse_reference_path") schemas = Schemas() @@ -180,8 +186,6 @@ def test_property_from_data_ref_not_found(self, mocker): @pytest.mark.parametrize("references_exist", (True, False)) def test_property_from_data_ref(self, any_property_factory, references_exist, config): - from openapi_python_client.parser.properties import Schemas, property_from_data - name = "new_name" required = False ref_path = "/components/schemas/RefName" @@ -201,8 +205,6 @@ def test_property_from_data_ref(self, any_property_factory, references_exist, co assert schemas.dependencies == {ref_path: {*roots, *references.get(ref_path, set())}} def test_property_from_data_invalid_ref(self, mocker): - from openapi_python_client.parser.properties import PropertyError, Schemas, property_from_data - name = mocker.MagicMock() required = mocker.MagicMock() data = oai.Reference.model_construct(ref=mocker.MagicMock()) @@ -222,8 +224,6 @@ def test_property_from_data_invalid_ref(self, mocker): class TestStringBasedProperty: def test__string_based_property_binary_format(self, file_property_factory, config): - from openapi_python_client.parser.properties import property_from_data - name = "file_prop" required = True data = oai.Schema.model_construct(type="string", schema_format="binary", default="a") @@ -236,9 +236,6 @@ def test__string_based_property_binary_format(self, file_property_factory, confi class TestCreateSchemas: def test_skips_references_and_keeps_going(self, mocker, config): - from openapi_python_client.parser.properties import Schemas, _create_schemas - from openapi_python_client.schema import Reference, Schema - components = {"a_ref": Reference.model_construct(), "a_schema": Schema.model_construct()} update_schemas_with_data = mocker.patch(f"{MODULE_NAME}.update_schemas_with_data") parse_reference_path = mocker.patch(f"{MODULE_NAME}.parse_reference_path") @@ -258,9 +255,6 @@ def test_skips_references_and_keeps_going(self, mocker, config): assert result == update_schemas_with_data.return_value def test_records_bad_uris_and_keeps_going(self, mocker, config): - from openapi_python_client.parser.properties import Schemas, _create_schemas - from openapi_python_client.schema import Schema - components = {"first": Schema.model_construct(), "second": Schema.model_construct()} update_schemas_with_data = mocker.patch(f"{MODULE_NAME}.update_schemas_with_data") parse_reference_path = mocker.patch( @@ -284,9 +278,6 @@ def test_records_bad_uris_and_keeps_going(self, mocker, config): assert result == update_schemas_with_data.return_value def test_retries_failing_properties_while_making_progress(self, mocker, config): - from openapi_python_client.parser.properties import Schemas, _create_schemas - from openapi_python_client.schema import Schema - components = {"first": Schema.model_construct(), "second": Schema.model_construct()} update_schemas_with_data = mocker.patch( f"{MODULE_NAME}.update_schemas_with_data", side_effect=[PropertyError(), Schemas(), PropertyError()] @@ -308,9 +299,6 @@ def test_retries_failing_properties_while_making_progress(self, mocker, config): class TestProcessModels: def test_detect_recursive_allof_reference_no_retry(self, mocker, model_property_factory, config): - from openapi_python_client.parser.properties import Class, _process_models - from openapi_python_client.schema import Reference - class_name = ClassName("class_name", "") recursive_model = model_property_factory( class_info=Class(name=class_name, module_name=PythonIdentifier("module_name", "")) @@ -341,7 +329,6 @@ def test_detect_recursive_allof_reference_no_retry(self, mocker, model_property_ def test_resolve_reference_to_single_allof_reference(self, config, model_property_factory): # test for https://github.com/openapi-generators/openapi-python-client/issues/1091 - from openapi_python_client.parser.properties import Schemas, build_schemas components = { "Model1": oai.Schema.model_construct( @@ -394,9 +381,6 @@ def test_resolve_reference_to_single_allof_reference(self, config, model_propert class TestPropogateRemoval: def test_propogate_removal_class_name(self): - from openapi_python_client.parser.properties import ReferencePath, _propogate_removal - from openapi_python_client.utils import ClassName - root = ClassName("ClassName", "") ref_path = ReferencePath("/reference") other_class_name = ClassName("OtherClassName", "") @@ -414,9 +398,6 @@ def test_propogate_removal_class_name(self): assert not error.detail def test_propogate_removal_ref_path(self): - from openapi_python_client.parser.properties import ReferencePath, _propogate_removal - from openapi_python_client.utils import ClassName - root = ReferencePath("/root/reference") class_name = ClassName("ClassName", "") ref_path = ReferencePath("/ref/path") @@ -434,9 +415,6 @@ def test_propogate_removal_ref_path(self): assert error.detail == f"\n{root}\n{ref_path}" def test_propogate_removal_ref_path_no_refs(self): - from openapi_python_client.parser.properties import ReferencePath, _propogate_removal - from openapi_python_client.utils import ClassName - root = ReferencePath("/root/reference") class_name = ClassName("ClassName", "") ref_path = ReferencePath("/ref/path") @@ -450,9 +428,6 @@ def test_propogate_removal_ref_path_no_refs(self): assert error.detail == f"\n{root}" def test_propogate_removal_ref_path_already_removed(self): - from openapi_python_client.parser.properties import ReferencePath, _propogate_removal - from openapi_python_client.utils import ClassName - root = ReferencePath("/root/reference") class_name = ClassName("ClassName", "") ref_path = ReferencePath("/ref/path") @@ -471,8 +446,6 @@ def test_propogate_removal_ref_path_already_removed(self): def test_process_model_errors(mocker, model_property_factory): - from openapi_python_client.parser.properties import _process_model_errors - propogate_removal = mocker.patch(f"{MODULE_NAME}._propogate_removal") model_errors = [ (model_property_factory(roots={"root1", "root2"}), PropertyError(detail="existing detail")), @@ -492,9 +465,6 @@ def test_process_model_errors(mocker, model_property_factory): class TestBuildParameters: def test_skips_references_and_keeps_going(self, mocker, config): - from openapi_python_client.parser.properties import Parameters, build_parameters - from openapi_python_client.schema import Parameter, Reference - parameters = { "reference": Reference(ref="#/components/parameters/another_parameter"), "defined": Parameter( @@ -524,9 +494,6 @@ def test_skips_references_and_keeps_going(self, mocker, config): assert result == update_parameters_with_data.return_value def test_records_bad_uris_and_keeps_going(self, mocker, config): - from openapi_python_client.parser.properties import Parameters, build_parameters - from openapi_python_client.schema import Parameter - parameters = {"first": Parameter.model_construct(), "second": Parameter.model_construct()} update_parameters_with_data = mocker.patch(f"{MODULE_NAME}.update_parameters_with_data") parse_reference_path = mocker.patch( @@ -549,9 +516,6 @@ def test_records_bad_uris_and_keeps_going(self, mocker, config): assert result == update_parameters_with_data.return_value def test_retries_failing_parameters_while_making_progress(self, mocker, config): - from openapi_python_client.parser.properties import Parameters, build_parameters - from openapi_python_client.schema import Parameter - parameters = {"first": Parameter.model_construct(), "second": Parameter.model_construct()} update_parameters_with_data = mocker.patch( f"{MODULE_NAME}.update_parameters_with_data", side_effect=[ParameterError(), Parameters(), ParameterError()] @@ -571,9 +535,6 @@ def test_retries_failing_parameters_while_making_progress(self, mocker, config): def test_build_schemas(mocker, config): - from openapi_python_client.parser.properties import Schemas, build_schemas - from openapi_python_client.schema import Reference, Schema - create_schemas = mocker.patch(f"{MODULE_NAME}._create_schemas") process_models = mocker.patch(f"{MODULE_NAME}._process_models") diff --git a/tests/test_parser/test_properties/test_model_property.py b/tests/test_parser/test_properties/test_model_property.py index a51fd984b..85ab3389a 100644 --- a/tests/test_parser/test_properties/test_model_property.py +++ b/tests/test_parser/test_properties/test_model_property.py @@ -5,8 +5,13 @@ import openapi_python_client.schema as oai from openapi_python_client.parser.errors import PropertyError -from openapi_python_client.parser.properties import Schemas, StringProperty -from openapi_python_client.parser.properties.model_property import ANY_ADDITIONAL_PROPERTY, _process_properties +from openapi_python_client.parser.properties import Class, ModelProperty, Schemas, StringProperty +from openapi_python_client.parser.properties.model_property import ( + ANY_ADDITIONAL_PROPERTY, + _process_properties, + _PropertyData, + process_model, +) MODULE_NAME = "openapi_python_client.parser.properties.model_property" @@ -87,8 +92,6 @@ class TestBuild: ], ) def test_additional_schemas(self, additional_properties_schema, expected_additional_properties, config): - from openapi_python_client.parser.properties import ModelProperty, Schemas - data = oai.Schema.model_construct( additionalProperties=additional_properties_schema, ) @@ -107,8 +110,6 @@ def test_additional_schemas(self, additional_properties_schema, expected_additio assert model.additional_properties == expected_additional_properties def test_happy_path(self, model_property_factory, string_property_factory, date_time_property_factory, config): - from openapi_python_client.parser.properties import Class, ModelProperty, Schemas - name = "prop" required = True @@ -166,8 +167,6 @@ def test_happy_path(self, model_property_factory, string_property_factory, date_ ) def test_model_name_conflict(self, config): - from openapi_python_client.parser.properties import ModelProperty - data = oai.Schema.model_construct() schemas = Schemas(classes_by_name={"OtherModel": None}) @@ -213,8 +212,6 @@ def test_model_naming( expected: str, config, ): - from openapi_python_client.parser.properties import ModelProperty - data = oai.Schema( title=title, properties={}, @@ -233,8 +230,6 @@ def test_model_naming( assert result.class_info.name == expected def test_model_bad_properties(self, config): - from openapi_python_client.parser.properties import ModelProperty - data = oai.Schema( properties={ "bad": oai.Reference.model_construct(ref="#/components/schema/NotExist"), @@ -253,8 +248,6 @@ def test_model_bad_properties(self, config): assert isinstance(result, PropertyError) def test_model_bad_additional_properties(self, config): - from openapi_python_client.parser.properties import ModelProperty - additional_properties = oai.Schema( type="object", properties={ @@ -275,8 +268,6 @@ def test_model_bad_additional_properties(self, config): assert isinstance(result, PropertyError) def test_process_properties_false(self, model_property_factory, config): - from openapi_python_client.parser.properties import Class, ModelProperty - name = "prop" required = True @@ -671,9 +662,6 @@ def test_merge_inline_objects(self, model_property_factory, enum_property_factor class TestProcessModel: def test_process_model_error(self, mocker, model_property_factory, config): - from openapi_python_client.parser.properties import Schemas - from openapi_python_client.parser.properties.model_property import process_model - model_prop = model_property_factory() schemas = Schemas() process_property_data = mocker.patch(f"{MODULE_NAME}._process_property_data") @@ -688,9 +676,6 @@ def test_process_model_error(self, mocker, model_property_factory, config): assert model_prop.additional_properties is None def test_process_model(self, mocker, model_property_factory, config): - from openapi_python_client.parser.properties import Schemas - from openapi_python_client.parser.properties.model_property import _PropertyData, process_model - model_prop = model_property_factory() schemas = Schemas() property_data = _PropertyData( @@ -715,8 +700,6 @@ def test_process_model(self, mocker, model_property_factory, config): def test_set_relative_imports(model_property_factory): - from openapi_python_client.parser.properties import Class - class_info = Class("ClassName", module_name="module_name") relative_imports = {f"from ..models.{class_info.module_name} import {class_info.name}"} diff --git a/tests/test_parser/test_properties/test_protocol.py b/tests/test_parser/test_properties/test_protocol.py index 1d4111750..800aa69e4 100644 --- a/tests/test_parser/test_properties/test_protocol.py +++ b/tests/test_parser/test_properties/test_protocol.py @@ -2,6 +2,7 @@ import pytest +from openapi_python_client.parser.properties import AnyProperty from openapi_python_client.parser.properties.protocol import Value @@ -23,8 +24,6 @@ def test_is_base_type(any_property_factory): ], ) def test_get_type_string(any_property_factory, mocker, required, no_optional, json, expected, quoted): - from openapi_python_client.parser.properties import AnyProperty - mocker.patch.object(AnyProperty, "_type_string", "TestType") mocker.patch.object(AnyProperty, "_json_type_string", "str") p = any_property_factory(required=required) @@ -65,8 +64,6 @@ def test_get_imports(any_property_factory): ], ) def test_get_base_type_string(quoted, expected, any_property_factory, mocker): - from openapi_python_client.parser.properties import AnyProperty - mocker.patch.object(AnyProperty, "_type_string", "TestType") p = any_property_factory() assert p.get_base_type_string(quoted=quoted) is expected @@ -80,8 +77,6 @@ def test_get_base_type_string(quoted, expected, any_property_factory, mocker): ], ) def test_get_base_json_type_string(quoted, expected, any_property_factory, mocker): - from openapi_python_client.parser.properties import AnyProperty - mocker.patch.object(AnyProperty, "_json_type_string", "str") p = any_property_factory() assert p.get_base_json_type_string(quoted=quoted) is expected diff --git a/tests/test_parser/test_properties/test_schemas.py b/tests/test_parser/test_properties/test_schemas.py index 5560795cf..7e7af8514 100644 --- a/tests/test_parser/test_properties/test_schemas.py +++ b/tests/test_parser/test_properties/test_schemas.py @@ -1,18 +1,21 @@ import pytest from attr import evolve +from openapi_python_client.config import ClassOverride from openapi_python_client.parser.errors import ParameterError from openapi_python_client.parser.properties import Class, Parameters -from openapi_python_client.parser.properties.schemas import parameter_from_reference -from openapi_python_client.schema import Parameter, Reference +from openapi_python_client.parser.properties.schemas import ( + parameter_from_data, + parameter_from_reference, + update_parameters_with_data, +) +from openapi_python_client.schema import Parameter, ParameterLocation, Reference, Schema from openapi_python_client.utils import ClassName MODULE_NAME = "openapi_python_client.parser.properties.schemas" def test_class_from_string_default_config(config): - from openapi_python_client.parser.properties import Class - class_ = Class.from_string(string="#/components/schemas/PingResponse", config=config) assert class_.name == "PingResponse" @@ -29,9 +32,6 @@ def test_class_from_string_default_config(config): ), ) def test_class_from_string(class_override, module_override, expected_class, expected_module, config): - from openapi_python_client.config import ClassOverride - from openapi_python_client.parser.properties import Class - ref = "#/components/schemas/MyResponse" config = evolve( config, class_overrides={"MyResponse": ClassOverride(class_name=class_override, module_name=module_override)} @@ -44,9 +44,6 @@ def test_class_from_string(class_override, module_override, expected_class, expe class TestParameterFromData: def test_cannot_parse_parameters_by_reference(self, config): - from openapi_python_client.parser.properties import Parameters - from openapi_python_client.parser.properties.schemas import parameter_from_data - ref = Reference.model_construct(ref="#/components/parameters/a_param") parameters = Parameters() param_or_error, new_parameters = parameter_from_data( @@ -56,10 +53,6 @@ def test_cannot_parse_parameters_by_reference(self, config): assert new_parameters == parameters def test_parameters_without_schema_are_ignored(self, config): - from openapi_python_client.parser.properties import Parameters - from openapi_python_client.parser.properties.schemas import parameter_from_data - from openapi_python_client.schema import ParameterLocation - param = Parameter(name="a_schemaless_param", param_in=ParameterLocation.QUERY) parameters = Parameters() param_or_error, new_parameters = parameter_from_data( @@ -69,10 +62,6 @@ def test_parameters_without_schema_are_ignored(self, config): assert new_parameters == parameters def test_registers_new_parameters(self, config): - from openapi_python_client.parser.properties import Parameters - from openapi_python_client.parser.properties.schemas import parameter_from_data - from openapi_python_client.schema import ParameterLocation, Schema - param = Parameter.model_construct( name="a_param", param_in=ParameterLocation.QUERY, param_schema=Schema.model_construct() ) @@ -119,9 +108,6 @@ def test_returns_reference_from_registry(self): class TestUpdateParametersFromData: def test_reports_parameters_with_errors(self, mocker, config): - from openapi_python_client.parser.properties.schemas import update_parameters_with_data - from openapi_python_client.schema import ParameterLocation, Schema - parameters = Parameters() param = Parameter.model_construct( name="a_param", param_in=ParameterLocation.QUERY, param_schema=Schema.model_construct() @@ -141,9 +127,6 @@ def test_reports_parameters_with_errors(self, mocker, config): ) def test_records_references_to_parameters(self, mocker, config): - from openapi_python_client.parser.properties.schemas import update_parameters_with_data - from openapi_python_client.schema import ParameterLocation, Schema - parameters = Parameters() param = Parameter.model_construct( name="a_param", param_in=ParameterLocation.QUERY, param_schema=Schema.model_construct() diff --git a/tests/test_parser/test_responses.py b/tests/test_parser/test_responses.py index 24fb94c61..8fb04d720 100644 --- a/tests/test_parser/test_responses.py +++ b/tests/test_parser/test_responses.py @@ -3,16 +3,15 @@ import pytest import openapi_python_client.schema as oai +from openapi_python_client.parser import responses from openapi_python_client.parser.errors import ParseError, PropertyError from openapi_python_client.parser.properties import Schemas -from openapi_python_client.parser.responses import JSON_SOURCE, NONE_SOURCE +from openapi_python_client.parser.responses import JSON_SOURCE, NONE_SOURCE, Response, response_from_data MODULE_NAME = "openapi_python_client.parser.responses" def test_response_from_data_no_content(any_property_factory): - from openapi_python_client.parser.responses import Response, response_from_data - data = oai.Response.model_construct(description="") response, schemas = response_from_data( @@ -38,8 +37,6 @@ def test_response_from_data_no_content(any_property_factory): def test_response_from_data_unsupported_content_type(): - from openapi_python_client.parser.responses import response_from_data - data = oai.Response.model_construct(description="", content={"blah": None}) config = MagicMock() config.content_type_overrides = {} @@ -56,8 +53,6 @@ def test_response_from_data_unsupported_content_type(): def test_response_from_data_no_content_schema(any_property_factory): - from openapi_python_client.parser.responses import Response, response_from_data - data = oai.Response.model_construct( description="", content={"application/vnd.api+json; version=2.2": oai.MediaType.model_construct()}, @@ -87,8 +82,6 @@ def test_response_from_data_no_content_schema(any_property_factory): def test_response_from_data_property_error(mocker): - from openapi_python_client.parser import responses - property_from_data = mocker.patch.object(responses, "property_from_data", return_value=(PropertyError(), Schemas())) data = oai.Response.model_construct( description="", @@ -118,8 +111,6 @@ def test_response_from_data_property_error(mocker): def test_response_from_data_property(mocker, any_property_factory): - from openapi_python_client.parser import responses - prop = any_property_factory() property_from_data = mocker.patch.object(responses, "property_from_data", return_value=(prop, Schemas())) data = oai.Response.model_construct( @@ -155,8 +146,6 @@ def test_response_from_data_property(mocker, any_property_factory): def test_response_from_data_reference(mocker, any_property_factory): - from openapi_python_client.parser import responses - prop = any_property_factory() mocker.patch.object(responses, "property_from_data", return_value=(prop, Schemas())) predefined_response_data = oai.Response.model_construct( @@ -192,8 +181,6 @@ def test_response_from_data_reference(mocker, any_property_factory): ], ) def test_response_from_data_invalid_reference(ref_string, expected_error_string, mocker, any_property_factory): - from openapi_python_client.parser import responses - prop = any_property_factory() mocker.patch.object(responses, "property_from_data", return_value=(prop, Schemas())) predefined_response_data = oai.Response.model_construct( @@ -217,8 +204,6 @@ def test_response_from_data_invalid_reference(ref_string, expected_error_string, def test_response_from_data_ref_to_response_that_is_a_ref(mocker, any_property_factory): - from openapi_python_client.parser import responses - prop = any_property_factory() mocker.patch.object(responses, "property_from_data", return_value=(prop, Schemas())) predefined_response_base_data = oai.Response.model_construct( @@ -248,8 +233,6 @@ def test_response_from_data_ref_to_response_that_is_a_ref(mocker, any_property_f def test_response_from_data_content_type_overrides(any_property_factory): - from openapi_python_client.parser.responses import Response, response_from_data - data = oai.Response.model_construct( description="", content={"application/zip": oai.MediaType.model_construct()}, diff --git a/tests/test_schema/test_data_type.py b/tests/test_schema/test_data_type.py index 19aa256eb..0a4d9681d 100644 --- a/tests/test_schema/test_data_type.py +++ b/tests/test_schema/test_data_type.py @@ -1,3 +1,4 @@ +import pydantic import pytest import openapi_python_client.schema as oai @@ -5,8 +6,6 @@ class TestDataType: def test_schema_bad_types(self): - import pydantic - with pytest.raises(pydantic.ValidationError): oai.Schema(type="bad_type")