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

Pylance sometimes infers str and int types as Literal types, which is something I have never wanted #697

Closed
jlxgcgcg opened this issue Dec 7, 2020 · 8 comments
Labels
fixed in next version (main) A fix has been implemented and will appear in an upcoming version

Comments

@jlxgcgcg
Copy link

jlxgcgcg commented Dec 7, 2020

Environment data

  • Language Server version: 2020.12.0
  • OS and version: macos catalina 10.15.7
  • Python version (& distribution if applicable, e.g. Anaconda): Anaconda 3.8.5

Expected behaviour

This code should be well-typed:

xs = [("me", 1)]

def append_to_xs(who: str, count: int) -> None:
    xs.append((who, count))

Actual behaviour

I get an error:

Argument of type "tuple[str, int]" cannot be assigned to parameter "__object" of type "tuple[Literal['me'], Literal[1]]" in function "append"
  Tuple entry 1 is incorrect type
    "str" cannot be assigned to type "Literal['me']"

If I add a type hint like

xs: List[Tuple[str, int]] = [("me", 1)]

then it works, but I shouldn't have to do that. Having the types be Literal['me'] and Literal[1] is something that I've basically never wanted (and I can't believe anyone else wants) and should not be the default inference.

--

Here's another instance of the same problem:

d = {"a": ("b", 1)}
d["c"] = ("d", 3)  # pylance error
@github-actions github-actions bot added the triage label Dec 7, 2020
@erictraut
Copy link
Contributor

This is largely a duplicate of this issue: microsoft/pyright#1249.

@jlxgcgcg
Copy link
Author

jlxgcgcg commented Dec 7, 2020

thanks, I googled "pylance github" and ended up at this repo, and then I actually searched the issues for "literal" before opening this one, but I didn't know to look in the pyright repo instead

@jakebailey
Copy link
Member

The fix for that issue will be in this week's Pylance release, so we'll be able to see what happens afterward.

@erictraut
Copy link
Contributor

@jlxgcgcg, no worries. I wouldn't have expected you to know about the other repo. Thanks for submitting the suggestion.

@judej judej added the fixed in next version (main) A fix has been implemented and will appear in an upcoming version label Dec 8, 2020
@github-actions github-actions bot removed the triage label Dec 8, 2020
@jakebailey
Copy link
Member

This issue has been fixed in version 2020.12.1, which we've just released. You can find the changelog here: https://github.com/microsoft/pylance-release/blob/master/CHANGELOG.md#2020121-9-december-2020

@tthommes
Copy link

tthommes commented Jul 20, 2023

This issue seems to be hot again in the current version (2023.7.30)!
When I try to pass an Enum-value e.g.

class MyEnum(Enum):
  A = 0x1
  B = 0x2

to a function parameter type-hinted as Integral e.g.

from numbers import Integral
def myfunc(a: Integral):
  pass

myfunc(MyEnum.A.value)

pylance infers Literal[] for the Enum-value and complains about incompatibility.

@erictraut
Copy link
Contributor

@tthommes, this issue was fixed and closed nearly three years ago. If you think that you are experiencing a new bug, please file a new issue and provide a minimal, self-contained code sample. In your code sample above, for example, I don't know what the symbol Integral refers to.

@tthommes
Copy link

I opened a new issue and fixed the code-example above.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
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

5 participants