diff --git a/packages/pyright-internal/src/analyzer/constraintSolver.ts b/packages/pyright-internal/src/analyzer/constraintSolver.ts index 2998dc6900be..1dbc202d9f5f 100644 --- a/packages/pyright-internal/src/analyzer/constraintSolver.ts +++ b/packages/pyright-internal/src/analyzer/constraintSolver.ts @@ -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) diff --git a/packages/pyright-internal/src/tests/samples/paramSpec41.py b/packages/pyright-internal/src/tests/samples/paramSpec41.py index 8226a4224de7..db20eadbc0ab 100644 --- a/packages/pyright-internal/src/tests/samples/paramSpec41.py +++ b/packages/pyright-internal/src/tests/samples/paramSpec41.py @@ -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 diff --git a/packages/pyright-internal/src/tests/samples/self10.py b/packages/pyright-internal/src/tests/samples/self10.py new file mode 100644 index 000000000000..6cdda68e82a6 --- /dev/null +++ b/packages/pyright-internal/src/tests/samples/self10.py @@ -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() diff --git a/packages/pyright-internal/src/tests/typeEvaluator1.test.ts b/packages/pyright-internal/src/tests/typeEvaluator1.test.ts index e5069b88bc23..3fefcbd100e4 100644 --- a/packages/pyright-internal/src/tests/typeEvaluator1.test.ts +++ b/packages/pyright-internal/src/tests/typeEvaluator1.test.ts @@ -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('.'); diff --git a/packages/pyright-internal/src/tests/typeEvaluator4.test.ts b/packages/pyright-internal/src/tests/typeEvaluator4.test.ts index 0824c70c0c04..558528445297 100644 --- a/packages/pyright-internal/src/tests/typeEvaluator4.test.ts +++ b/packages/pyright-internal/src/tests/typeEvaluator4.test.ts @@ -1052,7 +1052,7 @@ test('ParamSpec40', () => { test('ParamSpec41', () => { const results = TestUtils.typeAnalyzeSampleFiles(['paramSpec41.py']); - TestUtils.validateResults(results, 1); + TestUtils.validateResults(results, 2); }); test('ParamSpec42', () => {