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

Enhanced the type(y) == x type guard logic to support the case wher… #9266

Merged
merged 1 commit into from
Oct 19, 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
2 changes: 1 addition & 1 deletion packages/pyright-internal/src/analyzer/typeGuards.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2328,7 +2328,7 @@ function narrowTypeForTypeIs(evaluator: TypeEvaluator, type: Type, classType: Cl
if (isPositiveTest) {
if (matches) {
if (ClassType.isSameGenericClass(subtype, classType)) {
return subtype;
return addConditionToType(subtype, classType.props?.condition);
}

return addConditionToType(ClassType.cloneAsInstance(classType), subtype.props?.condition);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,10 @@ def func7(val: Any):
reveal_type(val, expected_text="int | Any")


class CParent:
...
class CParent: ...


class CChild(CParent):
...
class CChild(CParent): ...


_TC = TypeVar("_TC", bound=CParent)
Expand Down Expand Up @@ -127,3 +125,14 @@ def method1(cls, v: str):
reveal_type(v, expected_text="G*")
else:
reveal_type(v, expected_text="str")


class H:
def __init__(self, x): ...


def func9[T: H](x: type[T], y: H) -> T:
if type(y) == x:
reveal_type(y, expected_text="H*")
return y
return x(y)
Loading