Skip to content

Commit

Permalink
implement pure py set
Browse files Browse the repository at this point in the history
Summary:
Remove the `_cpp_obj` `std::shared_ptr<std::unordered_set<T>>` in `Set__X`.

This diff implement a pure python version, moving many functions that were generated cython into python. The `SetNew` base class in types.pyx will replace `Set` in the diff on top.

Reviewed By: Filip-F

Differential Revision: D64731523

fbshipit-source-id: fb2b6c3fc0cfbd5e1eab18e59a3ca26f54a1f1fb
  • Loading branch information
ahilger authored and facebook-github-bot committed Nov 10, 2024
1 parent f108ca6 commit 63f4a1f
Show file tree
Hide file tree
Showing 71 changed files with 1,929 additions and 151 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ want to include.
import thrift.py3.types
import importlib
from collections.abc import Sequence
from collections.abc import Sequence, Set
"""
This is a helper module to define py3 container types.
Expand Down Expand Up @@ -132,7 +132,78 @@ class {{type:flat_name}}(thrift.py3.types.List):


Sequence.register({{type:flat_name}})
__all__.append('{{type:flat_name}}')

{{/type:list?}}
{{/type:list?}}{{#type:set?}}
class {{type:flat_name}}(thrift.py3.types.SetNew):
{{! make the type appear to originate from .types module }}
__module__ = _fbthrift__module_name__
{{! __slots__ prevents accidental treatment as py-deprecated }}
__slots__ = ()

def __init__(self, items=None, private_ctor_token=None) -> None:
if private_ctor_token is thrift.py3.types._fbthrift_set_private_ctor:
{{!
For internal use only, when items set has been created internally
and each item has been converted from cpp so we can trust the type.
Do not use with user-exposed `set` to avoid accidental mutability.
}}
_py_obj = items
elif isinstance(items, {{type:flat_name}}):
_py_obj = frozenset(items)
elif items is None:
_py_obj = frozenset()
else:
{{#type:containerOfString?}}
if isinstance(items, str):
raise TypeError("If you really want to pass a string into a {{> types/pep484_type}} field, explicitly convert it first.")
{{/type:containerOfString?}}
check_method = {{type:flat_name}}._check_item_type_or_raise
_py_obj = frozenset(check_method(item) for item in items)

super().__init__(_py_obj, {{type:flat_name}})

@staticmethod
def _check_item_type_or_raise(item):
{{#type:set_elem_type}}
{{#type:container?}}
if item is None:
raise TypeError("None is not of the type {{> types/pep484_type}}")
if not isinstance(item, {{> types/python_type}}):
item = {{> types/python_type}}(item)
{{/type:container?}}
{{^type:container?}}
if not (
isinstance(item, {{> types/python_is_instance_type}}){{#type:enum?}} or
isinstance(item, thrift.py3.types.BadEnum){{/type:enum?}}
):
raise TypeError(f"{item!r} is not of type {{> types/pep484_type}}")
{{/type:container?}}
{{/type:set_elem_type}}
return item

{{! __contains__ doesn't raise TypeError}}
@staticmethod
def _check_item_type_or_none(item):
if item is None:
return None
{{#type:set_elem_type}}
if isinstance(item, {{> types/python_type}}):
return item
{{#type:container?}}
try:
return {{> types/python_type}}(item)
except:
return None
{{/type:container?}}
{{/type:set_elem_type}}
@staticmethod
def __get_reflection__():
return get_types_reflection().get_reflection__{{type:flat_name}}()
Set.register({{type:flat_name}})
{{/type:set?}}{{^type:map?}}
__all__.append('{{type:flat_name}}')
{{/type:map?}}
{{/program:containerTypes}}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import thrift.py3.types
import importlib
from collections.abc import Sequence
from collections.abc import Sequence, Set

"""
This is a helper module to define py3 container types.
Expand Down Expand Up @@ -67,5 +67,5 @@ def __get_reflection__():


Sequence.register(std_deque_std_string__List__string)
__all__.append('std_deque_std_string__List__string')

__all__.append('std_deque_std_string__List__string')
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import thrift.py3.types
import importlib
from collections.abc import Sequence
from collections.abc import Sequence, Set

"""
This is a helper module to define py3 container types.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import thrift.py3.types
import importlib
from collections.abc import Sequence
from collections.abc import Sequence, Set

"""
This is a helper module to define py3 container types.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import thrift.py3.types
import importlib
from collections.abc import Sequence
from collections.abc import Sequence, Set

