Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved type inference for lambdas in the case where a parameter inc… #5337

Merged
merged 1 commit into from
Jun 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions packages/pyright-internal/src/analyzer/typeEvaluator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13231,17 +13231,26 @@ export function createTypeEvaluator(importLookup: ImportLookup, evaluatorOptions
// For now, use only the first expected type.
const expectedFunctionType = expectedFunctionTypes.length > 0 ? expectedFunctionTypes[0] : undefined;
let paramsArePositionOnly = true;
const expectedParamDetails = expectedFunctionType ? getParameterListDetails(expectedFunctionType) : undefined;

node.parameters.forEach((param, index) => {
let paramType: Type = UnknownType.create();
if (expectedFunctionType && index < expectedFunctionType.details.parameters.length) {
paramType = FunctionType.getEffectiveParameterType(expectedFunctionType, index);
let paramType: Type | undefined;
if (expectedParamDetails) {
if (index < expectedParamDetails.params.length) {
paramType = expectedParamDetails.params[index].type;
} else if (param.defaultValue) {
// If the lambda param has a default value but there is no associated
// parameter in the expected type, assume that the default value is
// being used to explicitly capture a value from an outer scope. Infer
// its type from the default value expression.
paramType = getTypeOfExpression(param.defaultValue, undefined, inferenceContext).type;
}
}

if (param.name) {
writeTypeCache(
param.name,
{ type: transformVariadicParamType(node, param.category, paramType) },
{ type: transformVariadicParamType(node, param.category, paramType ?? UnknownType.create()) },
EvaluatorFlags.None
);
}
Expand Down Expand Up @@ -13288,7 +13297,7 @@ export function createTypeEvaluator(importLookup: ImportLookup, evaluatorOptions
hasDefault: !!param.defaultValue,
defaultValueExpression: param.defaultValue,
hasDeclaredType: true,
type: paramType,
type: paramType ?? UnknownType.create(),
};

FunctionType.addParameter(functionType, functionParam);
Expand Down
12 changes: 12 additions & 0 deletions packages/pyright-internal/src/tests/samples/lambda12.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# This sample tests the case where a lambda includes one or more parameters
# that accept a default value and the the expected type does not include
# these parameters. In this case, the types of the extra parameters should
# be inferred based on the default value type.

# pyright: strict

from typing import Callable


def func1() -> list[Callable[[int], int]]:
return [lambda x, i=i: i * x for i in range(5)]
6 changes: 6 additions & 0 deletions packages/pyright-internal/src/tests/typeEvaluator1.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -681,6 +681,12 @@ test('Lambda11', () => {
TestUtils.validateResults(analysisResults, 0);
});

test('Lambda12', () => {
const analysisResults = TestUtils.typeAnalyzeSampleFiles(['lambda12.py']);

TestUtils.validateResults(analysisResults, 0);
});

test('Call1', () => {
const analysisResults = TestUtils.typeAnalyzeSampleFiles(['call1.py']);

Expand Down