Report error if accessing an instance attribute through class object.
(
builder: IRBuilder, expr: MemberExpr, typ: ProperType | None
)
| 290 | |
| 291 | |
| 292 | def 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 | |
| 324 | def transform_super_expr(builder: IRBuilder, o: SuperExpr) -> Value: |
no test coverage detected
searching dependent graphs…