"""
This is a helper module to define py3 container types.
Expand All @@ -27,6 +27,45 @@ def get_types_reflection():

__all__ = []

class Set__float(thrift.py3.types.SetNew):
__module__ = _fbthrift__module_name__
__slots__ = ()

def __init__(self, items=None, private_ctor_token=None) -> None:
if private_ctor_token is thrift.py3.types._fbthrift_set_private_ctor:
_py_obj = items
elif isinstance(items, Set__float):
_py_obj = frozenset(items)
elif items is None:
_py_obj = frozenset()
else:
check_method = Set__float._check_item_type_or_raise
_py_obj = frozenset(check_method(item) for item in items)

super().__init__(_py_obj, Set__float)

@staticmethod
def _check_item_type_or_raise(item):
if not (
isinstance(item, (float, int))
):
raise TypeError(f"{item!r} is not of type float")
return item

@staticmethod
def _check_item_type_or_none(item):
if item is None:
return None
if isinstance(item, float):
return item

@staticmethod
def __get_reflection__():
return get_types_reflection().get_reflection__Set__float()


Set.register(Set__float)
__all__.append('Set__float')
class List__i32(thrift.py3.types.List):
__module__ = _fbthrift__module_name__
__slots__ = ()
Expand Down Expand Up @@ -65,5 +104,46 @@ def __get_reflection__():


Sequence.register(List__i32)

__all__.append('List__i32')
class Set__string(thrift.py3.types.SetNew):
__module__ = _fbthrift__module_name__
__slots__ = ()

def __init__(self, items=None, private_ctor_token=None) -> None:
if private_ctor_token is thrift.py3.types._fbthrift_set_private_ctor:
_py_obj = items
elif isinstance(items, Set__string):
_py_obj = frozenset(items)
elif items is None:
_py_obj = frozenset()
else:
if isinstance(items, str):
raise TypeError("If you really want to pass a string into a _typing.AbstractSet[str] field, explicitly convert it first.")
check_method = Set__string._check_item_type_or_raise
_py_obj = frozenset(check_method(item) for item in items)

super().__init__(_py_obj, Set__string)

@staticmethod
def _check_item_type_or_raise(item):
if not (
isinstance(item, str)
):
raise TypeError(f"{item!r} is not of type str")
return item

@staticmethod
def _check_item_type_or_none(item):
if item is None:
return None
if isinstance(item, str):
return item

@staticmethod
def __get_reflection__():
return get_types_reflection().get_reflection__Set__string()


Set.register(Set__string)
__all__.append('Set__string')
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import thrift.py3.types
import importlib
from collections.abc import Sequence
from collections.abc import Sequence, Set

"""
This is a helper module to define py3 container types.
Expand Down Expand Up @@ -65,8 +65,8 @@ def __get_reflection__():


Sequence.register(List__i64)
__all__.append('List__i64')

__all__.append('List__i64')
class List__string(thrift.py3.types.List):
__module__ = _fbthrift__module_name__
__slots__ = ()
Expand Down Expand Up @@ -107,5 +107,5 @@ def __get_reflection__():


Sequence.register(List__string)
__all__.append('List__string')

__all__.append('List__string')
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import thrift.py3.types
import importlib
from collections.abc import Sequence
from collections.abc import Sequence, Set

"""
This is a helper module to define py3 container types.
Expand Down Expand Up @@ -65,8 +65,8 @@ def __get_reflection__():


Sequence.register(List__i32)
__all__.append('List__i32')

__all__.append('List__i32')
class List__Map__string_i32(thrift.py3.types.List):
__module__ = _fbthrift__module_name__
__slots__ = ()
Expand Down Expand Up @@ -109,8 +109,8 @@ def __get_reflection__():


Sequence.register(List__Map__string_i32)
__all__.append('List__Map__string_i32')

__all__.append('List__Map__string_i32')
class List__Range(thrift.py3.types.List):
__module__ = _fbthrift__module_name__
__slots__ = ()
Expand Down Expand Up @@ -149,8 +149,8 @@ def __get_reflection__():


Sequence.register(List__Range)
__all__.append('List__Range')

__all__.append('List__Range')
class List__Internship(thrift.py3.types.List):
__module__ = _fbthrift__module_name__
__slots__ = ()
Expand Down Expand Up @@ -189,8 +189,8 @@ def __get_reflection__():


Sequence.register(List__Internship)
__all__.append('List__Internship')

__all__.append('List__Internship')
class List__string(thrift.py3.types.List):
__module__ = _fbthrift__module_name__
__slots__ = ()
Expand Down Expand Up @@ -231,5 +231,85 @@ def __get_reflection__():


Sequence.register(List__string)

__all__.append('List__string')
class Set__i32(thrift.py3.types.SetNew):
__module__ = _fbthrift__module_name__
__slots__ = ()

def __init__(self, items=None, private_ctor_token=None) -> None:
if private_ctor_token is thrift.py3.types._fbthrift_set_private_ctor:
_py_obj = items
elif isinstance(items, Set__i32):
_py_obj = frozenset(items)
elif items is None:
_py_obj = frozenset()
else:
check_method = Set__i32._check_item_type_or_raise
_py_obj = frozenset(check_method(item) for item in items)

super().__init__(_py_obj, Set__i32)

@staticmethod
def _check_item_type_or_raise(item):
if not (
isinstance(item, int)
):
raise TypeError(f"{item!r} is not of type int")
return item

@staticmethod
def _check_item_type_or_none(item):
if item is None:
return None
if isinstance(item, int):
return item

@staticmethod
def __get_reflection__():
return get_types_reflection().get_reflection__Set__i32()


Set.register(Set__i32)
__all__.append('Set__i32')
class Set__string(thrift.py3.types.SetNew):
__module__ = _fbthrift__module_name__
__slots__ = ()

def __init__(self, items=None, private_ctor_token=None) -> None:
if private_ctor_token is thrift.py3.types._fbthrift_set_private_ctor:
_py_obj = items
elif isinstance(items, Set__string):
_py_obj = frozenset(items)
elif items is None:
_py_obj = frozenset()
else:
if isinstance(items, str):
raise TypeError("If you really want to pass a string into a _typing.AbstractSet[str] field, explicitly convert it first.")
check_method = Set__string._check_item_type_or_raise
_py_obj = frozenset(check_method(item) for item in items)

super().__init__(_py_obj, Set__string)

@staticmethod
def _check_item_type_or_raise(item):
if not (
isinstance(item, str)
):
raise TypeError(f"{item!r} is not of type str")
return item

@staticmethod
def _check_item_type_or_none(item):
if item is None:
return None
if isinstance(item, str):
return item

@staticmethod
def __get_reflection__():
return get_types_reflection().get_reflection__Set__string()


Set.register(Set__string)
__all__.append('Set__string')
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import thrift.py3.types
import importlib
from collections.abc import Sequence
from collections.abc import Sequence, Set

"""
This is a helper module to define py3 container types.
Expand Down
Loading

0 comments on commit 63f4a1f

Please sign in to comment.