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

[red-knot] Incorrect Narrowing Constraint Applied After ExprIf #14588

Closed
cake-monotone opened this issue Nov 25, 2024 · 1 comment · Fixed by #14590
Closed

[red-knot] Incorrect Narrowing Constraint Applied After ExprIf #14588

cake-monotone opened this issue Nov 25, 2024 · 1 comment · Fixed by #14590
Labels
bug Something isn't working red-knot Multi-file analysis & type inference

Comments

@cake-monotone
Copy link
Contributor

cake-monotone commented Nov 25, 2024

def bool_instance() -> bool:
    return True

x: Literal[42, "hello"] = 42 if bool_instance() else "hello"
reveal_type(x)  # revealed: Literal[42] | Literal["hello"]

_ = 1234 if isinstance(x, str) else 1234

# The `isinstance` test incorrectly narrows the type of `x`.
# As a result, `x` is revealed as Literal["hello"], but it should remain Literal[42, "hello"].
reveal_type(x)  # revealed: Literal["hello"]

This issue is not limited to isinstance. Other narrowing expressions or patterns also cause the same issue.

@cake-monotone
Copy link
Contributor Author

cake-monotone commented Nov 25, 2024

If this isn't resolved first, #14550 could break some tests.
I'll take a look into it first.

@cake-monotone cake-monotone changed the title [red-knot] Incorrect Narrowing Constraint Applied After IfExp [red-knot] Incorrect Narrowing Constraint Applied After ExprIf Nov 25, 2024
@MichaReiser MichaReiser added the red-knot Multi-file analysis & type inference label Nov 25, 2024
@AlexWaygood AlexWaygood added the bug Something isn't working label Nov 25, 2024
@carljm carljm closed this as completed in f98eebd Nov 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working red-knot Multi-file analysis & type inference
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants