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

don't report unknown/any errors when casting #511

Merged
merged 1 commit into from
Jul 21, 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
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)
DetachHead marked this conversation as resolved.
Show resolved Hide resolved
Loading