Fixed bug that leads to a spurious "unbound variable" diagnostic when a variable is assigned a value using a walrus operator and is later used in a ** call argument. This addresses #8218.

This commit is contained in:
Eric Traut 2024-06-25 13:28:04 +02:00
parent bc1fc9f22b
commit 2672441c22
2 changed files with 19 additions and 3 deletions

View File

@ -10680,11 +10680,16 @@ export function createTypeEvaluator(
while (argIndex < argList.length) {
if (argList[argIndex].argumentCategory === ArgumentCategory.UnpackedDictionary) {
// Verify that the type used in this expression is a SupportsKeysAndGetItem[str, T].
const argType = getTypeOfArgument(
const argTypeResult = getTypeOfArgument(
argList[argIndex],
makeInferenceContext(paramDetails.unpackedKwargsTypedDictType),
signatureTracker
).type;
);
const argType = argTypeResult.type;
if (argTypeResult.isIncomplete) {
isTypeIncomplete = true;
}
if (isAnyOrUnknown(argType)) {
unpackedDictionaryArgType = argType;

View File

@ -1,5 +1,7 @@
# This sample tests assignment expressions used within arguments
# This sample tests assignment expressions used within arguments.
from dataclasses import dataclass
from typing import Mapping
import collections
@ -10,3 +12,12 @@ class NearestKeyDict(collections.UserDict):
# This should generate an error because walrus operators
# are not allowed with named arguments.
b = list(iterable = keys := [k for k in sorted(self.data) if k >= key])
@dataclass
class DC1:
x: str
def func1(mapping: Mapping[str, dict]):
return [DC1(temp := "x", **mapping[temp])]