MCPcopy Index your code
hub / github.com/python/mypy / check_instance_attribute_access_through_class

Function check_instance_attribute_access_through_class

mypyc/irbuild/expression.py:292–321  ·  view source on GitHub ↗

Report error if accessing an instance attribute through class object.

(
    builder: IRBuilder, expr: MemberExpr, typ: ProperType | None
)

Source from the content-addressed store, hash-verified

290
291
292def check_instance_attribute_access_through_class(
293 builder: IRBuilder, expr: MemberExpr, typ: ProperType | None
294) -> None:
295 """Report error if accessing an instance attribute through class object."""
296 if isinstance(expr.expr, RefExpr):
297 node = expr.expr.node
298 if isinstance(typ, TypeType) and isinstance(typ.item, Instance):
299 # TODO: Handle other item types
300 node = typ.item.type
301 if isinstance(node, TypeInfo):
302 class_ir = builder.mapper.type_to_ir.get(node)
303 if class_ir is not None and class_ir.is_ext_class:
304 sym = node.get(expr.name)
305 if (
306 sym is not None
307 and isinstance(sym.node, Var)
308 and not sym.node.is_classvar
309 and not sym.node.is_final
310 ):
311 builder.error(
312 'Cannot access instance attribute "{}" through class object'.format(
313 expr.name
314 ),
315 expr.line,
316 )
317 builder.note(
318 '(Hint: Use "x: Final = ..." or "x: ClassVar = ..." to define '
319 "a class attribute)",
320 expr.line,
321 )
322
323
324def transform_super_expr(builder: IRBuilder, o: SuperExpr) -> Value:

Callers 1

transform_member_exprFunction · 0.85

Calls 5

isinstanceFunction · 0.85
getMethod · 0.45
errorMethod · 0.45
formatMethod · 0.45
noteMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…