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

Fix issues when inferring match variables #1093

Merged
merged 5 commits into from Jul 10, 2021

Conversation

cdce8p
Copy link
Member

@cdce8p cdce8p commented Jul 10, 2021

Description

Add assigned_stmts functions to MatchMapping, MatchStar, and MatchAs. Except for the most basic case (bare MatchAs), they return util.Uninferable. This will fix the crashes.

Inferring anything more will require checking if a case pattern can be matched at all, and if it can which part of the pattern matches which of the subject. That isn't implement (yet).

# Code used for validation that pylint doesn't crash

msg = 42
match msg:
    case (1, *rest):
        if rest != 2:
            pass
    case {1: _, **rest2}:
        if rest2 != 2:
            pass
    case y:
        if y != 2:
            pass

Type of Changes

Type
🐛 Bug fix

Related Issue

Ref pylint-dev/pylint#4685
Ref pylint-dev/pylint#4686

Copy link
Member

@Pierre-Sassoulas Pierre-Sassoulas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, I hope it can go in astroid 2.6.3. Thank you for fixing the crash and pave the way for the proper inference to be implemented.

@@ -55,10 +56,10 @@
)
from astroid.manager import AstroidManager

try:
if sys.version_info >= (3, 8):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be py38_plus from consts :)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if PY38_PLUS can't be inferred by mypy. I added not PY310 to the test cases but that's about it what will work.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand, what is the problem with mypy ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mypy doesn't really infer if an if-block is reachable. It only checks specific conditions one of them being sys.version_info. https://mypy.readthedocs.io/en/stable/common_issues.html#python-version-and-system-platform-checks

Btw we had a similar issue with the overload-guard in astroid/rebuilder.py. See #1069

--
For tests PY310_PLUS and so on are fine, but for everything else directly using sys.version_info would be my preferred way of doing it. Although there is no need to replace all current occurrences, just if / when it makes sense, i.e. to fix some typing issue.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could remove the constant entirely in another MR if they do not make sense (well if they're making mypy worse). Originally I refactored because a single constant is better than n constants with the same name in n files.

Copy link
Member Author

@cdce8p cdce8p Jul 10, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In most cases they don't change anything. Just here and for the aforementioned overloads in astroid/rebuilder.py. I think we have more important things to do than to remove these.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We sure do (but those kind of refactor take a single keystroke in pycharm)

tests/unittest_protocols.py Outdated Show resolved Hide resolved
@cdce8p
Copy link
Member Author

cdce8p commented Jul 10, 2021

Looks good, I hope it can go in astroid 2.6.3.

From my point of view, this PR is ready and can be merged.

Thank you for fixing the crash and pave the way for the proper inference to be implemented.

I do have some ideas how to do it, but still have to think about if it would work. So will probably take a while.

@cdce8p cdce8p merged commit c9c4983 into pylint-dev:main Jul 10, 2021
@cdce8p cdce8p deleted the fix-match-assigned-stmts branch July 10, 2021 21:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants