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

Added missing check for the use of a subscripted Callable within an… #6103

Merged
merged 1 commit into from
Oct 7, 2023
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
4 changes: 3 additions & 1 deletion packages/pyright-internal/src/analyzer/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3797,7 +3797,9 @@ export class Checker extends ParseTreeWalker {
break;

case TypeCategory.Function:
isSupported = TypeBase.isInstantiable(subtype);
if (!TypeBase.isInstantiable(subtype) || subtype.isCallableWithTypeArgs) {
isSupported = false;
}
break;

case TypeCategory.Union:
Expand Down
2 changes: 2 additions & 0 deletions packages/pyright-internal/src/analyzer/typeEvaluator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14255,6 +14255,8 @@ export function createTypeEvaluator(importLookup: ImportLookup, evaluatorOptions
functionType.details.typeVarScopeId = ParseTreeUtils.getScopeIdForNode(errorNode);

if (typeArgs && typeArgs.length > 0) {
functionType.isCallableWithTypeArgs = true;

if (typeArgs[0].typeList) {
const typeList = typeArgs[0].typeList;
let sawUnpacked = false;
Expand Down
5 changes: 5 additions & 0 deletions packages/pyright-internal/src/analyzer/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1454,6 +1454,11 @@ export interface FunctionType extends TypeBase {
// for that call and any other signatures that were passed as
// arguments to it.
trackedSignatures?: SignatureWithOffsets[];

// If this function is created with a "Callable" annotation with
// type arguments? This allows us to detect and report an error
// when this is used in an isinstance call.
isCallableWithTypeArgs?: boolean;
}

export namespace FunctionType {
Expand Down
14 changes: 13 additions & 1 deletion packages/pyright-internal/src/tests/samples/isinstance3.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@


from abc import abstractmethod
from typing import Any, Generic, Sequence, Tuple, Type, TypeVar, Union
from typing import Any, Callable, Generic, Sequence, Tuple, Type, TypeVar, Union


_T = TypeVar("_T", int, str)
Expand Down Expand Up @@ -55,3 +55,15 @@ def execute(self, var: Union[_T, Tuple[_T]]) -> None:

def func1(exceptions: Sequence[type[BaseException]], exception: Exception):
return isinstance(exception, tuple(exceptions))


if isinstance(a, Callable):
...

# This should generate an error because a subscripted Callable
# will result in a runtime exception.
if isinstance(a, Callable[[], Any]):
...

if isinstance(a, type(len)):
...
4 changes: 2 additions & 2 deletions packages/pyright-internal/src/tests/typeEvaluator2.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -285,11 +285,11 @@ test('isInstance3', () => {

configOptions.defaultPythonVersion = PythonVersion.V3_9;
const analysisResults1 = TestUtils.typeAnalyzeSampleFiles(['isinstance3.py'], configOptions);
TestUtils.validateResults(analysisResults1, 1);
TestUtils.validateResults(analysisResults1, 2);

configOptions.defaultPythonVersion = PythonVersion.V3_10;
const analysisResults2 = TestUtils.typeAnalyzeSampleFiles(['isinstance3.py'], configOptions);
TestUtils.validateResults(analysisResults2, 1);
TestUtils.validateResults(analysisResults2, 2);
});

test('isInstance4', () => {
Expand Down
Loading