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

Basic match statement support #291

Merged
merged 3 commits into from
Sep 18, 2022
Merged

Conversation

kreathon
Copy link
Contributor

@kreathon kreathon commented Sep 16, 2022

Example

@dataclass
class X:
    a: int

x = input()

match x:
    case X(a=0):
        ...
    case _:
        ...

At the moment, a would be seen as unused. However, as nicely described by @exoriente, the code means something like

    if is_instance(x, X) and x.a == 0:

So it is not a normal / dead "assignment" and a should not be seen as unused.

Implementation

The actual code change is really trivial, since most of the desired behavior (for the match statement) is already handled by visit_Name:

    def visit_MatchClass(self, node):
        for kwd_attr in node.kwd_attrs:
            self.used_names.add(kwd_attr)

Limitations

  • cannot handle positional arguments (can be tricky since overwrites of __match_args__ are possible)
  • cannot handle variable definitions in case: e.g. "case [1, a, *rest]" (a and rest is not handled i.e. checked if it is used in the case body). Currently, I do not see any problem implementing it in the future.

Python Compatibility

For Python < 3.10 the visit_MatchClass should never be executed. Tests are guarded with @pytest.mark.skipif( sys.version_info < (3, 10), ...)

Related Issue

This PR is addressing the issue #276.

Checklist:

  • I have updated the documentation in the README.md file or my changes don't require an update.
  • I have added an entry in CHANGELOG.md.
  • I have added or adapted tests to cover my changes.
  • I have run tox -e fix-style to format my code and checked the result with tox -e style.

Copy link
Owner

@jendrikseipp jendrikseipp left a comment

Choose a reason for hiding this comment

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

Thanks for this very polished PR! I only have one small comment, see below.

check(v.defined_vars, ["RED", "YELLOW", "GREEN", "color"])

check(v.unused_classes, [])
check(v.unused_vars, [])
Copy link
Owner

Choose a reason for hiding this comment

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

Nice tests! Can you add one unused variable in each case, please? That is, a dataclass member that's not used in the match statement.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you, I updated the test cases.

@codecov-commenter
Copy link

codecov-commenter commented Sep 17, 2022

Codecov Report

Merging #291 (a362a25) into main (e3d2da3) will increase coverage by 0.00%.
The diff coverage is 100.00%.

@@           Coverage Diff           @@
##             main     #291   +/-   ##
=======================================
  Coverage   99.38%   99.38%           
=======================================
  Files          19       19           
  Lines         647      651    +4     
=======================================
+ Hits          643      647    +4     
  Misses          4        4           
Impacted Files Coverage Δ
vulture/core.py 99.40% <100.00%> (+<0.01%) ⬆️
vulture/whitelists/ast_whitelist.py 100.00% <100.00%> (ø)

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

@jendrikseipp jendrikseipp merged commit f060795 into jendrikseipp:main Sep 18, 2022
@jendrikseipp
Copy link
Owner

Great! Thanks @kreathon !

@exoriente
Copy link

🤩 Awesome! Nice to see this addressed! Thanks, for taking up this issue @jendrikseipp & @kreathon !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants