Skip to content

Commit

Permalink
Fixed bug that leads to false positive when a method is overridden by…
Browse files Browse the repository at this point in the history
… a polymorphic method. This addresses #9417. (#9434)
  • Loading branch information
erictraut authored Nov 9, 2024
1 parent 240fa3c commit ef4fdb1
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 12 deletions.
3 changes: 2 additions & 1 deletion packages/pyright-internal/src/analyzer/typeEvaluator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27275,7 +27275,8 @@ export function createTypeEvaluator(

// Now check the return type.
const baseReturnType = getEffectiveReturnType(baseMethod);
const overrideReturnType = getEffectiveReturnType(overrideMethod);
const overrideReturnType = solveAndApplyConstraints(getEffectiveReturnType(overrideMethod), constraints);

if (
!assignType(
baseReturnType,
Expand Down
30 changes: 19 additions & 11 deletions packages/pyright-internal/src/tests/samples/methodOverride4.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,31 @@
_T3 = TypeVar("_T3")


class Base(Generic[_TSource]):
class BaseA(Generic[_TSource]):
@abstractmethod
def method1(
self, mapper: Callable[[_TSource, _T1], _TResult], other: "Base[_T1]"
) -> "Base[_TResult]":
self, mapper: Callable[[_TSource, _T1], _TResult], other: "BaseA[_T1]"
) -> "BaseA[_TResult]":
raise NotImplementedError


class Subclass1(Base[_TSource]):
class SubclassA1(BaseA[_TSource]):
def method1(
self, mapper: Callable[[_TSource, _T2], _TResult], other: Base[_T2]
) -> Base[_TResult]:
return Subclass2()
self, mapper: Callable[[_TSource, _T2], _TResult], other: BaseA[_T2]
) -> BaseA[_TResult]:
return SubclassA2()


class Subclass2(Base[_TSource]):
class SubclassA2(BaseA[_TSource]):
def method1(
self, mapper: Callable[[_TSource, _T3], _TResult], other: Base[_T3]
) -> Base[_TResult]:
return Subclass2()
self, mapper: Callable[[_TSource, _T3], _TResult], other: BaseA[_T3]
) -> BaseA[_TResult]:
return SubclassA2()


class BaseB:
def f(self, v: str) -> str: ...


class SubclassB1(BaseB):
def f[T](self, v: T) -> T: ...

0 comments on commit ef4fdb1

Please sign in to comment.