diff --git a/mypy/stubtest.py b/mypy/stubtest.py index a6780984c1f5..ae1da96af92b 100644 --- a/mypy/stubtest.py +++ b/mypy/stubtest.py @@ -1323,6 +1323,14 @@ def verify_var( stub, runtime, ) + elif stub.final_value is not None and stub.final_value != runtime: + yield Error( + object_path, + "is inconsistent, stub value for Final var differs from runtime value", + stub, + runtime, + stub_desc=repr(stub.final_value), + ) @verify.register(nodes.OverloadedFuncDef) diff --git a/mypy/test/teststubtest.py b/mypy/test/teststubtest.py index 45cc46c40108..dc3af77dcb5a 100644 --- a/mypy/test/teststubtest.py +++ b/mypy/test/teststubtest.py @@ -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 + """, + 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]: