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

pylint doesn't understand returns from functions using match case #5288

Closed
wangoland opened this issue Nov 10, 2021 · 5 comments · Fixed by #8385
Closed

pylint doesn't understand returns from functions using match case #5288

wangoland opened this issue Nov 10, 2021 · 5 comments · Fixed by #8385
Labels
False Positive 🦟 A message is emitted but nothing is wrong with the code High priority Issue with more than 10 reactions Match case Needs astroid update Needs an astroid update (probably a release too) before being mergable python 3.10
Milestone

Comments

@wangoland
Copy link

Bug description

Pylint displays the following error code:
Error message: 'Attempting to unpack a non-sequence'
Details:

[{
	"resource": "/c:/[REDACTED]/main.py",
	"owner": "python",
	"code": "unpacking-non-sequence",
	"severity": 8,
	"message": "Attempting to unpack a non-sequence",
	"source": "pylint",
	"startLineNumber": 124,
	"startColumn": 5,
	"endLineNumber": 124,
	"endColumn": 5
}]

When the following code snippet is used:

def check_user_can_add_friend(username):
    # Get subscription level
    subscription_level = user_get_subscription_level(username)
    number_friend_requests = user_count_friend_requests(username)
    number_friends = number_friend_requests + user_count_friends(username)

    match subscription_level:
        case "BASIC":
            if number_friends < 5:
                return True, " "
            else:
                return False, "'" + username + "' can only have a maximum of 5 friends"
        case "GOLD":
            if number_friends < 20:
                return True, " "
            else:
                return False, "'" + username + "' can only have a maximum of 20 friends"
        case _:
            return True, " "

If the above match statement is changed to an if and elseif statement then the error reported by pylance for un-packing will go away

Configuration

No response

Command used

Automatically within VS Code via PyLance, the pylance team advised me to raise this with yourselves:
https://github.com/microsoft/pylance-release/issues/2060

Pylint output

Error message: 'Attempting to unpack a non-sequence'
Details:

[{
	"resource": "/c:/[REDACTED]/main.py",
	"owner": "python",
	"code": "unpacking-non-sequence",
	"severity": 8,
	"message": "Attempting to unpack a non-sequence",
	"source": "pylint",
	"startLineNumber": 124,
	"startColumn": 5,
	"endLineNumber": 124,
	"endColumn": 5
}]

Expected behavior

Pylance should not show any error for this line as it can be correctly unpacked.

Pylint version

pylint 2.11.1
astroid 2.8.4
Python 3.10.0 (tags/v3.10.0:b494f59, Oct  4 2021, 19:00:18) [MSC v.1929 64 bit (AMD64)]

OS / Environment

Windows 10 Enterprise (10.0.18363 Build 18363)

Additional dependencies

No response

@Sasszem
Copy link

Sasszem commented Jan 24, 2022

Maybe a bit simpler code to reproduce:

def foo() -> tuple[int, int]:
    match 1:
        case int(a):
            return a, 5
        case None:
            return 0, 0

x, y = foo()

Adding a return None, None or similar after the match statement makes the false positive disappear, but raising an exception does not.

@Pierre-Sassoulas Pierre-Sassoulas removed the Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling label Jan 24, 2022
@Pierre-Sassoulas Pierre-Sassoulas added Needs reproduction 🔍 Need a way to reproduce it locally on a maintainer's machine False Positive 🦟 A message is emitted but nothing is wrong with the code python 3.10 Needs investigation 🔬 A bug or crash where it's not immediately obvious what is happenning and removed Bug 🪲 Needs reproduction 🔍 Need a way to reproduce it locally on a maintainer's machine labels Jul 6, 2022
@Pierre-Sassoulas
Copy link
Member

I can reproduce with @Sasszem example.

@jacobtylerwalls jacobtylerwalls changed the title Attempting to unpack a non-sequence, raised when using a match method returning a tuple pylint doesn't understand returns from functions using match case Feb 13, 2023
@jacobtylerwalls jacobtylerwalls added Needs astroid update Needs an astroid update (probably a release too) before being mergable and removed Needs investigation 🔬 A bug or crash where it's not immediately obvious what is happenning labels Feb 13, 2023
@jacobtylerwalls
Copy link
Member

We need astroid to understand returns from match statements:

>>> import astroid
>>> code = """
... def check_user_can_add_friend(username):
...     # Get subscription level
...     subscription_level = user_get_subscription_level(username)
...     number_friend_requests = user_count_friend_requests(username)
...     number_friends = number_friend_requests + user_count_friends(username)
... 
...     match subscription_level:
...         case "BASIC":
...             if number_friends < 5:
...                 return True, " "
...             else:
...                 return False, "'" + username + "' can only have a maximum of 5 friends"
...         case "GOLD":
...             if number_friends < 20:
...                 return True, " "
...             else:
...                 return False, "'" + username + "' can only have a maximum of 20 friends"
...         case _:
...             return True, " "
...  
... z = check_user_can_add_friend('uu')  #@
... """
>>> n = astroid.extract_node(code)
>>> n.targets[0].inferred()
[<Const.NoneType l.None at 0x1105ff090>]

@jacobtylerwalls
Copy link
Member

Closing as I don't have immediate plans to work on this.

@jacobtylerwalls
Copy link
Member

Whoops, wrong tab. I definitely have plans to work on this. I just put up a PR on astroid: pylint-dev/astroid#2042

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
False Positive 🦟 A message is emitted but nothing is wrong with the code High priority Issue with more than 10 reactions Match case Needs astroid update Needs an astroid update (probably a release too) before being mergable python 3.10
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants