Skip to content

Commit b50d638

Browse files
committed
Add test for using is_lvalue from AttributeContext
1 parent 54e6de0 commit b50d638

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

test-data/unit/check-custom-plugin.test

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,8 @@ reveal_type(magic.non_magic_method()) # N: Revealed type is "builtins.int"
232232
reveal_type(magic.non_magic_field) # N: Revealed type is "builtins.int"
233233
magic.nonexistent_field # E: Field does not exist
234234
reveal_type(magic.fallback_example) # N: Revealed type is "Any"
235+
reveal_type(magic.no_assignment_field) # N: Revealed type is "builtins.float"
236+
magic.no_assignment_field = "bad" # E: Cannot assign to field
235237

236238
derived = DerivedMagic()
237239
reveal_type(derived.magic_field) # N: Revealed type is "builtins.str"
@@ -250,6 +252,7 @@ class Magic:
250252
def __getattr__(self, x: Any) -> Any: ...
251253
def non_magic_method(self) -> int: ...
252254
non_magic_field: int
255+
no_assignment_field: float
253256

254257
class DerivedMagic(Magic): ...
255258
[file mypy.ini]

test-data/unit/plugins/attrhook2.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ def get_attribute_hook(self, fullname: str) -> Callable[[AttributeContext], Type
1212
return magic_field_callback
1313
if fullname == "m.Magic.nonexistent_field":
1414
return nonexistent_field_callback
15+
if fullname == "m.Magic.no_assignment_field":
16+
return no_assignment_field
1517
return None
1618

1719

@@ -24,5 +26,12 @@ def nonexistent_field_callback(ctx: AttributeContext) -> Type:
2426
return AnyType(TypeOfAny.from_error)
2527

2628

29+
def no_assignment_field(ctx: AttributeContext) -> Type:
30+
if ctx.is_lvalue:
31+
ctx.api.fail(f"Cannot assign to field", ctx.context)
32+
return AnyType(TypeOfAny.from_error)
33+
return ctx.default_attr_type
34+
35+
2736
def plugin(version: str) -> type[AttrPlugin]:
2837
return AttrPlugin

0 commit comments

Comments
 (0)