Skip to content

Commit

Permalink
use python Enum in types.pyi
Browse files Browse the repository at this point in the history
Summary: After aligning the `py3.types.Enum` and `python.types.Enum` type stub, remove `py3.types.Enum` and replace it with `python.types.Enum` type stub via re-export. This reflects the reality that at runtime, py3 enums now have the base class  `python.types.Enum`. And same for `Flag` and `BadEnum`.

Differential Revision: D64160660

fbshipit-source-id: 9c56e1eb2f7c15bdba8aa7ceb8663a30decf663f
  • Loading branch information
ahilger authored and facebook-github-bot committed Oct 21, 2024
1 parent 410b111 commit 5518098
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 46 deletions.
3 changes: 2 additions & 1 deletion thrift/lib/py3/test/auto_migrate/converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
# pyre-strict

import unittest
from typing import cast

import convertible.thrift_types as python_types
import convertible.ttypes as py_deprecated_types
Expand Down Expand Up @@ -199,7 +200,7 @@ def test_py_bad_enum(self) -> None:
)._to_py3()
self.assertIsInstance(simple.color, BadEnum)
self.assertEqual(
simple.color.enum,
cast(BadEnum, simple.color).enum,
py3_types.Color,
)

Expand Down
2 changes: 1 addition & 1 deletion thrift/lib/py3/test/auto_migrate/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ def test_bad_enum_in_map_items(self) -> None:
def assertBadEnum(self, e: BadEnum | Enum, cls: Type[_E], val: int) -> None:
self.assertIsInstance(e, BadEnum)
self.assertEqual(e.value, val)
self.assertEqual(e.enum, cls)
self.assertEqual(cast(BadEnum, e).enum, cls)
self.assertEqual(int(e), val)

def test_pickle(self) -> None:
Expand Down
49 changes: 7 additions & 42 deletions thrift/lib/py3/types.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,21 @@ from typing import (
Optional,
Protocol,
Sequence,
SupportsInt,
Tuple,
Type,
TypeVar,
Union as tUnion,
)

import thrift.python.types

from thrift.py3.exceptions import GeneratedError

Enum = thrift.python.types.Enum
EnumMeta = thrift.python.types.EnumMeta
Flag = thrift.python.types.Flag
BadEnum = thrift.python.types.BadEnum

_T = TypeVar("_T")
eT = TypeVar("eT", bound=Enum)

Expand Down Expand Up @@ -70,44 +76,3 @@ class Container:
class List(Container): ...
class Set(Container): ...
class Map(Container): ...

class EnumMeta(type):
def __iter__(cls: Type[_T]) -> Iterator[_T]: ...
def __reversed__(cls: Type[_T]) -> Iterator[_T]: ...
def __contains__(cls: Type[_T], item: object) -> bool: ...
def __getitem__(cls: Type[_T], name: str) -> _T: ...
def __len__(cls) -> int: ...
@property
def __members__(self: Type[_T]) -> Mapping[str, _T]: ...

class Enum(metaclass=EnumMeta):
name: Final[str]
value: Final[int]
def __getattr__(self: eT, name: str) -> eT: ...
def __init__(self: eT, value: tUnion[eT, int]) -> None: ... # __call__ for meta
def __repr__(self) -> str: ...
def __str__(self) -> str: ...
def __hash__(self) -> int: ...
def __int__(self) -> int: ...
def __index__(self) -> int: ...
def __eq__(self, other: object) -> bool: ...

class Flag(Enum):
# pyre-fixme[14]: `__contains__` overrides method defined in `EnumMeta`
# inconsistently.
def __contains__(self: eT, other: eT) -> bool: ...
def __bool__(self) -> bool: ...
# pyre-ignore[15]: This is a pyre bug ignore
def __or__(self: eT, other: eT) -> eT: ...
def __and__(self: eT, other: eT) -> eT: ...
def __xor__(self: eT, other: eT) -> eT: ...
def __invert__(self: eT, other: eT) -> eT: ...

class BadEnum(SupportsInt):
name: str
value: int
enum: Enum
def __init__(self, the_enum: Type[eT], value: int) -> None: ...
def __repr__(self) -> str: ...
def __int__(self) -> int: ...
def __index__(self) -> int: ...
2 changes: 0 additions & 2 deletions thrift/test/py/TestServer.py
Original file line number Diff line number Diff line change
Expand Up @@ -295,8 +295,6 @@ def main() -> None:
ThriftTest.ContextProcessor(TestContextHandler(options.port)),
)
processor.registerProcessor(
# pyre-fixme[16]: Module `SecondService` has no attribute
# `ContextProcessor`.
"SecondService",
# pyre-fixme[16]: Module `SecondService` has no attribute
# `ContextProcessor`.
Expand Down

0 comments on commit 5518098

Please sign in to comment.