@@ -308,18 +308,21 @@ def report_missing_attribute(
308
308
def analyze_instance_member_access (
309
309
name : str , typ : Instance , mx : MemberContext , override_info : TypeInfo | None
310
310
) -> Type :
311
- if name == "__init__" and not mx .is_super :
312
- # Accessing __init__ in statically typed code would compromise
313
- # type safety unless used via super().
314
- mx .fail (message_registry .CANNOT_ACCESS_INIT )
315
- return AnyType (TypeOfAny .from_error )
316
-
317
- # The base object has an instance type.
318
-
319
311
info = typ .type
320
312
if override_info :
321
313
info = override_info
322
314
315
+ method = info .get_method (name )
316
+
317
+ if name == "__init__" and not mx .is_super and not info .is_final :
318
+ if not method or not method .is_final :
319
+ # Accessing __init__ in statically typed code would compromise
320
+ # type safety unless used via super() or the method/class is final.
321
+ mx .fail (message_registry .CANNOT_ACCESS_INIT )
322
+ return AnyType (TypeOfAny .from_error )
323
+
324
+ # The base object has an instance type.
325
+
323
326
if (
324
327
state .find_occurrences
325
328
and info .name == state .find_occurrences [0 ]
@@ -329,7 +332,6 @@ def analyze_instance_member_access(
329
332
mx .msg .note ("Occurrence of '{}.{}'" .format (* state .find_occurrences ), mx .context )
330
333
331
334
# Look up the member. First look up the method dictionary.
332
- method = info .get_method (name )
333
335
if method and not isinstance (method , Decorator ):
334
336
if mx .is_super and not mx .suppress_errors :
335
337
validate_super_call (method , mx )
0 commit comments