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

fix strictGenericNarrowing causing false positive reportUnnecessaryIsInstance errors when narrowing Callable #909

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
5 changes: 2 additions & 3 deletions packages/pyright-internal/src/analyzer/typeUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1090,9 +1090,8 @@ export const shouldUseVarianceForSpecialization = (typeToNarrow: Type, strictGen
return allSubtypes(
typeToNarrow,
(subtype) =>
subtype.category !== TypeCategory.Class ||
// !ClassType.isSameGenericClass(subtype, narrowToType) ||
subtype.shared.typeParams.length === 0
(subtype.category !== TypeCategory.Class || subtype.shared.typeParams.length === 0) &&
(subtype.category !== TypeCategory.Function || !subtype.priv.isCallableWithTypeArgs)
);
};

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Any, Never, assert_type, runtime_checkable, Protocol, Iterable, Iterator, MutableMapping, Reversible
from typing import Any, Never, assert_type, runtime_checkable, Protocol, Iterable, Iterator, MutableMapping, Reversible, Callable


class Covariant[T]:
Expand Down Expand Up @@ -115,4 +115,18 @@ def _(
value: str | Foo[str],
):
if isinstance(value, Foo):
assert_type(value, Foo[str])
assert_type(value, Foo[str])

def _(f: Callable[[], None]):
if isinstance(f, staticmethod):
# narrowing Callable this way doesn't work so we use the old method.
# see https://github.com/DetachHead/basedpyright/issues/905
assert_type(f, staticmethod[..., Any])

class CallableProtocol[**P, T](Protocol):
def __call__(self, *args: P.args, **kwargs: P.kwargs) -> T: ...


def _(f: CallableProtocol[[], None]):
if isinstance(f, staticmethod):
assert_type(f, staticmethod[[], None])
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Any, assert_type, runtime_checkable, Protocol, Iterable, Iterator, MutableMapping, Reversible
from typing import Any, assert_type, runtime_checkable, Protocol, Iterable, Iterator, MutableMapping, Reversible, Callable


class Covariant[T]:
Expand Down Expand Up @@ -115,4 +115,18 @@ def _(
value: str | Foo[str],
):
if isinstance(value, Foo):
assert_type(value, Foo[str])
assert_type(value, Foo[str])

def _(f: Callable[[], None]):
if isinstance(f, staticmethod):
# narrowing Callable this way doesn't work so we use the old method.
# see https://github.com/DetachHead/basedpyright/issues/905
assert_type(f, staticmethod[..., Any])

class CallableProtocol[**P, T](Protocol):
def __call__(self, *args: P.args, **kwargs: P.kwargs) -> T: ...


def _(f: CallableProtocol[[], None]):
if isinstance(f, staticmethod):
assert_type(f, staticmethod[[], None])
Loading