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

Overly restrictive narrowing of TypeVar via isinstance #13956

Closed
huguesb opened this issue Oct 28, 2022 · 3 comments
Closed

Overly restrictive narrowing of TypeVar via isinstance #13956

huguesb opened this issue Oct 28, 2022 · 3 comments
Labels
bug mypy got something wrong

Comments

@huguesb
Copy link
Contributor

huguesb commented Oct 28, 2022

Bug Report

When a value whose type is a TypeVar is tested via isinstance, mypy so aggressively narrows types that it refuses to let them be broadened back to the original TypeVar, even when the value is untouched.

To Reproduce

https://mypy-play.net/?mypy=latest&python=3.10&gist=ac798d8bf4d5a39d1446d86bd5c30cf3

from typing import TypeVar

T = TypeVar('T')

def foo(t: T) -> T:
  if isinstance(t, str):
      return t + "."
  elif isinstance(t, dict):
      return t
  elif isinstance(t, list):
      return t
  else:
    return t

Expected Behavior

no type errors

Actual Behavior

main.py:8: error: Incompatible return value type (got "str", expected "T")
main.py:10: error: Incompatible return value type (got "Dict[Any, Any]", expected "T")
main.py:12: error: Incompatible return value type (got "List[Any]", expected "T")
Found 3 errors in 1 file (checked 1 source file)

Your Environment

  • Mypy version used: 0.971 (reproduces in all versions I've tried in the playground)
  • Mypy command-line flags: N/A
  • Python version used: 3.7 (reproduces in all versions I've tried in the playground)
@huguesb huguesb added the bug mypy got something wrong label Oct 28, 2022
@huguesb huguesb changed the title Overly restrictive narrowing of typevar Overly restrictive narrowing of TypeVar via isinstance Oct 28, 2022
@tmke8
Copy link
Contributor

tmke8 commented Oct 28, 2022

Same as #12800?

@kmillikin
Copy link

kmillikin commented Oct 28, 2022

The second and third errors do look like bugs. The type of t after isinstance(t, dict) should be revealed as <subclass of "T" and "Dict[Any, Any]"> and it should be OK to return this as a value of type T.

The first one is more subtle. After isinstance(t, str) the type of t should be revealed as <subclass of "T" and "str">. But the string concatenation operator returns a str and T might actually be a subtype of str, so it's not necessarily OK to return that as a value of type T.

@hauntsaninja
Copy link
Collaborator

Closing as duplicate of #10817

@hauntsaninja hauntsaninja closed this as not planned Won't fix, can't repro, duplicate, stale Jan 10, 2023
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

4 participants