Created
November 12, 2022 20:43
-
-
Save keithel/89be88a4615f84aae246e609e469fe2e to your computer and use it in GitHub Desktop.
PySide 6 6.4 Enum test
This file contains hidden or 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
| from PySide6.QtGui import QStandardItem | |
| import traceback | |
| from enum import IntEnum | |
| user_type = QStandardItem.UserType | |
| # should get: `ItemType.UserType` with new enum support | |
| assert str(user_type) == "ItemType.UserType", "user_type != ItemType.UserType" | |
| try: | |
| assert user_type + 1 == 1001, "user_type + 1 != 1001" | |
| except TypeError as e: | |
| # This fails presently, as you cannot add an int to an enum value, because | |
| # ItemType is not an IntEnum. | |
| # It would be nice if it was, so this would work, however there is | |
| # a limitation with IntEnum seen below | |
| traceback.print_exc() | |
| # Presently, to make a new UserType, you need to get the .value and add the | |
| # offset to that. | |
| assert user_type.value + 1 == 1001 | |
| # To make it an actual enum entry, you need to wrap it in the ItemType | |
| # constructor | |
| my_type = QStandardItem.ItemType(user_type.value + 1) | |
| assert str(my_type) == "ItemType.1001", "my_type didn't match ItemType.1001" | |
| class Color(IntEnum): | |
| RED = 1 | |
| GREEN = 2 | |
| BLUE = 3 | |
| try: | |
| assert Color.RED + 1 == 2, f"Color.RED + 1 != 2: {str(Color.RED + 1)}" | |
| except TypeError as e: | |
| traceback.print_exc() | |
| try: | |
| assert Color(Color.RED + 1) == Color.GREEN, f"Color(Color.RED + 1) != Color.GREEN: { str(Color(Color.RED + 1)) }" | |
| except TypeError as e: | |
| traceback.print_exc() | |
| # Adding to the end of the enum value is fine, as the result is just an int. | |
| try: | |
| assert Color.BLUE + 1 == 4, f"Color.BLUE + 1 != 4: {str(Color.BLUE + 1)}" | |
| except TypeError as e: | |
| traceback.print_exc() | |
| # Specifying additional int values to an enum beyond what it holds and still | |
| # treating it like an enum is an error today: | |
| try: | |
| assert Color(Color.BLUE + 1) == 4, f"Color.BLUE + 1 != 4: {str(Color.BLUE + 1)}" | |
| except TypeError as e: | |
| traceback.print_exc() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment