1313# SPDX-License-Identifier: Apache-2.0
1414
1515from enum import Enum , auto , unique
16+ from typing import Any , Type , TypeVar
1617
1718
1819@unique
1920class OutputFormat (Enum ):
2021 """Output formats.
2122
22- Do not rely on the actual/literal values, just use enum cases.
23+ Cases are hashable.
24+
25+ Do not rely on the actual/literal values, just use enum cases, like so:
26+ my_of = OutputFormat.XML
2327 """
28+
2429 JSON = auto ()
2530 XML = auto ()
2631
32+ def __hash__ (self ) -> int :
33+ return hash (self .name )
34+
35+
36+ _SV = TypeVar ('_SV' , bound = 'SchemaVersion' )
37+
2738
2839@unique
2940class SchemaVersion (Enum ):
@@ -33,52 +44,54 @@ class SchemaVersion(Enum):
3344 Cases are hashable.
3445 Cases are comparable(!=,>=,>,==,<,<=)
3546
36- Do not rely on the actual/literal values, just use enum cases.
47+ Do not rely on the actual/literal values, just use enum cases, like so:
48+ my_sv = SchemaVersion.V1_3
3749 """
50+
3851 V1_4 = (1 , 4 )
3952 V1_3 = (1 , 3 )
4053 V1_2 = (1 , 2 )
4154 V1_1 = (1 , 1 )
4255 V1_0 = (1 , 0 )
4356
4457 @classmethod
45- def from_version (cls , version : str ) -> 'SchemaVersion' :
46- """Return instance from a version string - e.g. `1.4`"""
58+ def from_version (cls : Type [ _SV ] , version : str ) -> _SV :
59+ """Return instance based of a version string - e.g. `1.4`"""
4760 return cls (tuple (map (int , version .split ('.' )))[:2 ])
4861
4962 def to_version (self ) -> str :
5063 """Return as a version string - e.g. `1.4`"""
5164 return '.' .join (map (str , self .value ))
5265
53- def __ne__ (self , other : object ) -> bool :
54- return self . value != other . value \
55- if isinstance ( other , self .__class__ ) \
56- else NotImplemented # type:ignore[return-value]
57-
58- def __lt__ (self , other : object ) -> bool :
59- return self . value < other . value \
60- if isinstance ( other , self .__class__ ) \
61- else NotImplemented # type:ignore[return-value]
62-
63- def __le__ (self , other : object ) -> bool :
64- return self . value <= other . value \
65- if isinstance ( other , self .__class__ ) \
66- else NotImplemented # type:ignore[return-value]
67-
68- def __eq__ (self , other : object ) -> bool :
69- return self . value == other . value \
70- if isinstance ( other , self .__class__ ) \
71- else NotImplemented # type:ignore[return-value]
72-
73- def __ge__ (self , other : object ) -> bool :
74- return self . value >= other . value \
75- if isinstance ( other , self .__class__ ) \
76- else NotImplemented # type:ignore[return-value]
77-
78- def __gt__ (self , other : object ) -> bool :
79- return self . value > other . value \
80- if isinstance ( other , self .__class__ ) \
81- else NotImplemented # type:ignore[return-value]
66+ def __ne__ (self , other : Any ) -> bool :
67+ if isinstance ( other , self . __class__ ):
68+ return self .value != other . value
69+ return NotImplemented # pragma: no cover
70+
71+ def __lt__ (self , other : Any ) -> bool :
72+ if isinstance ( other , self . __class__ ):
73+ return self .value < other . value
74+ return NotImplemented # pragma: no cover
75+
76+ def __le__ (self , other : Any ) -> bool :
77+ if isinstance ( other , self . __class__ ):
78+ return self .value <= other . value
79+ return NotImplemented # pragma: no cover
80+
81+ def __eq__ (self , other : Any ) -> bool :
82+ if isinstance ( other , self . __class__ ):
83+ return self .value == other . value
84+ return NotImplemented # pragma: no cover
85+
86+ def __ge__ (self , other : Any ) -> bool :
87+ if isinstance ( other , self . __class__ ):
88+ return self .value >= other . value
89+ return NotImplemented # pragma: no cover
90+
91+ def __gt__ (self , other : Any ) -> bool :
92+ if isinstance ( other , self . __class__ ):
93+ return self .value > other . value
94+ return NotImplemented # pragma: no cover
8295
8396 def __hash__ (self ) -> int :
8497 return hash (self .name )
0 commit comments