Skip to content

Commit

Permalink
Use unified thrift enums with thrift-python abstract types
Browse files Browse the repository at this point in the history
Summary:
# TL;DR
Provide a single identity for enums in a thrift schema through a .thrift_enums module.

# Problem Statement
With the introduction of thrift mutable types (`.thrift_mutable_types`) and thrift abstract types (`.thrift_abstract_types`), along with current thrift types (`.thrift_types`), which are immutable  there are three different flavors of types for a given thrift schema. Enums are just enums and are neither mutable nor abstract. This implies enums should have a single identity irrespective of whether they are from `.thrift_abstract_types`,  `.thrift_types`, or `.thrift_mutable_types`.

# Solution
To capture this nature of enums, create a single identity for thrift enums via a `.thrift_enums` module and refer to that from the other python flavors.

# Current diff
This diff uses the enums from `.thrift_enums` with abstract types in `.thrift_abstract_types`.

Reviewed By: Filip-F

Differential Revision: D66421089

fbshipit-source-id: c93835067c79c52775ce20c98dafe8b0cca84537
  • Loading branch information
Satish Kumar authored and facebook-github-bot committed Nov 28, 2024
1 parent b8240bf commit 7f4509b
Show file tree
Hide file tree
Showing 66 changed files with 442 additions and 195 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,7 @@ import {{included_module_path}}.thrift_abstract_types as {{included_module_mangl
import {{module_path}}
{{/program:adapter_type_hint_modules}}

{{#program:enums}}
class {{enum:name}}:
pass

{{/program:enums}}
{{> types/import_enums}}
{{#program:structs}}

class {{> structs/unadapted_name}}({{!
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,21 @@ import {{program:base_library_package}}.types as _fbthrift_python_types
import {{program:module_path}}.thrift_metadata
{{#program:enums}}

{{!
thrift_enums.pyi already defines class _fbthrift_compatible_with_....
This is necessary in the thrift_enums.py file as well because
thrift_abstract_types.py imports this (see a generated file for reference),
and .py files only import symbols from .py files.
}}
class _fbthrift_compatible_with_{{enum:name}}:
pass


class {{enum:name}}(_fbthrift_python_types.{{!
}}{{#enum:flags?}}Flag{{/enum:flags?}}{{!
}}{{^enum:flags?}}Enum, int{{/enum:flags?}}):
}}{{^enum:flags?}}Enum, int{{/enum:flags?}}{{!
}}, _fbthrift_compatible_with_{{enum:name}}{{!
}}):
{{#enum:values}}
{{enum_value:py_name}} = {{enum_value:value}}
{{/enum:values}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,7 @@ Generated Python typestubs for Thrift enums
{{> common/auto_generated_py}}

import {{program:base_library_package}}.types as _fbthrift_python_types
{{#program:enable_abstract_types?}}
import {{program:module_path}}.thrift_abstract_types
{{/program:enable_abstract_types?}}{{!
}}{{#program:enums}}
{{#program:enums}}

class _fbthrift_compatible_with_{{enum:name}}:
pass
Expand All @@ -32,9 +29,6 @@ class _fbthrift_compatible_with_{{enum:name}}:
class {{enum:name}}(_fbthrift_python_types.{{!
}}{{#enum:flags?}}Flag{{/enum:flags?}}{{!
}}{{^enum:flags?}}Enum, int{{/enum:flags?}}{{!
}}{{#program:enable_abstract_types?}}{{!
}}, {{program:module_path}}.thrift_abstract_types.{{enum:name}}{{!
}}{{/program:enable_abstract_types?}}{{!
}}, _fbthrift_compatible_with_{{enum:name}}):
{{#enum:values}}
{{enum_value:py_name}}: {{enum:name}} = ...
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{{!
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.
}}{{!
Import enums into the modules that reference enums.
}}
{{!
from module_path.thrift_enums import *
below does not import private symbols _fbthrift_compatible_with_*.
These are referenced in the generated pyi files and, for simplicity,
need to be imported through the types modules.
}}{{#program:enums}}
from {{program:module_path}}.thrift_enums import _fbthrift_compatible_with_{{enum:name}}
{{/program:enums}}

from {{program:module_path}}.thrift_enums import *
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,6 @@ class _fbthrift_compatible_with_{{enum:name}}:
class {{enum:name}}(_fbthrift_python_types.{{!
}}{{#enum:flags?}}Flag{{/enum:flags?}}{{!
}}{{^enum:flags?}}Enum, int{{/enum:flags?}}{{!
}}{{#program:enable_abstract_types?}}{{!
}}, _fbthrift_python_abstract_types.{{enum:name}}{{!
}}{{/program:enable_abstract_types?}}{{!
}}, _fbthrift_compatible_with_{{enum:name}}):
{{#enum:values}}
{{enum_value:py_name}}: {{enum:name}} = ...
Expand All @@ -72,17 +69,8 @@ class {{enum:name}}(_fbthrift_python_types.{{!
{{/program:enums}}
{{/program:generate_immutable_types}}
{{#program:generate_mutable_types}}
{{!
from module_path.thrift_enums import *
below does not import private symbols _fbthrift_compatible_with_*.
These are referenced in the generated pyi files and, for simplicity,
need to be imported through the types modules.
}}{{#program:enums}}
from {{program:module_path}}.thrift_enums import _fbthrift_compatible_with_{{enum:name}}
{{/program:enums}}
{{> types/import_enums}}

from {{program:module_path}}.thrift_enums import *
{{/program:generate_mutable_types}}
{{#program:structs}}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@
import thrift.python.types as _fbthrift_python_types
import test.fixtures.basic.module.thrift_metadata

class MyEnum(_fbthrift_python_types.Enum, int):
class _fbthrift_compatible_with_MyEnum:
pass


class MyEnum(_fbthrift_python_types.Enum, int, _fbthrift_compatible_with_MyEnum):
MyValue1 = 0
MyValue2 = 1
@staticmethod
Expand All @@ -36,7 +40,11 @@ def _to_py3(self):
def _to_py_deprecated(self):
return self.value

class HackEnum(_fbthrift_python_types.Enum, int):
class _fbthrift_compatible_with_HackEnum:
pass


class HackEnum(_fbthrift_python_types.Enum, int, _fbthrift_compatible_with_HackEnum):
Value1 = 0
Value2 = 1
@staticmethod
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,10 @@
import folly.iobuf as _fbthrift_iobuf
import thrift.python.abstract_types as _fbthrift_python_abstract_types

class MyEnum:
pass

class HackEnum:
pass

from test.fixtures.basic.module.thrift_enums import _fbthrift_compatible_with_MyEnum
from test.fixtures.basic.module.thrift_enums import _fbthrift_compatible_with_HackEnum

from test.fixtures.basic.module.thrift_enums import *
class MyStruct(_abc.ABC):
@_fbthrift_property
@_abc.abstractmethod
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@
import thrift.python.types as _fbthrift_python_types
import test.fixtures.basic.module.thrift_metadata

class MyEnum(_fbthrift_python_types.Enum, int):
class _fbthrift_compatible_with_MyEnum:
pass


class MyEnum(_fbthrift_python_types.Enum, int, _fbthrift_compatible_with_MyEnum):
MyValue1 = 0
MyValue2 = 1
@staticmethod
Expand All @@ -36,7 +40,11 @@ def _to_py3(self):
def _to_py_deprecated(self):
return self.value

class HackEnum(_fbthrift_python_types.Enum, int):
class _fbthrift_compatible_with_HackEnum:
pass


class HackEnum(_fbthrift_python_types.Enum, int, _fbthrift_compatible_with_HackEnum):
Value1 = 0
Value2 = 1
@staticmethod
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,12 @@
from __future__ import annotations

import thrift.python.types as _fbthrift_python_types
import test.fixtures.basic.module.thrift_abstract_types

class _fbthrift_compatible_with_MyEnum:
pass


class MyEnum(_fbthrift_python_types.Enum, int, test.fixtures.basic.module.thrift_abstract_types.MyEnum, _fbthrift_compatible_with_MyEnum):
class MyEnum(_fbthrift_python_types.Enum, int, _fbthrift_compatible_with_MyEnum):
MyValue1: MyEnum = ...
MyValue2: MyEnum = ...
def _to_python(self) -> MyEnum: ...
Expand All @@ -25,7 +24,7 @@ class _fbthrift_compatible_with_HackEnum:
pass


class HackEnum(_fbthrift_python_types.Enum, int, test.fixtures.basic.module.thrift_abstract_types.HackEnum, _fbthrift_compatible_with_HackEnum):
class HackEnum(_fbthrift_python_types.Enum, int, _fbthrift_compatible_with_HackEnum):
Value1: HackEnum = ...
Value2: HackEnum = ...
def _to_python(self) -> HackEnum: ...
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class _fbthrift_compatible_with_MyEnum:
pass


class MyEnum(_fbthrift_python_types.Enum, int, _fbthrift_python_abstract_types.MyEnum, _fbthrift_compatible_with_MyEnum):
class MyEnum(_fbthrift_python_types.Enum, int, _fbthrift_compatible_with_MyEnum):
MyValue1: MyEnum = ...
MyValue2: MyEnum = ...
def _to_python(self) -> MyEnum: ...
Expand All @@ -31,7 +31,7 @@ class _fbthrift_compatible_with_HackEnum:
pass


class HackEnum(_fbthrift_python_types.Enum, int, _fbthrift_python_abstract_types.HackEnum, _fbthrift_compatible_with_HackEnum):
class HackEnum(_fbthrift_python_types.Enum, int, _fbthrift_compatible_with_HackEnum):
Value1: HackEnum = ...
Value2: HackEnum = ...
def _to_python(self) -> HackEnum: ...
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@
import thrift.python.types as _fbthrift_python_types
import test.fixtures.basic.module.thrift_metadata

class MyEnum(_fbthrift_python_types.Enum, int):
class _fbthrift_compatible_with_MyEnum:
pass


class MyEnum(_fbthrift_python_types.Enum, int, _fbthrift_compatible_with_MyEnum):
MyValue1 = 0
MyValue2 = 1
@staticmethod
Expand All @@ -36,7 +40,11 @@ def _to_py3(self):
def _to_py_deprecated(self):
return self.value

class HackEnum(_fbthrift_python_types.Enum, int):
class _fbthrift_compatible_with_HackEnum:
pass


class HackEnum(_fbthrift_python_types.Enum, int, _fbthrift_compatible_with_HackEnum):
Value1 = 0
Value2 = 1
@staticmethod
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@
import thrift.python.types as _fbthrift_python_types
import module.thrift_metadata

class MyEnum(_fbthrift_python_types.Enum, int):
class _fbthrift_compatible_with_MyEnum:
pass


class MyEnum(_fbthrift_python_types.Enum, int, _fbthrift_compatible_with_MyEnum):
MyValue1 = 0
MyValue2 = 1
MyValue3 = 3
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,9 @@
import folly.iobuf as _fbthrift_iobuf
import thrift.python.abstract_types as _fbthrift_python_abstract_types

class MyEnum:
pass

from module.thrift_enums import _fbthrift_compatible_with_MyEnum

from module.thrift_enums import *
class MyStructFloatFieldThrowExp(_abc.ABC):
@_fbthrift_property
@_abc.abstractmethod
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@
import thrift.python.types as _fbthrift_python_types
import module.thrift_metadata

class MyEnum(_fbthrift_python_types.Enum, int):
class _fbthrift_compatible_with_MyEnum:
pass


class MyEnum(_fbthrift_python_types.Enum, int, _fbthrift_compatible_with_MyEnum):
MyValue1 = 0
MyValue2 = 1
MyValue3 = 3
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,12 @@
from __future__ import annotations

import thrift.python.types as _fbthrift_python_types
import module.thrift_abstract_types

class _fbthrift_compatible_with_MyEnum:
pass


class MyEnum(_fbthrift_python_types.Enum, int, module.thrift_abstract_types.MyEnum, _fbthrift_compatible_with_MyEnum):
class MyEnum(_fbthrift_python_types.Enum, int, _fbthrift_compatible_with_MyEnum):
MyValue1: MyEnum = ...
MyValue2: MyEnum = ...
MyValue3: MyEnum = ...
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class _fbthrift_compatible_with_MyEnum:
pass


class MyEnum(_fbthrift_python_types.Enum, int, _fbthrift_python_abstract_types.MyEnum, _fbthrift_compatible_with_MyEnum):
class MyEnum(_fbthrift_python_types.Enum, int, _fbthrift_compatible_with_MyEnum):
MyValue1: MyEnum = ...
MyValue2: MyEnum = ...
MyValue3: MyEnum = ...
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@
import thrift.python.types as _fbthrift_python_types
import module.thrift_metadata

class MyEnum(_fbthrift_python_types.Enum, int):
class _fbthrift_compatible_with_MyEnum:
pass


class MyEnum(_fbthrift_python_types.Enum, int, _fbthrift_compatible_with_MyEnum):
MyValue1 = 0
MyValue2 = 1
MyValue3 = 3
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import thrift.python.abstract_types as _fbthrift_python_abstract_types


from module.thrift_enums import *
class ComplexUnion(_abc.ABC):
@_fbthrift_property
@_abc.abstractmethod
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,3 @@
from __future__ import annotations

import thrift.python.types as _fbthrift_python_types
import module.thrift_abstract_types
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@
import thrift.python.types as _fbthrift_python_types
import module.thrift_metadata

class EmptyEnum(_fbthrift_python_types.Enum, int):
class _fbthrift_compatible_with_EmptyEnum:
pass


class EmptyEnum(_fbthrift_python_types.Enum, int, _fbthrift_compatible_with_EmptyEnum):
@staticmethod
def __get_thrift_name__() -> str:
return "module.EmptyEnum"
Expand All @@ -34,7 +38,11 @@ def _to_py3(self):
def _to_py_deprecated(self):
return self.value

class City(_fbthrift_python_types.Enum, int):
class _fbthrift_compatible_with_City:
pass


class City(_fbthrift_python_types.Enum, int, _fbthrift_compatible_with_City):
NYC = 0
MPK = 1
SEA = 2
Expand Down Expand Up @@ -62,7 +70,11 @@ def _to_py3(self):
def _to_py_deprecated(self):
return self.value

class Company(_fbthrift_python_types.Enum, int):
class _fbthrift_compatible_with_Company:
pass


class Company(_fbthrift_python_types.Enum, int, _fbthrift_compatible_with_Company):
FACEBOOK = 0
WHATSAPP = 1
OCULUS = 2
Expand Down
Loading

0 comments on commit 7f4509b

Please sign in to comment.