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

Error when using Union as constraint for TypeVar #1412

Closed
cdce8p opened this issue Jun 8, 2021 · 3 comments
Closed

Error when using Union as constraint for TypeVar #1412

cdce8p opened this issue Jun 8, 2021 · 3 comments
Labels
bug Something isn't working fixed in next version (main) A fix has been implemented and will appear in an upcoming version

Comments

@cdce8p
Copy link

cdce8p commented Jun 8, 2021

Environment data

  • Language Server version: 2021.6.0
  • OS and version: darwin x64
  • Python version: 3.10.0b2
  • python.analysis.indexing: undefined
  • python.analysis.typeCheckingMode: basic

Code Snippet

from typing import TypeVar, Union

class A: ...
class B: ...
class C: ...
class D: ...

T = TypeVar("T", A, B, Union[C, D])


def do_something(value: T) -> T:
    ...

def func(value: Union[C, D]):
    value = do_something(value)  # error
    reveal_type(value)

Expected behaviour

No error. I'm not completely sure that's the way to do it, but it seems to work with mypy

test.py:16: note: Revealed type is "Union[test.C, test.D]"

Actual behaviour

Expression of type "T@do_something" cannot be assigned to declared type "C | D"
  Type "T@do_something" cannot be assigned to type "C | D"
    Type "A* | B* | C* | D*" cannot be assigned to type "C"
    Type "A* | B* | C* | D*" cannot be assigned to type "D"
    Type "A* | B* | C* | D*" cannot be assigned to type "C | D"
      "A*" is incompatible with "C"
      "B*" is incompatible with "C"
      "D*" is incompatible with "C"
      "A*" is incompatible with "D"
  ...

Argument of type "C | D" cannot be assigned to parameter "value" of type "T@do_something" in function "do_something"
  Type "C | D" is incompatible with constrained type variable "T"

Logs

--
@erictraut
Copy link
Contributor

Thanks for the bug report. Yes, the type checker logic wasn't properly dealing with the case where a constrained TypeVar included both union and non-union constraints. This is now working, and the fix will be included in the next release.

@erictraut erictraut added bug Something isn't working fixed in next version (main) A fix has been implemented and will appear in an upcoming version and removed needs investigation Could be an issue - needs investigation labels Jun 8, 2021
@cdce8p
Copy link
Author

cdce8p commented Jun 8, 2021

Thanks for the quick fix 🚀

@jakebailey
Copy link
Member

This issue has been fixed in version 2021.6.1, which we've just released. You can find the changelog here: https://github.com/microsoft/pylance-release/blob/main/CHANGELOG.md#202161-9-june-2021

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working fixed in next version (main) A fix has been implemented and will appear in an upcoming version
Projects
None yet
Development

No branches or pull requests

4 participants