diff --git a/lumibot/entities/asset.py b/lumibot/entities/asset.py index 26098c802..2d41f98c9 100644 --- a/lumibot/entities/asset.py +++ b/lumibot/entities/asset.py @@ -1,32 +1,8 @@ from collections import UserDict from datetime import date, datetime -from enum import Enum from lumibot.tools import parse_symbol - - -# Custom string enum implementation for Python 3.9 compatibility -class StrEnum(str, Enum): - """ - A string enum implementation that works with Python 3.9+ - - This class extends str and Enum to create string enums that: - 1. Can be used like strings (string methods, comparison) - 2. Are hashable (for use in dictionaries, sets, etc.) - 3. Can be used in string comparisons without explicit conversion - """ - def __str__(self): - return self.value - - def __eq__(self, other): - if isinstance(other, str): - return self.value == other - return super().__eq__(other) - - def __hash__(self): - # Use the hash of the enum member, not the string value - # This ensures proper hashability while maintaining enum identity - return super().__hash__() +from lumibot.tools.types import StrEnum class Asset: diff --git a/lumibot/entities/order.py b/lumibot/entities/order.py index 428a9184c..c724cf458 100644 --- a/lumibot/entities/order.py +++ b/lumibot/entities/order.py @@ -2,38 +2,12 @@ import uuid from collections import namedtuple from decimal import Decimal -from enum import Enum from threading import Event import datetime from typing import Union import lumibot.entities as entities -from lumibot.tools.types import check_positive, check_price - - -# Custom string enum implementation for Python 3.9 compatibility -class StrEnum(str, Enum): - """ - A string enum implementation that works with Python 3.9+ - - This class extends str and Enum to create string enums that: - 1. Can be used like strings (string methods, comparison) - 2. Are hashable (for use in dictionaries, sets, etc.) - 3. Can be used in string comparisons without explicit conversion - """ - def __str__(self): - return self.value - - def __eq__(self, other): - if isinstance(other, str): - return self.value == other - return super().__eq__(other) - - def __hash__(self): - # Use the hash of the enum member, not the string value - # This ensures proper hashability while maintaining enum identity - return super().__hash__() - +from lumibot.tools.types import check_positive, check_price, StrEnum SELL = "sell" BUY = "buy" diff --git a/lumibot/tools/types.py b/lumibot/tools/types.py index f0bf8cc43..114fbb6b1 100644 --- a/lumibot/tools/types.py +++ b/lumibot/tools/types.py @@ -1,6 +1,29 @@ import warnings +from enum import Enum from decimal import Decimal +# Custom string enum implementation for Python 3.9 compatibility +class StrEnum(str, Enum): + """ + A string enum implementation that works with Python 3.9+ + + This class extends str and Enum to create string enums that: + 1. Can be used like strings (string methods, comparison) + 2. Are hashable (for use in dictionaries, sets, etc.) + 3. Can be used in string comparisons without explicit conversion + """ + def __str__(self): + return self.value + + def __eq__(self, other): + if isinstance(other, str): + return self.value == other + return super().__eq__(other) + + def __hash__(self): + # Use the hash of the enum member, not the string value + # This ensures proper hashability while maintaining enum identity + return super().__hash__() def check_numeric( input, type, error_message, positive=True, strict=False, nullable=False, ratio=False, allow_negative=True