Skip to content

Commit

Permalink
Added test cases for #6261 and fixed a bug found by test. (#6332)
Browse files Browse the repository at this point in the history
  • Loading branch information
erictraut authored Nov 5, 2023
1 parent d56dfab commit b03c411
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 3 deletions.
2 changes: 1 addition & 1 deletion packages/pyright-internal/src/analyzer/constraintSolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ export function assignTypeToTypeVar(

// Emit an error unless this is a synthesized type variable used
// for pseudo-generic classes.
if (!destType.details.isSynthesized && !destType.details.isSynthesizedSelf) {
if (!destType.details.isSynthesized || destType.details.isSynthesizedSelf) {
diag?.addMessage(
Localizer.DiagnosticAddendum.typeAssignmentMismatch().format(
evaluator.printSrcDestTypes(srcType, destType)
Expand Down
2 changes: 1 addition & 1 deletion packages/pyright-internal/src/tests/samples/paramSpec41.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class A:
def __init__(self, x: int, y: int, z: str) -> None:
self.a = x

# This should generate an error.
# This should generate two errors.
@classmethod
def f(cls: Callable[P, Self], *args: P.args, **kwargs: P.kwargs) -> int:
return cls(*args, **kwargs).a
Expand Down
16 changes: 16 additions & 0 deletions packages/pyright-internal/src/tests/samples/self10.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# This sample tests that a class is not assignable to Self@Class.

from typing import Self


class A:
def self_arg(self, other: Self):
...

def call_self_arg(self):
# This should generate an error.
self.self_arg(A())

def get_instance(self) -> Self:
# This should generate an error.
return A()
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 @@ -1482,6 +1482,12 @@ test('Self9', () => {
TestUtils.validateResults(analysisResults, 0);
});

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

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

test('UnusedVariable1', () => {
const configOptions = new ConfigOptions('.');

Expand Down
2 changes: 1 addition & 1 deletion packages/pyright-internal/src/tests/typeEvaluator4.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1052,7 +1052,7 @@ test('ParamSpec40', () => {

test('ParamSpec41', () => {
const results = TestUtils.typeAnalyzeSampleFiles(['paramSpec41.py']);
TestUtils.validateResults(results, 1);
TestUtils.validateResults(results, 2);
});

test('ParamSpec42', () => {
Expand Down

0 comments on commit b03c411

Please sign in to comment.