question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Token is inconsistent in py3.10 pattern matching

See original GitHub issue

Describe the bug

Token is not working correctly with structural pattern matching.

While you can create tokens as:

t = Token(type_="TEST", value="test")

you have to pattern match on:

match t:
    case Token(type="TEST"):
        pass

As pattern matching tries to access the attributes by names provided to the pattern.

I find this solution fairly confusing and ugly, as well as hard to work with type checkers and triggers a nasty bug in pylance (https://github.com/microsoft/pylance-release/issues/3197).

To Reproduce

t = Token(type_="TEST", value="test")

match t:
    case Token(type_="TEST"):
        print("with underscore") # expected match
    case Token(type="TEST"):
        print("without underscore") # actual match

Potential solutions The simplest solution would be to rename type/type_ to be consistent between the __init__ argument and the attribute, but this might be a fairly big change to the API. There might be a possibility to add custom pattern matching logic so that matching works on Token(type_=...), but I’m not very familiar with the pattern matching API yet.

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:12 (12 by maintainers)

github_iconTop GitHub Comments

1reaction
MegaIngcommented, Aug 15, 2022

I would say that both should be done.

1reaction
MegaIngcommented, Aug 15, 2022

No, IMO the behavior that is achived with type, value is good: You can ignore the type if you are matching as str, which is the expected behavior. I feared that case a would not behave correctly, but if it does I think we should keep it with that.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Mismatch between `__init__` arg name and matching attribute ...
Mismatch between __init__ arg name and matching attribute results ... Token is inconsistent in py3.10 pattern matching lark-parser/lark#1180.
Read more >
What's New In Python 3.10 — Python 3.11.1 documentation
While structural pattern matching can be used in its simplest form comparing a variable to a literal in a case statement, its true...
Read more >
How to use Structural Pattern Matching to match built-in types?
Hello,. I'm trying to use SPM to test if some variable is an int or a str and I can't seem to make...
Read more >
match a token or rule N times - Stack Overflow
In Context free grammars how do you match a token or rule N times. ... Grammar in nltk or elsewhere that can be...
Read more >
What's great about Python 3.10? - Trey Hunner
python3.10 temp.py 70 Traceback (most recent call last): File ... To the same snippet refactored to use structural pattern matching: ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found