-
-
Notifications
You must be signed in to change notification settings - Fork 273
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
Conversation
There was a problem hiding this 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): |
There was a problem hiding this comment.
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 :)
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 ?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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)
From my point of view, this PR is ready and can be merged.
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. |
Description
Add
assigned_stmts
functions toMatchMapping
,MatchStar
, andMatchAs
. Except for the most basic case (bareMatchAs
), they returnutil.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).
Type of Changes
Related Issue
Ref pylint-dev/pylint#4685
Ref pylint-dev/pylint#4686