-
Notifications
You must be signed in to change notification settings - Fork 99
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
Add Typeshare support for Python #169
Open
hculea
wants to merge
60
commits into
1Password:main
Choose a base branch
from
hculea:hculea/add-python-support-in-typeshare
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 54 commits
Commits
Show all changes
60 commits
Select commit
Hold shift + click to select a range
90f7537
Add experimental typeshare support for Python
hculea ee5673c
Resolve clippy errors
hculea 78bca98
Resolve merge conflicts with latest main
hculea dfd6522
Resolve merge conflicts with latest main
hculea 0c14914
Add python as an option for the --lang flag
hculea 439783b
Add support for todo and priorly unsupported types
hculea d7f12d0
Leverage mutable self to remove RefCell around Module
hculea 2039818
Fix ParsedRusthThing typo
hculea 48a8adb
Merge branch 'main' into hculea/add-python-support-in-typeshare
MOmarMiraj 1427223
Merge branch '1Password:main' into hculea/add-python-support-in-types…
AndyTitu c9f4be4
Use serialization alias in renaming fields in Python
hculea ad6645a
Merge branch '1Password:main' into hculea/add-python-support-in-types…
hculea 459f596
Fix python serializing and deserialzing using pydantic Field aliasing…
AndyTitu fde8907
Fix indentation of writing config dict
AndyTitu ead7d3a
added None default to Optional python type
amandayu1 a74e4ce
Updated None default condition
amandayu1 9b5c4be
fixing optional field set to default
amandayu1 628608d
add fix for missing fields
amandayu1 8519dea
added fix
amandayu1 6786aad
changed fix
amandayu1 2d0e627
testing new approach
amandayu1 169b380
Merge pull request #1 from hculea/optional-fields-default-to-none
amandayu1 e6bea87
added clarifying comment for None default
amandayu1 d51d903
set optional field to None by default
amandayu1 aa2213d
Bring Python fork up to date with main
hculea eeaa0d1
Run cargo fmt
hculea e79b7b6
fix duplicated " = None = None"
CheatCod d98a3e2
fix duplicate
CheatCod 677cf13
add tests
CheatCod df4a611
add comments to test
CheatCod 77b7239
minor formatting
CheatCod b3536c8
Remove level of indentation of type alias on comments
MOmarMiraj 45e9e5f
Refactor Python Codegen to correctly top sort file and use typeshare …
MOmarMiraj c8026b0
Add Feature Flag for Python TS (#3)
MOmarMiraj e419246
Draft: Change tagged enum generation in python (#2)
CheatCod 53b25db
Merge remote-tracking branch 'upstream/main' into hculea/add-python-s…
MOmarMiraj fb666ca
Add python support and update cargo lock
MOmarMiraj 40206a7
fmt fix and fix rust doc
MOmarMiraj 7184b52
update test cases
MOmarMiraj 3d447df
Add mention of Python support in README
hculea a251db1
remove trailing comma
MOmarMiraj 03d600b
Merge branch 'hculea/add-python-support-in-typeshare' of github.com:h…
MOmarMiraj 61b6afd
Update unit enum to use enum values
MOmarMiraj 9810e7b
fmt fix
MOmarMiraj 92f6922
Remove config dict and allow unit enums to be serializble
MOmarMiraj 0f89783
fix tuple variant generation
CheatCod 90793c7
dedup union
CheatCod 40803c4
better error handling
CheatCod 23ecf7f
Merge pull request #4 from hculea/peter/fix-tuple-variant-generation
CheatCod ce97007
remove once-cell; use index-set
CheatCod 260fa75
update tests
CheatCod d4fabb6
add comments on unused function for generics
CheatCod 83001f7
refactor how union works. Note this requires the struct to be externa…
CheatCod 6afd25a
proper rename
CheatCod 0c9b8ac
use literal for tag keys
CheatCod a10d3e7
generate comments properly
CheatCod 3df3ece
fix type aliasing issue
CheatCod 533c75f
remove unneeded newlines
CheatCod 4a0bb0e
fix to confrom python's wonderful doc conventions
CheatCod a49914a
use enum for type
CheatCod 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
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains 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 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 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 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 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 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 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 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 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,57 @@ | ||
""" | ||
Generated by typeshare 1.12.0 | ||
""" | ||
from __future__ import annotations | ||
|
||
from enum import Enum | ||
from pydantic import BaseModel, ConfigDict, Field | ||
from typing import List, Union | ||
|
||
|
||
class AnonymousStructWithRenameListInner(BaseModel): | ||
""" | ||
Generated type representing the anonymous struct variant `List` of the `AnonymousStructWithRename` Rust enum | ||
""" | ||
list: List[str] | ||
|
||
|
||
class AnonymousStructWithRenameLongFieldNamesInner(BaseModel): | ||
""" | ||
Generated type representing the anonymous struct variant `LongFieldNames` of the `AnonymousStructWithRename` Rust enum | ||
""" | ||
model_config = ConfigDict(populate_by_name=True) | ||
|
||
some_long_field_name: str | ||
and_: bool = Field(alias="and") | ||
but_one_more: List[str] | ||
|
||
|
||
class AnonymousStructWithRenameKebabCaseInner(BaseModel): | ||
""" | ||
Generated type representing the anonymous struct variant `KebabCase` of the `AnonymousStructWithRename` Rust enum | ||
""" | ||
model_config = ConfigDict(populate_by_name=True) | ||
|
||
another_list: List[str] = Field(alias="another-list") | ||
camel_case_string_field: str = Field(alias="camelCaseStringField") | ||
something_else: bool = Field(alias="something-else") | ||
|
||
|
||
class AnonymousStructWithRenameTypes(str, Enum): | ||
LIST = "list" | ||
LONG_FIELD_NAMES = "longFieldNames" | ||
KEBAB_CASE = "kebabCase" | ||
|
||
class AnonymousStructWithRenameList(BaseModel): | ||
type: AnonymousStructWithRenameTypes = AnonymousStructWithRenameTypes.LIST | ||
content: AnonymousStructWithRenameListInner | ||
|
||
class AnonymousStructWithRenameLongFieldNames(BaseModel): | ||
type: AnonymousStructWithRenameTypes = AnonymousStructWithRenameTypes.LONG_FIELD_NAMES | ||
content: AnonymousStructWithRenameLongFieldNamesInner | ||
|
||
class AnonymousStructWithRenameKebabCase(BaseModel): | ||
type: AnonymousStructWithRenameTypes = AnonymousStructWithRenameTypes.KEBAB_CASE | ||
content: AnonymousStructWithRenameKebabCaseInner | ||
|
||
AnonymousStructWithRename = Union[AnonymousStructWithRenameList, AnonymousStructWithRenameLongFieldNames, AnonymousStructWithRenameKebabCase] |
This file contains 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,47 @@ | ||
""" | ||
Generated by typeshare 1.12.0 | ||
""" | ||
from __future__ import annotations | ||
|
||
from enum import Enum | ||
from pydantic import BaseModel | ||
from typing import Dict, List, Union | ||
|
||
|
||
class ItemDetailsFieldValue(BaseModel): | ||
hello: str | ||
|
||
|
||
class AdvancedColorsTypes(str, Enum): | ||
STRING = "String" | ||
NUMBER = "Number" | ||
NUMBER_ARRAY = "NumberArray" | ||
REALLY_COOL_TYPE = "ReallyCoolType" | ||
ARRAY_REALLY_COOL_TYPE = "ArrayReallyCoolType" | ||
DICTIONARY_REALLY_COOL_TYPE = "DictionaryReallyCoolType" | ||
|
||
class AdvancedColorsString(BaseModel): | ||
t: AdvancedColorsTypes = AdvancedColorsTypes.STRING | ||
c: str | ||
|
||
class AdvancedColorsNumber(BaseModel): | ||
t: AdvancedColorsTypes = AdvancedColorsTypes.NUMBER | ||
c: int | ||
|
||
class AdvancedColorsNumberArray(BaseModel): | ||
t: AdvancedColorsTypes = AdvancedColorsTypes.NUMBER_ARRAY | ||
c: List[int] | ||
|
||
class AdvancedColorsReallyCoolType(BaseModel): | ||
t: AdvancedColorsTypes = AdvancedColorsTypes.REALLY_COOL_TYPE | ||
c: ItemDetailsFieldValue | ||
|
||
class AdvancedColorsArrayReallyCoolType(BaseModel): | ||
t: AdvancedColorsTypes = AdvancedColorsTypes.ARRAY_REALLY_COOL_TYPE | ||
c: List[ItemDetailsFieldValue] | ||
|
||
class AdvancedColorsDictionaryReallyCoolType(BaseModel): | ||
t: AdvancedColorsTypes = AdvancedColorsTypes.DICTIONARY_REALLY_COOL_TYPE | ||
c: Dict[str, ItemDetailsFieldValue] | ||
|
||
AdvancedColors = Union[AdvancedColorsString, AdvancedColorsNumber, AdvancedColorsNumberArray, AdvancedColorsReallyCoolType, AdvancedColorsArrayReallyCoolType, AdvancedColorsDictionaryReallyCoolType] |
This file contains 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,67 @@ | ||
""" | ||
Generated by typeshare 1.12.0 | ||
""" | ||
from __future__ import annotations | ||
|
||
from enum import Enum | ||
from pydantic import BaseModel | ||
from typing import List, Union | ||
|
||
|
||
class ItemDetailsFieldValue(BaseModel): | ||
""" | ||
Struct comment | ||
""" | ||
pass | ||
|
||
class AdvancedColorsTypes(str, Enum): | ||
STRING = "String" | ||
NUMBER = "Number" | ||
UNSIGNED_NUMBER = "UnsignedNumber" | ||
NUMBER_ARRAY = "NumberArray" | ||
REALLY_COOL_TYPE = "ReallyCoolType" | ||
|
||
class AdvancedColorsString(BaseModel): | ||
type: AdvancedColorsTypes = AdvancedColorsTypes.STRING | ||
content: str | ||
|
||
class AdvancedColorsNumber(BaseModel): | ||
type: AdvancedColorsTypes = AdvancedColorsTypes.NUMBER | ||
content: int | ||
|
||
class AdvancedColorsUnsignedNumber(BaseModel): | ||
type: AdvancedColorsTypes = AdvancedColorsTypes.UNSIGNED_NUMBER | ||
content: int | ||
|
||
class AdvancedColorsNumberArray(BaseModel): | ||
type: AdvancedColorsTypes = AdvancedColorsTypes.NUMBER_ARRAY | ||
content: List[int] | ||
|
||
class AdvancedColorsReallyCoolType(BaseModel): | ||
type: AdvancedColorsTypes = AdvancedColorsTypes.REALLY_COOL_TYPE | ||
content: ItemDetailsFieldValue | ||
|
||
AdvancedColors = Union[AdvancedColorsString, AdvancedColorsNumber, AdvancedColorsUnsignedNumber, AdvancedColorsNumberArray, AdvancedColorsReallyCoolType] | ||
class AdvancedColors2Types(str, Enum): | ||
STRING = "string" | ||
NUMBER = "number" | ||
NUMBER_ARRAY = "number-array" | ||
REALLY_COOL_TYPE = "really-cool-type" | ||
|
||
class AdvancedColors2String(BaseModel): | ||
type: AdvancedColors2Types = AdvancedColors2Types.STRING | ||
content: str | ||
|
||
class AdvancedColors2Number(BaseModel): | ||
type: AdvancedColors2Types = AdvancedColors2Types.NUMBER | ||
content: int | ||
|
||
class AdvancedColors2NumberArray(BaseModel): | ||
type: AdvancedColors2Types = AdvancedColors2Types.NUMBER_ARRAY | ||
content: List[int] | ||
|
||
class AdvancedColors2ReallyCoolType(BaseModel): | ||
type: AdvancedColors2Types = AdvancedColors2Types.REALLY_COOL_TYPE | ||
content: ItemDetailsFieldValue | ||
|
||
AdvancedColors2 = Union[AdvancedColors2String, AdvancedColors2Number, AdvancedColors2NumberArray, AdvancedColors2ReallyCoolType] |
22 changes: 22 additions & 0 deletions
22
core/data/tests/can_generate_algebraic_enum_with_skipped_variants/output.py
This file contains 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,22 @@ | ||
""" | ||
Generated by typeshare 1.12.0 | ||
""" | ||
from __future__ import annotations | ||
|
||
from enum import Enum | ||
from pydantic import BaseModel | ||
from typing import Union | ||
|
||
|
||
class SomeEnumTypes(str, Enum): | ||
A = "A" | ||
C = "C" | ||
|
||
class SomeEnumA(BaseModel): | ||
type: SomeEnumTypes = SomeEnumTypes.A | ||
|
||
class SomeEnumC(BaseModel): | ||
type: SomeEnumTypes = SomeEnumTypes.C | ||
content: int | ||
|
||
SomeEnum = Union[SomeEnumA, SomeEnumC] |
43 changes: 43 additions & 0 deletions
43
core/data/tests/can_generate_anonymous_struct_with_skipped_fields/output.py
This file contains 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,43 @@ | ||
""" | ||
Generated by typeshare 1.12.0 | ||
""" | ||
from __future__ import annotations | ||
|
||
from enum import Enum | ||
from pydantic import BaseModel | ||
from typing import Union | ||
|
||
|
||
class AutofilledByUsInner(BaseModel): | ||
""" | ||
Generated type representing the anonymous struct variant `Us` of the `AutofilledBy` Rust enum | ||
""" | ||
uuid: str | ||
""" | ||
The UUID for the fill | ||
""" | ||
|
||
|
||
class AutofilledBySomethingElseInner(BaseModel): | ||
""" | ||
Generated type representing the anonymous struct variant `SomethingElse` of the `AutofilledBy` Rust enum | ||
""" | ||
uuid: str | ||
""" | ||
The UUID for the fill | ||
""" | ||
|
||
|
||
class AutofilledByTypes(str, Enum): | ||
US = "Us" | ||
SOMETHING_ELSE = "SomethingElse" | ||
|
||
class AutofilledByUs(BaseModel): | ||
type: AutofilledByTypes = AutofilledByTypes.US | ||
content: AutofilledByUsInner | ||
|
||
class AutofilledBySomethingElse(BaseModel): | ||
type: AutofilledByTypes = AutofilledByTypes.SOMETHING_ELSE | ||
content: AutofilledBySomethingElseInner | ||
|
||
AutofilledBy = Union[AutofilledByUs, AutofilledBySomethingElse] |
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.
It's possible I'm misunderstanding pydantic, but shouldn't this be a
Literal
parameter? I had understood this=
to just set a default value.https://docs.pydantic.dev/2.0/usage/types/unions/
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.
Fixed