Skip to content

Commit

Permalink
Fixed a bug that results in a false positive when a class is defined …
Browse files Browse the repository at this point in the history
…within a generic function and uses type parameters from that function. This addresses #9359. (#9360)
  • Loading branch information
erictraut authored Oct 30, 2024
1 parent 5006323 commit e6fe8cb
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
5 changes: 5 additions & 0 deletions packages/pyright-internal/src/analyzer/typeUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2037,6 +2037,11 @@ export function getTypeVarArgsRecursive(type: Type, recursionCount = 0): TypeVar
return [];
}

// Don't return any bound type variables.
if (TypeVarType.isBound(type)) {
return [];
}

// Don't return any P.args or P.kwargs types.
if (isParamSpec(type) && type.priv.paramSpecAccess) {
return [TypeVarType.cloneForParamSpecAccess(type, /* access */ undefined)];
Expand Down
13 changes: 13 additions & 0 deletions packages/pyright-internal/src/tests/samples/typeParams8.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# This sample tests the case where a class defined in an inner scope
# uses type variables from an outer scope.


class Parent[S, T]:
def task(self, input: S) -> T: ...


def outer_func1[S, T]():
class Child(Parent[S, T]):
def task(self, input: S) -> T: ...

return Child
8 changes: 8 additions & 0 deletions packages/pyright-internal/src/tests/typeEvaluator5.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,14 @@ test('TypeParams7', () => {
TestUtils.validateResults(analysisResults, 4);
});

test('TypeParams8', () => {
const configOptions = new ConfigOptions(Uri.empty());
configOptions.defaultPythonVersion = pythonVersion3_12;

const analysisResults = TestUtils.typeAnalyzeSampleFiles(['typeParams8.py'], configOptions);
TestUtils.validateResults(analysisResults, 0);
});

test('AutoVariance1', () => {
const configOptions = new ConfigOptions(Uri.empty());
configOptions.defaultPythonVersion = pythonVersion3_12;
Expand Down

0 comments on commit e6fe8cb

Please sign in to comment.