1010from copy import deepcopy
1111from typing import Any , Generic , TypeVar
1212
13- from typing_extensions import Never , Self
13+ from typing_extensions import Never , Self , override
1414
1515from validataclass .helpers import UnsetValue , UnsetValueType
1616
@@ -35,9 +35,11 @@ class BaseDefault(Generic[T_Default], ABC):
3535 See also: `Default`, `DefaultFactory()`, `DefaultUnset`, `NoDefault`
3636 """
3737
38+ @override
3839 def __repr__ (self ) -> str :
3940 return type (self ).__name__
4041
42+ @override
4143 def __eq__ (self , other : Any ) -> bool :
4244 return NotImplemented
4345
@@ -79,9 +81,11 @@ def __init__(self, value: T_Default):
7981 # If copying the value resulted in the identical object, no factory is needed
8082 self ._needs_factory = self ._value is not value
8183
84+ @override
8285 def __repr__ (self ) -> str :
8386 return f'{ type (self ).__name__ } ({ self ._value !r} )'
8487
88+ @override
8589 def __eq__ (self , other : Any ) -> bool :
8690 # Only handle this if self is of the same type as other OR self is a subclass of other.
8791 # In other words, don't handle this if other is a completely different type or more specialized than self.
@@ -90,6 +94,7 @@ def __eq__(self, other: Any) -> bool:
9094 return isinstance (other , Default ) and bool (self ._value == other ._value )
9195 return NotImplemented
9296
97+ @override
9398 def __hash__ (self ) -> int :
9499 return hash (self ._value )
95100
@@ -105,12 +110,14 @@ def __call__(self) -> Self:
105110 return self
106111 raise TypeError (f"'{ type (self ).__name__ } ' object is not callable" )
107112
113+ @override
108114 def get_value (self ) -> T_Default :
109115 """
110116 Get actual default value.
111117 """
112118 return deepcopy (self ._value )
113119
120+ @override
114121 def needs_factory (self ) -> bool :
115122 """
116123 Return True if a dataclass `default_factory` is needed for this default object, for example if the value is a
@@ -144,9 +151,11 @@ class DefaultFactory(BaseDefault[T_Default]):
144151 def __init__ (self , factory : Callable [[], T_Default ]):
145152 self ._factory = factory
146153
154+ @override
147155 def __repr__ (self ) -> str :
148156 return f'{ type (self ).__name__ } ({ self ._factory !r} )'
149157
158+ @override
150159 def __eq__ (self , other : Any ) -> bool :
151160 # Only handle this if self is of the same type as other OR self is a subclass of other.
152161 # In other words, don't handle this if other is a completely different type or more specialized than self.
@@ -155,15 +164,18 @@ def __eq__(self, other: Any) -> bool:
155164 return isinstance (other , DefaultFactory ) and bool (self ._factory == other ._factory )
156165 return NotImplemented
157166
167+ @override
158168 def __hash__ (self ) -> int :
159169 return hash (self ._factory )
160170
171+ @override
161172 def get_value (self ) -> T_Default :
162173 """
163174 Get an actual default value by calling the factory function.
164175 """
165176 return self ._factory ()
166177
178+ @override
167179 def needs_factory (self ) -> bool :
168180 """
169181 Return True if a dataclass `default_factory` is needed for this default object.
@@ -185,18 +197,22 @@ class _NoDefault(BaseDefault[Never]):
185197 A validataclass field with `NoDefault` is equivalent to a validataclass field without specified default.
186198 """
187199
200+ @override
188201 def __repr__ (self ) -> str :
189202 return 'NoDefault'
190203
204+ @override
191205 def __eq__ (self , other : Any ) -> bool :
192206 # Nothing is equal to NoDefault except itself
193207 return self is other
194208
195209 __hash__ = BaseDefault .__hash__
196210
211+ @override
197212 def get_value (self ) -> Never :
198213 raise ValueError ('No default value specified!' )
199214
215+ @override
200216 def needs_factory (self ) -> bool :
201217 raise NotImplementedError ('NoDefault can be used neither as a value nor as a factory.' )
202218
@@ -212,5 +228,5 @@ def __call__(self) -> Self:
212228
213229# Create sentinel object NoDefault, redefine __new__ to always return the same instance, and delete temporary class
214230NoDefault = _NoDefault ()
215- _NoDefault .__new__ = lambda cls : NoDefault # type: ignore
231+ _NoDefault .__new__ = lambda cls : NoDefault # type: ignore[assignment, method-assign, return-value]
216232del _NoDefault
0 commit comments