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] how should assert_type handle the float/complex special case? #16171

Open
carljm opened this issue Feb 14, 2025 · 1 comment
Open
Labels
red-knot Multi-file analysis & type inference

Comments

@carljm
Copy link
Contributor

carljm commented Feb 14, 2025

Description

We follow the typing spec and other type checkers in implementing the special case that a float annotation means int | float, and a complex annotation means int | float | complex.

We don't currently follow other type checkers in trying to "hide" this special case by revealing just float or complex when the actual type we have is int | float or int | float | complex. Doing this leads to very strange-looking behaviors, like this from pyright:

def _(x: float):
    reveal_type(x)  # float
    if isinstance(x, float):
        reveal_type(x)  # float
    reveal_type(x)  # float | int ??

But this leads us to fail this type assertion:

assert_type(3.14, float)

Because we interpret float there as a type expression, meaning we take it to be int | float, but we actually infer just float for the literal.

@carljm carljm added the red-knot Multi-file analysis & type inference label Feb 14, 2025
@InSyncWithFoo
Copy link
Contributor

InSyncWithFoo commented Feb 15, 2025

I think float should be interpreted as int | float no matter the context. That helps with consistency.

Instead, knot_extensions should export BareFloat and BareComplex as public APIs for such use cases (credits go to @sharkdp):

type BareFloat = TypeOf[1.0]
type BareComplex = TypeOf[1j]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
red-knot Multi-file analysis & type inference
Projects
None yet
Development

No branches or pull requests

2 participants