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

element of tuple (w, *args) inferred with 'Any' although both 'w' and 'args' have type 'str' #991

Closed
PatrickBourdon opened this issue Feb 25, 2021 · 4 comments
Labels
enhancement New feature or request fixed in next version (main) A fix has been implemented and will appear in an upcoming version

Comments

@PatrickBourdon
Copy link

Environment data

  • Windows-10-10.0.18362-SP0
  • python(3.9.2 (tags/v3.9.2:1a79785, Feb 19 2021, 13:44:55) [MSC v.1928 64 bit (AMD64)])
  • Visual Code Studio: 1.53.2
  • extension pylance: v2021.2.4
  • type checking mode: strict

Expected behaviour

A problem reported.

Actual behaviour

No problemreported.
see example below

Code Snippet / Additional information

def func1(w: str, *args: str) -> list[str]:
    y = [ z.lower() for z in (w, *args) ]
    y.append([1, 'a'])
    return y  
# pylance infers that 'z' has type 'Any', and that 'y' has type list[Any].
# Therefore lower() appears in white, and you can append anything to 'y'
# without a problem be reported.

# The issue disappears when the tuple is changed to a list:
def func2(w: str, *args: str) -> list[str]:
    return [ z.lower() for z in [w, *args] ]
@judej
Copy link
Contributor

judej commented Feb 25, 2021

@erictraut, could you please look at this?

@judej judej added the needs investigation Could be an issue - needs investigation label Feb 25, 2021
@github-actions github-actions bot removed the triage label Feb 25, 2021
@erictraut
Copy link
Contributor

It's correct either way. Currently, a tuple expression that includes an unpacked open-ended (unknown length) tuple is translated to a tuple[Any, ...]. I can add additional logic to combine the types in the particular case where all of the other entries in the tuple share the same type as the unpacked open-ended tuple. It's a very special case, but it's reasonable to expect that they will be combined correctly. The list expression logic contains logic to handle this special case already.

@erictraut
Copy link
Contributor

This will be fixed in the next release. Here's the commit: microsoft/pyright@b65928c.

@erictraut erictraut added enhancement New feature or request 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 Feb 25, 2021
@jakebailey
Copy link
Member

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

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