Repository navigation
add lazy model generation #863
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
beabc3d
add lazy model generation
stobrien89 ec1c6fd
test streaming blob model generation
stobrien89 2cbfa44
support event stream unions in model generation
stobrien89 005fe7c
exclude eventstream members from generated structs
stobrien89 d83c5f6
filter event stream members from generated stubs
stobrien89 52d3dc6
filter event streams from generated typecheck fixtures
stobrien89 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,8 @@ | ||
| { | ||
| "files": { | ||
| "src/example_types/_private/schemas.py": "d0f35e8979c4aa9d49a1d9c0c8efeb860420bc0ceab3ba3edcc9488c053e9110" | ||
| "src/example_types/_private/schemas.py": "1d708d275049f3f4a86b343c3f38104ef4a7e5bf749a27cd1a490c95c8f9db59", | ||
| "src/example_types/models.py": "51aadd47e9971f2b44fabd47e81e4b04d7156aa61b64454b39f4b6d47603ab45", | ||
| "src/example_types/models.pyi": "31c14804662d43c11fc6c61d0a49244e5e606b65aadb774df913f6a26e1b135a" | ||
| }, | ||
| "version": 1 | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| from __future__ import annotations | ||
|
|
||
| from smithy_core._dynamic_models import ModelRegistry | ||
| from smithy_core.exceptions import ModeledError | ||
|
|
||
| from ._private import schemas | ||
|
|
||
|
|
||
| class ServiceError(ModeledError): | ||
| """ | ||
| Base error for all errors in the service. | ||
|
|
||
| Some exceptions do not extend from this class, including synthetic, | ||
| implicit, and shared exception types. | ||
| """ | ||
|
|
||
|
|
||
| _MODEL_INDEX = { | ||
| "GetExampleInput": "GET_EXAMPLE_INPUT", | ||
| "GetExampleOutput": "GET_EXAMPLE_OUTPUT", | ||
| "ExampleError": "EXAMPLE_ERROR", | ||
| "RenamedExample": "RENAMED_EXAMPLE", | ||
| "ExampleValue": "EXAMPLE_VALUE", | ||
| "ExampleValueText": "EXAMPLE_VALUE", | ||
| "ExampleValueNumber": "EXAMPLE_VALUE", | ||
| "ExampleValueEmpty": "EXAMPLE_VALUE", | ||
| "ExampleValueUnknown": "EXAMPLE_VALUE", | ||
| "EventStream": "EVENT_STREAM", | ||
| "EventStreamExample": "EVENT_STREAM", | ||
| "EventStreamUnknown": "EVENT_STREAM", | ||
| "Status": "STATUS", | ||
| "Code": "CODE", | ||
| } | ||
|
|
||
| _RENAMES = { | ||
| "RenamedExample": "RENAMED_EXAMPLE", | ||
| "ExampleValue": "EXAMPLE_VALUE", | ||
| } | ||
|
|
||
| _TARGET_NAMES = { | ||
| "example#Example": "RenamedExample", | ||
| "example#Value": "ExampleValue", | ||
| } | ||
|
|
||
| _MODEL_NAMES = (*tuple(_MODEL_INDEX), "Unit") | ||
| __all__ = ("ServiceError", *_MODEL_NAMES) | ||
|
|
||
|
|
||
| _registry = ModelRegistry( | ||
| schemas, | ||
| renames=_RENAMES, | ||
| index=_MODEL_INDEX, | ||
| target_names=_TARGET_NAMES, | ||
| shape_names=frozenset( | ||
| { | ||
| "Code", | ||
| "EventStream", | ||
| "ExampleError", | ||
| "ExampleValue", | ||
| "GetExampleInput", | ||
| "GetExampleOutput", | ||
| "RenamedExample", | ||
| "Status", | ||
| "Unit", | ||
| } | ||
| ), | ||
| service_error="ServiceError", | ||
| ) | ||
| del schemas, ModelRegistry, ModeledError | ||
|
|
||
|
|
||
| def __getattr__(name: str): | ||
| value = _registry.build(name) | ||
| globals()[name] = value | ||
| return value | ||
|
|
||
|
|
||
| def __dir__() -> list[str]: | ||
| return sorted({*globals(), *__all__}) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,160 @@ | ||
| from __future__ import annotations | ||
|
|
||
| from dataclasses import dataclass | ||
| from enum import IntEnum, StrEnum | ||
| from typing import Self | ||
|
|
||
| from smithy_core.deserializers import ShapeDeserializer | ||
| from smithy_core.documents import Document | ||
| from smithy_core.exceptions import ModeledError | ||
| from smithy_core.response import ResponseMetadata | ||
| from smithy_core.serializers import ShapeSerializer | ||
| from smithy_core.types import UnknownEnumMixin | ||
|
|
||
| __all__ = [ | ||
| "Code", | ||
| "EventStream", | ||
| "EventStreamExample", | ||
| "EventStreamUnknown", | ||
| "ExampleError", | ||
| "ExampleValue", | ||
| "ExampleValueEmpty", | ||
| "ExampleValueNumber", | ||
| "ExampleValueText", | ||
| "ExampleValueUnknown", | ||
| "GetExampleInput", | ||
| "GetExampleOutput", | ||
| "RenamedExample", | ||
| "ServiceError", | ||
| "Status", | ||
| "Unit", | ||
| ] | ||
|
|
||
| class _SerdeShape: | ||
| def serialize(self, serializer: ShapeSerializer) -> None: ... | ||
| def serialize_members(self, serializer: ShapeSerializer) -> None: ... | ||
| @classmethod | ||
| def deserialize(cls, deserializer: ShapeDeserializer) -> Self: ... | ||
|
|
||
| class ServiceError(ModeledError): | ||
| """ | ||
| Base error for all errors in the service. | ||
|
|
||
| Some exceptions do not extend from this class, including synthetic, | ||
| implicit, and shared exception types. | ||
| """ | ||
|
|
||
| @dataclass(kw_only=True) | ||
| class GetExampleInput(_SerdeShape): | ||
| "Input for retrieving an example." | ||
|
|
||
| id: str | None = ... | ||
|
|
||
| @dataclass(kw_only=True) | ||
| class GetExampleOutput(_SerdeShape): | ||
| "Dataclass for GetExampleOutput structure." | ||
|
|
||
| example: RenamedExample | None = ... | ||
| value: ExampleValue | None = ... | ||
| response_metadata: ResponseMetadata = ... | ||
| """ | ||
| Metadata about the response that produced this output. | ||
|
|
||
| Use this to recover the request identifiers a service's support team | ||
| needs in order to investigate a call. Members of the metadata are | ||
| individually optional. | ||
| """ | ||
|
|
||
| @dataclass(kw_only=True) | ||
| class ExampleError(ServiceError, _SerdeShape): | ||
| "An example modeled error." | ||
|
|
||
| retry_after_seconds: int | None = ... | ||
| response_metadata: ResponseMetadata = ... | ||
| """ | ||
| Metadata about the response that produced this output. | ||
|
|
||
| Use this to recover the request identifiers a service's support team | ||
| needs in order to investigate a call. Members of the metadata are | ||
| individually optional. | ||
| """ | ||
|
|
||
| @dataclass(kw_only=True) | ||
| class RenamedExample(_SerdeShape): | ||
| "A representative generated structure." | ||
|
|
||
| name: str | ||
| "The example name." | ||
| count: int | None = ... | ||
| tags: list[str] | None = ... | ||
| attributes: dict[str, Document] | None = ... | ||
| choice: ExampleValue | None = ... | ||
| status: str | None = ... | ||
| code: int | None = ... | ||
|
|
||
| @dataclass | ||
| class ExampleValueText(_SerdeShape): | ||
| value: str | ||
|
|
||
| @dataclass | ||
| class ExampleValueNumber(_SerdeShape): | ||
| value: int | ||
|
|
||
| @dataclass | ||
| class ExampleValueEmpty(_SerdeShape): | ||
| value: Unit | ||
|
|
||
| @dataclass | ||
| class ExampleValueUnknown(_SerdeShape): | ||
| """ | ||
| Represents an unknown variant. | ||
|
|
||
| If you receive this value, you will need to update your library to | ||
| receive the parsed value. | ||
|
|
||
| This value may not be deliberately sent. | ||
| """ | ||
|
|
||
| tag: str | ||
|
|
||
| ExampleValue = ( | ||
| ExampleValueText | ExampleValueNumber | ExampleValueEmpty | ExampleValueUnknown | ||
| ) | ||
| "A representative generated union." | ||
|
|
||
| class _ExampleValueDeserializer: | ||
| def deserialize(self, deserializer: ShapeDeserializer) -> ExampleValue: ... | ||
|
|
||
| @dataclass | ||
| class EventStreamExample(_SerdeShape): | ||
| value: RenamedExample | ||
|
|
||
| @dataclass | ||
| class EventStreamUnknown(_SerdeShape): | ||
| """ | ||
| Represents an unknown variant. | ||
|
|
||
| If you receive this value, you will need to update your library to | ||
| receive the parsed value. | ||
|
|
||
| This value may not be deliberately sent. | ||
| """ | ||
|
|
||
| tag: str | ||
|
|
||
| EventStream = EventStreamExample | EventStreamUnknown | ||
|
|
||
| class _EventStreamDeserializer: | ||
| def deserialize(self, deserializer: ShapeDeserializer) -> EventStream: ... | ||
|
|
||
| class Status(UnknownEnumMixin, StrEnum): | ||
| ACTIVE = "active" | ||
| INACTIVE = "inactive" | ||
|
|
||
| class Code(UnknownEnumMixin, IntEnum): | ||
| OK = 200 | ||
| NOT_FOUND = 404 | ||
|
|
||
| @dataclass(kw_only=True) | ||
| class Unit(_SerdeShape): | ||
| pass |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what is this needed for? Are we applying it unconditionally to every file?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah,
PythonWriteradds it to every generated module. it keeps forward and recursive type annotations from being evaluated at import time