Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
stubtest: check Final variables with literal values against runtime
  • Loading branch information
Vikash-Kumar-23 committed Feb 22, 2026
commit 380d5d34b258e5a32e1f48c326a8d302c2365599
8 changes: 8 additions & 0 deletions mypy/stubtest.py
Original file line number Diff line number Diff line change
Expand Up @@ -1279,6 +1279,14 @@ def verify_var(
yield Error(object_path, "is not present at runtime", stub, runtime)
return

if stub.final_value is not None and stub.final_value != runtime:
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe move this to an elif branch of the subtype check below? That way we don't get duplicate errors when the types are different.

yield Error(
object_path,
f"is inconsistent, stub value {stub.final_value!r} differs from runtime value {runtime!r}",
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The runtime object could potentially have a very long repr, which we wouldn't want included in the error message. It's also already rendered in the output (with length truncation), so including in the message isn't necessary.

stub,
runtime,
Comment thread
Vikash-Kumar-23 marked this conversation as resolved.
Outdated
)

if (
stub.is_initialized_in_class
and is_read_only_property(runtime)
Expand Down
16 changes: 16 additions & 0 deletions mypy/test/teststubtest.py
Original file line number Diff line number Diff line change
Expand Up @@ -1171,6 +1171,22 @@ def read_write_attr(self, val): self._val = val
""",
error=None,
)
yield Case(
stub="""
from typing import Final
x_final: Final = 2
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style nit: maybe use CAPS_CASE, like the other cases with Final variables?

""",
runtime="x_final = 1",
error="x_final",
)
yield Case(
stub="""
from typing import Final
x_final_ok: Final = 1
""",
runtime="x_final_ok = 1",
error=None,
)

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