@@ -103,18 +103,21 @@ def process_annotation(self, cls : Type ,
103103 var_name : str ,
104104 var_type : Type ):
105105
106- class_declares_annotation = var_name in getattr (base_cls , '__annotations__' , {})
107-
108- if not hasattr (base_cls , var_name ):
109- self .handle_undefined_var (cls , kwargs , var_name , var_type )
110- elif class_declares_annotation and base_cls is cls :
111- origin = type_safe_cache .get_origin (var_type ) # Only recalculate default for Type[T] annotations
112- if origin is type :
113- self .handle_undefined_var (cls , kwargs , var_name , var_type )
114- else :
115- self .handle_defined_var (base_cls , var_name , var_type )
116- else :
117- self .handle_defined_var (base_cls , var_name , var_type )
106+ class_declares_annotation = var_name in getattr (base_cls , '__annotations__' , {}) # Check if this class has the annotation in its own __annotations__
107+ class_has_own_value = var_name in base_cls .__dict__ # Check if this class defines its own value (not inherited)
108+
109+ if not hasattr (base_cls , var_name ): # Case 1: No value exists anywhere in hierarchy
110+ self .handle_undefined_var (cls , kwargs , var_name , var_type ) # Create fresh default value for this type
111+ elif class_declares_annotation and base_cls is cls and not class_has_own_value : # Case 2: Target class redeclares annotation without own value
112+ self .handle_undefined_var (cls , kwargs , var_name , var_type ) # Create fresh default, don't inherit parent's explicit None
113+ elif class_declares_annotation and base_cls is cls : # Case 3: Target class declares annotation with its own value
114+ origin = type_safe_cache .get_origin (var_type ) # Check if it's a Type[T] annotation
115+ if origin is type : # Type[T] annotations need special handling
116+ self .handle_undefined_var (cls , kwargs , var_name , var_type ) # Recalculate default for Type[T]
117+ else : # Normal annotation with explicit value
118+ self .handle_defined_var (base_cls , var_name , var_type ) # Validate the defined value
119+ else : # Case 4: Inherited value from parent class
120+ self .handle_defined_var (base_cls , var_name , var_type ) # Use and validate the inherited value
118121
119122 def process_annotations (self , cls : Type , # Process all annotations
120123 base_cls : Type ,
0 commit comments