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

Skip to content

Commit b972d9a

Browse files
Vikash-Kumar-23brianschuberthauntsaninja
authored
stubtest: check Final variables with literal values against runtime (#20858)
Currently, stubtest does not detect discrepancies when a stub uses Final = <value> 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. Fixes #20857 --------- Co-authored-by: Brian Schubert <[email protected]> Co-authored-by: Shantanu <[email protected]>
1 parent 837052e commit b972d9a

2 files changed

Lines changed: 24 additions & 0 deletions

File tree

mypy/stubtest.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1323,6 +1323,14 @@ def verify_var(
13231323
stub,
13241324
runtime,
13251325
)
1326+
elif stub.final_value is not None and stub.final_value != runtime:
1327+
yield Error(
1328+
object_path,
1329+
"is inconsistent, stub value for Final var differs from runtime value",
1330+
stub,
1331+
runtime,
1332+
stub_desc=repr(stub.final_value),
1333+
)
13261334

13271335

13281336
@verify.register(nodes.OverloadedFuncDef)

mypy/test/teststubtest.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1171,6 +1171,22 @@ def read_write_attr(self, val): self._val = val
11711171
""",
11721172
error=None,
11731173
)
1174+
yield Case(
1175+
stub="""
1176+
from typing import Final
1177+
X_FINAL: Final = 2
1178+
""",
1179+
runtime="X_FINAL = 1",
1180+
error="X_FINAL",
1181+
)
1182+
yield Case(
1183+
stub="""
1184+
from typing import Final
1185+
X_FINAL_OK: Final = 1
1186+
""",
1187+
runtime="X_FINAL_OK = 1",
1188+
error=None,
1189+
)
11741190

11751191
@collect_cases
11761192
def test_type_alias(self) -> Iterator[Case]:

0 commit comments

Comments
 (0)