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)
The text was updated successfully, but these errors were encountered:
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.)
Bug report
Your environment
Steps to reproduce:
Run the code in bug.py, attached.
bug.zip
The output on my Mac mini is:
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)
The text was updated successfully, but these errors were encountered: