Skip to content

Commit

Permalink
don't report unknown/any errors when casting
Browse files Browse the repository at this point in the history
  • Loading branch information
DetachHead committed Jul 21, 2024
1 parent 0c1ead1 commit c2b3d46
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 11 deletions.
15 changes: 6 additions & 9 deletions packages/pyright-internal/src/analyzer/typeEvaluator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5305,7 +5305,7 @@ export function createTypeEvaluator(
}

// See if we need to log an "unknown member access" diagnostic.
let skipPartialUnknownCheck = typeResult.isIncomplete;
let skipPartialUnknownCheck = typeResult.isIncomplete || flags & EvalFlags.AllowAnyOrUnknown;

// Don't report an error if the type is a partially-specialized
// class being passed as an argument. This comes up frequently in
Expand Down Expand Up @@ -10269,7 +10269,8 @@ export function createTypeEvaluator(
let castFromType = getTypeOfArgument(
argList[1],
/* inferenceContext */ undefined,
/* signatureTracker */ undefined
/* signatureTracker */ undefined,
EvalFlags.AllowAnyOrUnknown
).type;

if (castFromType.props?.specialForm) {
Expand Down Expand Up @@ -20767,7 +20768,8 @@ export function createTypeEvaluator(
function getTypeOfArgument(
arg: FunctionArgument,
inferenceContext: InferenceContext | undefined,
signatureTracker: UniqueSignatureTracker | undefined
signatureTracker: UniqueSignatureTracker | undefined,
flags: EvalFlags = EvalFlags.None
): TypeResult {
if (arg.typeResult) {
const type = arg.typeResult.type;
Expand All @@ -20781,12 +20783,7 @@ export function createTypeEvaluator(

// If there was no defined type provided, there should always
// be a value expression from which we can retrieve the type.
const typeResult = getTypeOfExpression(
arg.valueExpression,
/* flags */ undefined,
inferenceContext,
signatureTracker
);
const typeResult = getTypeOfExpression(arg.valueExpression, flags, inferenceContext, signatureTracker);

if (signatureTracker) {
typeResult.type = ensureFunctionSignaturesAreUnique(
Expand Down
3 changes: 3 additions & 0 deletions packages/pyright-internal/src/analyzer/typeEvaluatorTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,9 @@ export const enum EvalFlags {
// with the second argument to isinstance and issubclass calls.
IsinstanceArg = 1 << 29,

/** don't report an error if the type is `Any` or "Unknown" */
AllowAnyOrUnknown = 1 << 30,

// Defaults used for evaluating the LHS of a call expression.
CallBaseDefaults = NoSpecialize,

Expand Down
7 changes: 5 additions & 2 deletions packages/pyright-internal/src/tests/samples/reportAny.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Any, Callable
from typing import Any, Callable, cast


def foo(bar: Any) -> Any:
Expand All @@ -17,4 +17,7 @@ def baz() -> None: ...

match(bar):
case _:
...
...

cast(int, bar)
cast(int, bar.asdf)

0 comments on commit c2b3d46

Please sign in to comment.