Skip to content

mypy ignores isinstance #14197

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

Closed
ghost opened this issue Nov 26, 2022 · 1 comment
Closed

mypy ignores isinstance #14197

ghost opened this issue Nov 26, 2022 · 1 comment
Labels
bug mypy got something wrong

Comments

@ghost
Copy link

ghost commented Nov 26, 2022

After passing isinstance(obj, dict) successfully, obj is certainly of type dict.
mypy gives warnings as if there was no check.

To Reproduce

from typing import Callable

b: list[Callable[[str], str] | str] = []

def myfun(a: dict[str, str] | str) -> None:
    if isinstance(a, dict):
        b.append(lambda key: a[key])
    else:
        b.append(a)

myfun({"key": "hello"})
myfun("world")
print(b[0]("key"), b[1])  # hello world

Expected Behavior

There are no warnings.

Actual Behavior

1.py:7: error: Invalid index type "str" for "Union[Dict[str, str], str]"; expected type "Union[SupportsIndex, slice]"  [index]
1.py:13: error: "str" not callable  [operator]
Found 2 errors in 1 file (checked 1 source file)

Environment

  • Mypy version used: mypy 0.991 (compiled: no)
  • Mypy command-line flags:
  • Mypy configuration options from mypy.ini (and other config files):
  • Python version used: Python 3.11.0
@ghost ghost added the bug mypy got something wrong label Nov 26, 2022
@hauntsaninja
Copy link
Collaborator

hauntsaninja commented Nov 26, 2022

@hauntsaninja hauntsaninja closed this as not planned Won't fix, can't repro, duplicate, stale Nov 26, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong
Projects
None yet
Development

No branches or pull requests

1 participant