1- import sys
1+ from shared import LoraLoader
22
3- if sys .version_info >= (3 , 11 ):
4- from enum import StrEnum
5- # StrEnum is introduced in 3.11 while we support python 3.10
6- else :
7- from enum import Enum , auto
8- from typing import Any
9-
10- # Fallback for Python 3.10 and earlier
11- class StrEnum (str , Enum ):
12- def __new__ (cls , value , * args , ** kwargs ):
13- if not isinstance (value , (str , auto )):
14- raise TypeError (
15- f"Values of StrEnums must be strings: { value !r} is a { type (value )} "
16- )
17- return super ().__new__ (cls , value , * args , ** kwargs )
18-
19- def __str__ (self ):
20- return str (self .value )
21-
22- @staticmethod
23- def _generate_next_value_ (
24- name : str , start : int , count : int , last_values : list [Any ]
25- ) -> str :
26- return name
27-
28-
29- class LoraLoader (StrEnum ):
30- DIFFUSERS = "diffusers"
31- LORA_READY = "lora_ready"
32- DEFAULT = LORA_READY
33-
34- @staticmethod
35- def supported_values () -> list [str ]:
36- """Returns a list of all supported LoraLoader values."""
37- return [loader .value for loader in LoraLoader ]
38-
39- @staticmethod
40- def safe_parse (value : "str | LoraLoader" ) -> "LoraLoader" :
41- if isinstance (value , LoraLoader ):
42- return value
43- try :
44- return LoraLoader (value )
45- except ValueError :
46- return LoraLoader .DEFAULT
47-
48-
49- if __name__ == "__main__" :
50- # Test the StrEnum functionality
51- print ("diffusers:" , LoraLoader .DIFFUSERS ) # Should print "diffusers"
52- print ("lora_ready:" , LoraLoader .LORA_READY ) # Should print "lora_ready"
53- print ("default:" , LoraLoader .DEFAULT ) # Should print "lora_ready"
54- print ( # Should print all unique supported values (excludes aliases like DEFAULT)
55- "supported_values:" , LoraLoader .supported_values ()
56- )
57- try :
58- print ("fail:" , LoraLoader ("invalid" )) # Should raise ValueError
59- except ValueError as e :
60- print ("pass:" , e ) # Prints: Invalid LoraLoader value: invalid
61- try :
62- print ("pass:" , LoraLoader ("diffusers" )) # Should return LoraLoader.DIFFUSERS
63- except ValueError as e :
64- print ("fail:" , e )
65- try :
66- print ("type of LoraLoader.DEFAULT:" , type (LoraLoader .DEFAULT ))
67- default = LoraLoader .DEFAULT
68- print ("type of default:" , type (default )) # Should be LoraLoader, not str
69- except Exception as e :
70- print (f"fail: { e } " )
71-
72- assert isinstance (LoraLoader ("lora_ready" ), StrEnum )
73- assert isinstance (LoraLoader .DIFFUSERS , LoraLoader ), (
74- "DIFFUSERS should be an instance of LoraLoader"
75- )
76- assert LoraLoader .DEFAULT == LoraLoader .DIFFUSERS , (
77- "Default loader should be DIFFUSERS"
78- )
79- assert LoraLoader .DIFFUSERS != LoraLoader .LORA_READY , (
80- "DIFFUSERS should not equal LORA_READY"
81- )
82-
83- assert LoraLoader .LORA_READY .value == "lora_ready" , (
84- "lora_ready string should equal LoraLoader.LORA_READY"
85- )
3+ # todo: remove this import when the diffusers_helper is updated to use the new enums directly
4+ __all__ = ["LoraLoader" ]
0 commit comments