Skip to content

Inconsistent feeling handling of Any in binder #20870

@hauntsaninja

Description

@hauntsaninja
from typing import Any, Final

CONSTANT: Final = 2015

def good(result: dict[str, Any]) -> None:
    code = 'asdf'
    if isinstance(result, dict):
        code = result.get('code', code)
    reveal_type(code)  # N: Revealed type is "Any"
    if code == CONSTANT:
        reveal_type(code)  # N: Revealed type is "Any"

def bad(result: Any) -> None:
    code = 'asdf'
    if isinstance(result, dict):
        code = result.get('code', code)
    reveal_type(code)  # N: Revealed type is "builtins.str"
    if code == CONSTANT:
        reveal_type(code)  # N: Revealed type is "builtins.str"

Based on real world code for rotki

This feels a little inconsistent / violating of gradual typing
Not exactly sure what if anything we should do, though

Found this via #20660 which makes --warn-unreachable flag raise a diagnostic on the second comparison

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrongtopic-type-narrowingConditional type narrowing / binder

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions