Skip to content

Commit

Permalink
implement flat-name container support
Browse files Browse the repository at this point in the history
Summary:
Implement support for flat-name containers in py3 auto-migrate.

Previously, `import List__i32 from foo.types` would hard crash. Now it returns a class based loosely on `python.types.ListTypeFactory(typeinfo_i32)`. The class extends `python.types.List` and supplies the necessary `typeinfo` to properly construct `List`. It forwards the values, whether passed as `args` or `kwargs`.

The class uses a `metaclass` that implements `__instancecheck__` so that customer `isinstance(x, List__i32)` will work properly even in auto-migrate.

Reviewed By: prakashgayasen, Filip-F

Differential Revision: D66985311

fbshipit-source-id: f4aa70084676f0617daa17768069f2df85080718
  • Loading branch information
ahilger authored and facebook-github-bot committed Dec 14, 2024
1 parent 3acd126 commit 8c09d66
Show file tree
Hide file tree
Showing 10 changed files with 865 additions and 21 deletions.
9 changes: 9 additions & 0 deletions thrift/compiler/generate/t_mstch_py3_generator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -601,6 +601,8 @@ class py3_mstch_type : public mstch_type {
this,
{
{"type:modulePath", &py3_mstch_type::modulePath},
{"type:module_auto_migrate_path",
&py3_mstch_type::moduleAutoMigratePath},
{"type:cbinding_path", &py3_mstch_type::cbinding_path},
{"type:flat_name", &py3_mstch_type::flatName},
{"type:cppNamespaces", &py3_mstch_type::cppNamespaces},
Expand Down Expand Up @@ -651,6 +653,13 @@ class py3_mstch_type : public mstch_type {
get_type_py3_namespace(get_type_program(), "cbindings"), "_"));
}

mstch::node moduleAutoMigratePath() {
return fmt::format(
"_{}",
fmt::join(
get_type_py3_namespace(get_type_program(), "thrift_types"), "_"));
}

mstch::node flatName() { return cached_props_.flat_name(); }

mstch::node cppNamespaces() {
Expand Down
47 changes: 47 additions & 0 deletions thrift/compiler/generate/templates/py3/types.py.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,51 @@ want to include.
{{#program:auto_migrate?}}
{{> common/auto_generated_py}}
from {{#program:py3Namespaces}}{{value}}.{{/program:py3Namespaces}}{{program:name}}.thrift_types import *
{{#program:hasContainerTypes}}
import thrift.python.types as _fbthrift_python_types
import {{#program:py3Namespaces}}{{value}}.{{/program:py3Namespaces}}{{program:name}}.thrift_types as {{!
}}_{{#program:py3Namespaces}}{{value}}_{{/program:py3Namespaces}}{{program:name}}_thrift_types
{{#program:includeNamespaces}}
{{#hasTypes?}}
import {{#includeNamespace}}{{value}}.{{/includeNamespace}}thrift_types as _{{#includeNamespace}}{{value}}_{{/includeNamespace}}thrift_types
{{/hasTypes?}}
{{/program:includeNamespaces}}
{{/program:hasContainerTypes}}{{!
py3-container "flat name" name aliases for backwards-compatibility
}}
{{#program:containerTypes}}
{{#type:container?}}
class {{type:flat_name}}__Meta(type):
def _fbthrift_type_info(cls):
return (
{{#type:list?}}
{{#type:list_elem_type}}{{> types/typeinfo }}{{/type:list_elem_type}},
{{/type:list?}}
{{#type:set?}}
{{#type:set_elem_type}}{{> types/typeinfo }}{{/type:set_elem_type}},
{{/type:set?}}
{{#type:map?}}
{{#type:key_type}}{{> types/typeinfo }}{{/type:key_type}},
{{#type:value_type}}{{> types/typeinfo }}{{/type:value_type}},
{{/type:map?}}
)
def __instancecheck__(cls, instance):
return (
isinstance(instance, _fbthrift_python_types.{{> types/container_base}}) and
instance._fbthrift_same_type(*cls._fbthrift_type_info())
)
class {{type:flat_name}}(_fbthrift_python_types.{{> types/container_base}}, metaclass={{type:flat_name}}__Meta):
def __init__(self, *args, **kwargs):
super().__init__(
*{{type:flat_name}}._fbthrift_type_info(),
*args,
**kwargs,
)
{{/type:container?}}
{{/program:containerTypes}}
{{/program:auto_migrate?}}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{{!
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.
}}{{!
This is used only in py3 auto-migrate for the base class
of flat-name containers (List__i32)
}}{{#type:list?}}List{{/type:list?}}{{!
}}{{#type:set?}}Set{{/type:set?}}{{!
}}{{#type:map?}}Map{{/type:map?}}
42 changes: 42 additions & 0 deletions thrift/compiler/generate/templates/py3/types/typeinfo.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{{!
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.
}}{{!
This is used only in py3 auto-migrate to generate
aliases for container flat names, i.e., List__i32
}}{{#type:bool?}}_fbthrift_python_types.typeinfo_bool{{/type:bool?}}{{!
}}{{#type:byte?}}_fbthrift_python_types.typeinfo_byte{{/type:byte?}}{{!
}}{{#type:i16?}}_fbthrift_python_types.typeinfo_i16{{/type:i16?}}{{!
}}{{#type:i32?}}_fbthrift_python_types.typeinfo_i32{{/type:i32?}}{{!
}}{{#type:i64?}}_fbthrift_python_types.typeinfo_i64{{/type:i64?}}{{!
}}{{#type:double?}}_fbthrift_python_types.typeinfo_double{{/type:double?}}{{!
}}{{#type:float?}}_fbthrift_python_types.typeinfo_float{{/type:float?}}{{!
}}{{#type:string?}}_fbthrift_python_types.typeinfo_string{{/type:string?}}{{!
}}{{#type:binary?}}{{#type:iobuf?}}_fbthrift_python_types.typeinfo_iobuf{{/type:iobuf?}}{{^type:iobuf?}}_fbthrift_python_types.typeinfo_binary{{/type:iobuf?}}{{/type:binary?}}{{!
}}{{#type:struct}}_fbthrift_python_types.StructTypeInfo({{!
}}{{type:module_auto_migrate_path}}.{{struct:name}}){{!
}}{{/type:struct}}{{!
}}{{#type:list?}}_fbthrift_python_types.ListTypeInfo({{#type:list_elem_type}}{{> types/typeinfo }}{{/type:list_elem_type}}){{/type:list?}}{{!
}}{{#type:set?}}_fbthrift_python_types.SetTypeInfo({{#type:set_elem_type}}{{> types/typeinfo }}{{/type:set_elem_type}}){{/type:set?}}{{!
}}{{#type:map?}}_fbthrift_python_types.MapTypeInfo({{!
}}{{#type:key_type}}{{> types/typeinfo }}{{/type:key_type}}, {{!
}}{{#type:value_type}}{{> types/typeinfo }}{{/type:value_type}}{{!
}}){{/type:map?}}{{!
}}{{#type:enum}}_fbthrift_python_types.EnumTypeInfo({{!
}}{{type:module_auto_migrate_path}}.{{enum:name}}){{!
}}{{/type:enum}}
Loading

0 comments on commit 8c09d66

Please sign in to comment.