Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed regression that resulted in a false positive error when calling… #7127

Merged
merged 1 commit into from
Jan 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Loading