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

Structural pattern matching data corruption bug #93059

Open
JohnLockwood opened this issue May 21, 2022 · 1 comment
Open

Structural pattern matching data corruption bug #93059

JohnLockwood opened this issue May 21, 2022 · 1 comment
Labels
type-bug

Comments

@JohnLockwood
Copy link

@JohnLockwood JohnLockwood commented May 21, 2022

Bug report

Your environment

  • CPython versions tested on: 3.10.2, 3.11.0a6
  • Operating system and architecture: macOS Monterey, Version 12.3.1. Mac mini running M1, Apple silicon.

Steps to reproduce:

Run the code in bug.py, attached.

bug.zip

The output on my Mac mini is:

% python bug.py
Cheese Sandwich

Search for 'match' in the code. Notice that one of the terms matches
the only str in the list, "Cheese Sandwich". Later we create a dataclass
of type PersonHeight. On the last line of the file, we print the type
of one of the fields in a PersonHeight object. Note that it is not <class 'str'> as it should be,
but "Cheese Sandwich"

If we comment out the match expression, we get the correct behavior
(see comment below in code)

@JohnLockwood JohnLockwood added the type-bug label May 21, 2022
@JelleZijlstra
Copy link
Member

@JelleZijlstra JelleZijlstra commented May 21, 2022

The code in the sample boils down to this:

match "Cheese Sandwich":
    case str as s: pass

print(str)

The case str as s: pattern binds the "Cheese Sandwich" object to both the name str and the name s. You likely wanted to write case str() as s: instead, which matches to instances of the class str.

This is behaving as expected, but the behavior is indeed surprising. Perhaps we could give a SyntaxWarning for an as-pattern where the LHS is a name-pattern, or this could be handled in linters. (cc @brandtbucher for pattern matching.)

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

No branches or pull requests

2 participants