Skip to content

Fix crash on invalid property inside its own body #19208

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions mypy/checkmember.py
Original file line number Diff line number Diff line change
Expand Up @@ -981,6 +981,10 @@ def expand_and_bind_callable(
assert isinstance(expanded, CallableType)
if var.is_settable_property and mx.is_lvalue and var.setter_type is not None:
# TODO: use check_call() to infer better type, same as for __set__().
if not expanded.arg_types:
# This can happen when accessing invalid property from its own body,
# error will be reported elsewhere.
return AnyType(TypeOfAny.from_error)
return expanded.arg_types[0]
else:
return expanded.ret_type
Expand Down
13 changes: 13 additions & 0 deletions test-data/unit/check-classes.test
Original file line number Diff line number Diff line change
Expand Up @@ -8726,3 +8726,16 @@ class Fields:
reveal_type(Fields.bool_f) # N: Revealed type is "__main__.BoolField"
reveal_type(Fields.int_f) # N: Revealed type is "__main__.NumField"
reveal_type(Fields.custom_f) # N: Revealed type is "__main__.AnyField[__main__.Custom]"

[case testRecursivePropertyWithInvalidSetterNoCrash]
class NoopPowerResource:
_hardware_type: int

@property
def hardware_type(self) -> int:
return self._hardware_type

@hardware_type.setter
def hardware_type(self) -> None: # E: Invalid property setter signature
self.hardware_type = None # Note: intentionally recursive
[builtins fixtures/property.pyi]