Skip to content

Comments

stubtest: check Final variables with literal values against runtime#20858

Open
Vikash-Kumar-23 wants to merge 5 commits intopython:masterfrom
Vikash-Kumar-23:fix-stubtest-final-20857
Open

stubtest: check Final variables with literal values against runtime#20858
Vikash-Kumar-23 wants to merge 5 commits intopython:masterfrom
Vikash-Kumar-23:fix-stubtest-final-20857

Conversation

@Vikash-Kumar-23
Copy link

Description
Currently, stubtest does not detect discrepancies when a stub uses Final = but the runtime has a different value. While it correctly handles Literal[...] mismatches, it was previously silent on Final assignments.

This PR extends the variable verification logic in stubtest to ensure assigned Final values in stubs are compared against the actual runtime value.

Changes

mypy/stubtest.py: Added logic in verify_var to compare stub.final_value with the runtime object if a concrete value is assigned in the stub.

mypy/test/teststubtest.py: Added regression test cases to test_var covering both matching and mismatched Final values.

Verification
Verified the fix using a minimal reproduction case:

Stub: FOO: Final = 2

Runtime: FOO = 1

Result: error: repro.FOO is inconsistent, stub value 2 differs from runtime value 1

Evidence:
image

Note on Environment:
Full stubtest suite was verified (63/63 passed) prior to recent upstream changes affecting the local environment.

Fixes #20857

mypy/stubtest.py Outdated
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
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.

mypy/stubtest.py Outdated
if stub.final_value is not None and stub.final_value != runtime:
yield Error(
object_path,
f"is inconsistent, stub value {stub.final_value!r} differs from runtime value {runtime!r}",
Copy link
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.

yield Case(
stub="""
from typing import Final
x_final: Final = 2
Copy link
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?

@Vikash-Kumar-23
Copy link
Author

"Hi @brianschubert, thank you for the feedback! I have applied the suggested refinements to the PR:

Logic Refactor: Moved the Final value check into an elif branch of the subtype check to prevent duplicate error reporting.

Error Message: Cleaned up the error string by removing {runtime!r} value as suggested.

Consistency: Updated the new test cases to use CAPS_CASE for constants to match the project's style.

Verification: Verified the fix locally

image

Copy link
Collaborator

@hauntsaninja hauntsaninja left a comment

Choose a reason for hiding this comment

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

one more nit...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[stubtest] Better checking of Final variables with literal values

3 participants