Skip to content

stubtest: error if an attribute is a read-only property at runtime, but isn't read-only in the stub #12291

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 5 commits into from
Mar 21, 2022
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
29 changes: 26 additions & 3 deletions mypy/stubtest.py
Original file line number Diff line number Diff line change
Expand Up @@ -770,6 +770,18 @@ def verify_var(
yield Error(object_path, "is not present at runtime", stub, runtime)
return

if (
stub.is_initialized_in_class
and is_read_only_property(runtime)
and (stub.is_settable_property or not stub.is_property)
):
yield Error(
object_path,
"is read-only at runtime but not in the stub",
stub,
runtime
)

runtime_type = get_mypy_type_of_runtime_value(runtime)
if (
runtime_type is not None
Expand Down Expand Up @@ -802,7 +814,14 @@ def verify_overloadedfuncdef(
return

if stub.is_property:
# We get here in cases of overloads from property.setter
# Any property with a setter is represented as an OverloadedFuncDef
if is_read_only_property(runtime):
yield Error(
object_path,
"is read-only at runtime but not in the stub",
stub,
runtime
)
return

if not is_probably_a_function(runtime):
Expand Down Expand Up @@ -845,7 +864,7 @@ def verify_typevarexpr(
yield None


def _verify_property(stub: nodes.Decorator, runtime: Any) -> Iterator[str]:
def _verify_readonly_property(stub: nodes.Decorator, runtime: Any) -> Iterator[str]:
assert stub.func.is_property
if isinstance(runtime, property):
return
Expand Down Expand Up @@ -920,7 +939,7 @@ def verify_decorator(
yield Error(object_path, "is not present at runtime", stub, runtime)
return
if stub.func.is_property:
for message in _verify_property(stub, runtime):
for message in _verify_readonly_property(stub, runtime):
yield Error(object_path, message, stub, runtime)
return

Expand Down Expand Up @@ -1040,6 +1059,10 @@ def is_probably_a_function(runtime: Any) -> bool:
)


def is_read_only_property(runtime: object) -> bool:
return isinstance(runtime, property) and runtime.fset is None


def safe_inspect_signature(runtime: Any) -> Optional[inspect.Signature]:
try:
return inspect.signature(runtime)
Expand Down
62 changes: 60 additions & 2 deletions mypy/test/teststubtest.py
Original file line number Diff line number Diff line change
Expand Up @@ -548,12 +548,12 @@ def test_property(self) -> Iterator[Case]:
stub="""
class Good:
@property
def f(self) -> int: ...
def read_only_attr(self) -> int: ...
""",
runtime="""
class Good:
@property
def f(self) -> int: return 1
def read_only_attr(self): return 1
""",
error=None,
)
Expand Down Expand Up @@ -593,6 +593,38 @@ class BadReadOnly:
""",
error="BadReadOnly.f",
)
yield Case(
stub="""
class Y:
@property
def read_only_attr(self) -> int: ...
@read_only_attr.setter
def read_only_attr(self, val: int) -> None: ...
""",
runtime="""
class Y:
@property
def read_only_attr(self): return 5
""",
error="Y.read_only_attr",
)
yield Case(
stub="""
class Z:
@property
def read_write_attr(self) -> int: ...
@read_write_attr.setter
def read_write_attr(self, val: int) -> None: ...
""",
runtime="""
class Z:
@property
def read_write_attr(self): return self._val
@read_write_attr.setter
def read_write_attr(self, val): self._val = val
""",
error=None,
)

@collect_cases
def test_var(self) -> Iterator[Case]:
Expand Down Expand Up @@ -631,6 +663,32 @@ def __init__(self):
""",
error=None,
)
yield Case(
stub="""
class Y:
read_only_attr: int
""",
runtime="""
class Y:
@property
def read_only_attr(self): return 5
""",
error="Y.read_only_attr",
)
yield Case(
stub="""
class Z:
read_write_attr: int
""",
runtime="""
class Z:
@property
def read_write_attr(self): return self._val
@read_write_attr.setter
def read_write_attr(self, val): self._val = val
""",
error=None,
)

@collect_cases
def test_type_alias(self) -> Iterator[Case]:
Expand Down