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

reveal_locals is hella inconsistent / top level variables not listed / narrowed type inconsistent with reveal_type #11956

Open
KotlinIsland opened this issue Jan 10, 2022 · 2 comments
Labels
bug mypy got something wrong topic-reveal-type reveal_type() and reveal_locals()

Comments

@KotlinIsland
Copy link
Contributor

KotlinIsland commented Jan 10, 2022

a: int
b = 1
if isinstance(a, bool):
    reveal_locals()  # "b: int", I would expect "a: bool, b: int"
    reveal_type(a)  # "a: bool" ✔
    
def foo(a: int) -> None:
    b: int
    reveal_locals()  # "a: int, b: int", this is correct, but inconsistent with top level
    reveal_type(a)
a: int = 1
reveal_locals() # nothing 😥
@KotlinIsland KotlinIsland added the bug mypy got something wrong label Jan 10, 2022
@erictraut
Copy link

I would not expect reveal_locals to emit the narrowed types of the locals, whereas I would expect reveal_type to take narrowing into account. This appears to be what mypy is doing. This is consistent with pyright, BTW.

It looks like reveal_locals is not currently displaying locals with an inferred type (such as b in your example above), which looks like a bug.

@JelleZijlstra JelleZijlstra added the topic-reveal-type reveal_type() and reveal_locals() label Mar 19, 2022
@JonathonReinhart
Copy link

I was also surprised to see the difference between reveal_type and reveal_locals with regard to type narrowing.

def test() -> None:
    foo = None
    if foo is None:
        foo = "never None"

    reveal_locals()     # Revealed local types are:
                        #    foo: Union[builtins.str, None]

    reveal_type(foo)    # Revealed type is 'builtins.str'

I would not expect reveal_locals to emit the narrowed types of the locals, whereas I would expect reveal_type to take narrowing into account.

Interesting. Why do you feel that this is the correct behavior?

If this is working as intended and not a bug, then perhaps the docs for reveal_locals() should be updated to explain this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong topic-reveal-type reveal_type() and reveal_locals()
Projects
None yet
Development

No branches or pull requests

4 participants