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

Type captured by wildcard pattern is partially unknown - Why? #4058

Closed
carlos-souto opened this issue Mar 11, 2023 · 4 comments
Closed

Type captured by wildcard pattern is partially unknown - Why? #4058

carlos-souto opened this issue Mar 11, 2023 · 4 comments
Assignees
Labels
fixed in next version (main) A fix has been implemented and will appear in an upcoming version

Comments

@carlos-souto
Copy link

Environment data

  • Language Server version: 2023.3.20
  • OS and version: Windows 11 x64
  • Python version (& distribution if applicable, e.g. Anaconda): Python 3.11.2 64-bit

Code Snippet

print('LIST OF OPERATIONS:')
print('0: ADD')
print('1: SUBTRACT')
print('2: MULTIPLY')
print('3: DIVIDE')

operation: str = ''
a = 0
b = 0
for i in range(3):

    if i == 0:
        user: str = input('STEP 1, CHOOSE OPERATION: ')
        operation = (
            'ADD'      if user == '0' else
            'SUBTRACT' if user == '1' else
            'MULTIPLY' if user == '2' else
            'DIVIDE'   if user == '3' else
            ''
        )

    elif i == 1:
        print('STEP 2, CHOOSE a AND b')
        try:
            a: int = int(input('a = '))
        except:
            print('invalid a, assuming 0')
        try:
            b: int = int(input('b = '))
        except:
            print('invalid b, assuming 0')

    else:
        print('STEP 3, RESULT:')
        match operation:                     # (variable) operation: Literal['', 'ADD', 'SUBTRACT', 'MULTIPLY', 'DIVIDE']
            case 'ADD':      print(a + b)
            case 'SUBTRACT': print(a - b)
            case 'MULTIPLY': print(a * b)
            case 'DIVIDE':   print(a / b)
            case _:                          # Type captured by wildcard pattern is partially unknown (why?)
                print('invalid operation')

In the match statement, the variable operation is correctly identified as (variable) operation: Literal['', 'ADD', 'SUBTRACT', 'MULTIPLY', 'DIVIDE']. So why is pylance reporting the following in the case _: case statement:

_: Unknown
Type captured by wildcard pattern is partially unknown
Type of "_" is "Unknown | Literal['']"

image

image

My expectation is that no error should be reported.

Please note that I am not implying that the code snippet shown above makes very much sense. The for loop is unnecessary; however, I found that it's the loop that creates this unwanted behavior in the case _: statement. The code snippet is just a minimal working example that showcases the issue that I have in a real-world piece of code.

Logs

Attached.
log.txt

@erictraut
Copy link
Contributor

Yes, this looks like a bug in the type narrowing logic for pattern matching. I'll investigate.

@carlos-souto
Copy link
Author

carlos-souto commented Mar 11, 2023

I believe this issue appeared with the new update of Pylance (v2023.3.20). Before that, I didn't have this problem.

erictraut pushed a commit to microsoft/pyright that referenced this issue Mar 11, 2023
…re when a wildcard pattern is used in a `case` statement within a loop. This addresses microsoft/pylance-release#4058.
@erictraut
Copy link
Contributor

Thanks for the bug report. This will be fixed in a future release.

@judej judej added the fixed in next version (main) A fix has been implemented and will appear in an upcoming version label Mar 13, 2023
@debonte
Copy link
Contributor

debonte commented Mar 16, 2023

This issue has been fixed in prerelease version 2023.3.22, which we've just released. You can find the changelog here: CHANGELOG.md

@debonte debonte closed this as completed Mar 16, 2023
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