Skip to content

Commit

Permalink
Fixed regression that resulted in a false positive error when calling…
Browse files Browse the repository at this point in the history
… an abstract method on an abstract class that passes through the constraint solver (e.g. a generic decorator). This addresses #7105. (#7127)
  • Loading branch information
erictraut committed Jan 26, 2024
1 parent df5d48c commit 9c0c056
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
8 changes: 7 additions & 1 deletion packages/pyright-internal/src/analyzer/typeUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4170,7 +4170,13 @@ class ApplySolvedTypeVarsTransformer extends TypeVarTransformer {
// now with default type arguments.
if (this._options.unknownIfNotFound) {
replacement = mapSubtypes(replacement, (subtype) => {
if (isClassInstance(subtype) && !subtype.includeSubclasses) {
if (isClassInstance(subtype)) {
// If the includeSubclasses wasn't set, force it to be set by
// converting to/from an instantiable.
if (!subtype.includeSubclasses) {
subtype = ClassType.cloneAsInstance(ClassType.cloneAsInstantiable(subtype));
}

return specializeWithDefaultTypeArgs(subtype);
}

Expand Down
22 changes: 17 additions & 5 deletions packages/pyright-internal/src/tests/samples/abstractClass6.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,35 @@
# emit an error when the variable is instantiated.

from abc import ABC, abstractmethod
from typing import Type
from typing import Type, TypeVar


class Base(ABC):
@abstractmethod
def foo(self, x: int) -> int:
def method1(self, x: int) -> int:
pass


def foo1(base_cls: Type[Base]):
def func1(base_cls: Type[Base]):
base_cls()


def foo2():
def func2():
# This should generate an error.
Base()


def foo3(base_cls: type[Base]):
def func3(base_cls: type[Base]):
base_cls()


T = TypeVar("T")


def create_instance(cls: Type[T]) -> T:
return cls()


def func4():
base = create_instance(Base)
base.method1(1)

0 comments on commit 9c0c056

Please sign in to comment.