(
stub: nodes.Var, runtime: MaybeMissing[Any], object_path: list[str]
)
| 1310 | |
| 1311 | @verify.register(nodes.Var) |
| 1312 | def verify_var( |
| 1313 | stub: nodes.Var, runtime: MaybeMissing[Any], object_path: list[str] |
| 1314 | ) -> Iterator[Error]: |
| 1315 | if isinstance(runtime, Missing): |
| 1316 | # Don't always yield an error here, because we often can't find instance variables |
| 1317 | if len(object_path) <= 2: |
| 1318 | yield Error(object_path, "is not present at runtime", stub, runtime) |
| 1319 | return |
| 1320 | |
| 1321 | if ( |
| 1322 | stub.is_initialized_in_class |
| 1323 | and is_read_only_property(runtime) |
| 1324 | and (stub.is_settable_property or not stub.is_property) |
| 1325 | ): |
| 1326 | yield Error(object_path, "is read-only at runtime but not in the stub", stub, runtime) |
| 1327 | |
| 1328 | runtime_type = get_mypy_type_of_runtime_value(runtime, type_context=stub.type) |
| 1329 | note = "" |
| 1330 | if ( |
| 1331 | runtime_type is not None |
| 1332 | and stub.type is not None |
| 1333 | and not is_subtype_helper(runtime_type, stub.type) |
| 1334 | ): |
| 1335 | should_error = True |
| 1336 | # Avoid errors when defining enums, since runtime_type is the enum itself, but we'd |
| 1337 | # annotate it with the type of runtime.value |
| 1338 | if isinstance(runtime, enum.Enum): |
| 1339 | runtime_type = get_mypy_type_of_runtime_value(runtime.value) |
| 1340 | if runtime_type is not None and is_subtype_helper(runtime_type, stub.type): |
| 1341 | should_error = False |
| 1342 | # We always allow setting the stub value to Ellipsis (...), but use |
| 1343 | # _value_ type as a fallback if given. If a member is ... and _value_ |
| 1344 | # type is given, all runtime types should be assignable to _value_. |
| 1345 | proper_type = mypy.types.get_proper_type(stub.type) |
| 1346 | if ( |
| 1347 | isinstance(proper_type, mypy.types.Instance) |
| 1348 | and proper_type.type.fullname in mypy.types.ELLIPSIS_TYPE_NAMES |
| 1349 | ): |
| 1350 | value_t = stub.info.get("_value_") |
| 1351 | if value_t is None or value_t.type is None or runtime_type is None: |
| 1352 | should_error = False |
| 1353 | elif is_subtype_helper(runtime_type, value_t.type): |
| 1354 | should_error = False |
| 1355 | else: |
| 1356 | note = " (incompatible '_value_')" |
| 1357 | |
| 1358 | if should_error: |
| 1359 | yield Error( |
| 1360 | object_path, |
| 1361 | f"variable differs from runtime type {runtime_type}{note}", |
| 1362 | stub, |
| 1363 | runtime, |
| 1364 | ) |
| 1365 | elif stub.final_value is not None and stub.final_value != runtime: |
| 1366 | yield Error( |
| 1367 | object_path, |
| 1368 | "is inconsistent, stub value for Final var differs from runtime value", |
| 1369 | stub, |
nothing calls this directly
no test coverage detected
searching dependent graphs…