Skip to content

Commit

Permalink
fix reportInvalidCast false positive when casting to/from None
Browse files Browse the repository at this point in the history
  • Loading branch information
DetachHead committed Aug 14, 2024
1 parent 5ca0b65 commit 223ebe0
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/pyright-internal/src/analyzer/typeEvaluator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10328,7 +10328,7 @@ export function createTypeEvaluator(
return isTypeSame(leftType, rightType);
}

if (isNoneInstance(leftType) || isNoneInstance(rightType)) {
if (checkEq && (isNoneInstance(leftType) || isNoneInstance(rightType))) {
return isTypeSame(leftType, rightType);
}

Expand Down
2 changes: 2 additions & 0 deletions packages/pyright-internal/src/tests/samples/cast.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ def foo(str_: str, int_or_str: int | str, dict_int_or_none: dict[str, int | None
cast(int | str, str_) # wider, no error
cast(Never, int_or_str) # narrower, no error
cast(int, int_or_str) # narrower, no error
cast(None, object()) # narrower, no error
cast(object, None) # wider, no error
cast(Mapping[str, int], dict_int_or_none) # wider (ignore type parameters), no error
cast(dict[str, int], mapping) # wider, no error
cast(dict[str, bytes], mapping) # xfail, https://github.com/DetachHead/basedpyright/issues/259

0 comments on commit 223ebe0

Please sign in to comment.