-
Notifications
You must be signed in to change notification settings - Fork 611
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix: Annotation is not a valid type with union field enum type
Summary: # What? The current Pyre architecture does not recognize nested type aliases as types for annotation purposes. Below is the pattern that will cause the error ``` import abc import enum # Consider a thrift file with the definition below # # union MyUnion {} # # # Defined as MyUnion in thrift_abstract_types.py # Named MyUnionAbstract for illustration. class MyUnionAbstract(abc.ABC): class FbThriftUnionFieldEnum(enum.Enum): EMPTY = 0 # Defined as MyUnion in thrift_types.py # Named MyUnionImmutable for illustration. class MyUnionImmutable(MyUnionAbstract): Type = MyUnionAbstract.FbThriftUnionFieldEnum # The line below is the one with the failure my_var: MyUnionImmutable.Type = MyUnionImmutable.Type.EMPTY ``` This results in the error ``` 22,8: Undefined or invalid type [11]: Annotation `MyUnionImmutable.Type` is not defined as a type. ``` # Why? Pyre does not recognize nested type aliases as a type. # What to do? Change the implementation as follows: ``` import abc import enum # Consider a thrift file with the definition below # # union MyUnion {} # # # Defined as MyUnion in thrift_abstract_types.py # Named MyUnionAbstract for illustration. class MyUnionAbstract(abc.ABC): class FbThriftUnionFieldEnum(enum.Enum): EMPTY = 0 # Defined as MyUnion in thrift_types.py # Named MyUnionImmutable for illustration. class MyUnionImmutable(MyUnionAbstract): class Type(enum.Enum): EMPTY = 0 class FbThriftUnionFieldEnum(enum.Enum): EMPTY = 0 # This line no longer fails my_var: MyUnionImmutable.Type = MyUnionImmutable.Type.EMPTY ``` For the sake of completeness, Mutable types look like below: ``` # Defined as MyUnion in thrift_mutable_types.py # Named MyUnionMutable for illustration. class MyUnionMutable(MyUnionAbstract): class FbThriftUnionFieldEnum(enum.Enum): EMPTY = 0 my_mutable_var: MyUnionMutable.FbThriftUnionFieldEnum = MyUnionMutable.FbThriftUnionFieldEnum.EMPTY ``` Reviewed By: ahilger Differential Revision: D66786497 fbshipit-source-id: 302510ec09bc5ac33357dee8572b13dcb494ea40
- Loading branch information
1 parent
8457ed2
commit e76a862
Showing
39 changed files
with
667 additions
and
176 deletions.
There are no files selected for viewing
41 changes: 41 additions & 0 deletions
41
thrift/compiler/generate/templates/python/structs/maybe_union_field_and_value.mustache
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,41 @@ | ||
{{! | ||
Copyright (c) Meta Platforms, Inc. and affiliates. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
}}{{! | ||
Generate union field and value related items. | ||
}} | ||
{{! | ||
Generate the FbThriftUnionFieldEnum: | ||
For Immutable: | ||
abstract enabled OR mutable enabled | ||
For Mutable: | ||
always | ||
}} | ||
{{#program:generate_immutable_types}} | ||
{{#program:enable_abstract_types?}} | ||
{{> structs/union_field_and_value}} | ||
{{/program:enable_abstract_types?}} | ||
{{^program:enable_abstract_types?}} | ||
{{#program:generate_to_mutable_python_conversion_methods?}} | ||
{{> structs/union_field_and_value}} | ||
{{/program:generate_to_mutable_python_conversion_methods?}} | ||
{{/program:enable_abstract_types?}} | ||
{{/program:generate_immutable_types}} | ||
{{#program:generate_mutable_types}} | ||
{{> structs/union_field_and_value}} | ||
{{/program:generate_mutable_types}} |
25 changes: 25 additions & 0 deletions
25
thrift/compiler/generate/templates/python/structs/union_field_and_value.mustache
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,25 @@ | ||
{{! | ||
Copyright (c) Meta Platforms, Inc. and affiliates. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
}} | ||
class FbThriftUnionFieldEnum(enum.Enum): | ||
EMPTY: {{> structs/unadapted_name}}.FbThriftUnionFieldEnum = ... | ||
{{#struct:fields_ordered_by_id}} | ||
{{field:py_name}}: {{> structs/unadapted_name}}.FbThriftUnionFieldEnum = ... | ||
{{/struct:fields_ordered_by_id}} | ||
|
||
fbthrift_current_value: _typing.Final[{{> types/field_value_pep484_union_type}}] | ||
fbthrift_current_field: _typing.Final[FbThriftUnionFieldEnum] |
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
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
Oops, something went wrong.