Skip to content

Commit

Permalink
Update mode.py to warn in Mode.FUNCTIONS access vs. in __new__
Browse files Browse the repository at this point in the history
Currently, any import of `instructor` raises a `DeprecationWarning`. This is noisy and also makes it impossible to have test suites raise an error on `DeprecationWarning`. Example code:

```python3
import warnings
warnings.simplefilter("default")

import instructor  # raises `DeprecationWarning: FUNCTIONS is deprecated and will be removed in future versions enum_member = enum_class._new_member_(enum_class, *args)`
```
  • Loading branch information
boydgreenfield committed May 17, 2024
1 parent cd5169e commit 9ef6f1a
Showing 1 changed file with 12 additions and 15 deletions.
27 changes: 12 additions & 15 deletions instructor/mode.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,18 @@
import warnings


class Mode(enum.Enum):
class _WarnOnFunctionsAccessEnumMeta(enum.EnumMeta):
def __getattribute__(cls, name):
if name == "FUNCTIONS":
warnings.warn(
"FUNCTIONS is deprecated and will be removed in future versions",
DeprecationWarning,
stacklevel=2,
)
return super().__getattribute__(name)


class Mode(enum.Enum, metaclass=_WarnOnFunctionsAccessEnumMeta):
"""The mode to use for patching the client"""

FUNCTIONS = "function_call"
Expand All @@ -15,17 +26,3 @@ class Mode(enum.Enum):
ANTHROPIC_TOOLS = "anthropic_tools"
ANTHROPIC_JSON = "anthropic_json"
COHERE_TOOLS = "cohere_tools"

def __new__(cls, value: str) -> "Mode":
member = object.__new__(cls)
member._value_ = value

# Deprecation warning for FUNCTIONS
if value == "function_call":
warnings.warn(
"FUNCTIONS is deprecated and will be removed in future versions",
DeprecationWarning,
stacklevel=2,
)

return member

0 comments on commit 9ef6f1a

Please sign in to comment.