Files
pydantic/tests/test_structural_pattern_matching.py
rafael 7344374dd0 BaseModel: Document and test structural pattern matching (#3921)
* BaseModel: Add __match_args__ attribute to namespace

* changelog

* amend docs

* add tests

* Update changes/3920-irgolic.md

Co-authored-by: Samuel Colvin <samcolvin@gmail.com>

* Update .github/workflows/ci.yml

Co-authored-by: Samuel Colvin <samcolvin@gmail.com>

* rename test_match => test_structural_pattern_matching

* pytest.skip => pytest.mark.skipif

* Revert "BaseModel: Add __match_args__ attribute to namespace"

This reverts commit 79367356fddac90b047b10620dc895425ec25b40.

* adjust tests for kwarg only structural pattern matching

* adjust docs for kwarg only pattern matching

* changes: Support => Document

* fix docs?

Co-authored-by: Samuel Colvin <samcolvin@gmail.com>
2022-08-16 08:47:17 +02:00

30 lines
666 B
Python

import sys
import pytest
@pytest.mark.skipif(sys.version_info < (3, 10), reason='requires python 3.10 or higher')
def test_match_kwargs(create_module):
module = create_module(
# language=Python
"""
from pydantic import BaseModel
class Model(BaseModel):
a: str
b: str
def main(model):
match model:
case Model(a='a', b=b):
return b
case Model(a='a2'):
return 'b2'
case _:
return None
"""
)
assert module.main(module.Model(a='a', b='b')) == 'b'
assert module.main(module.Model(a='a2', b='b')) == 'b2'
assert module.main(module.Model(a='x', b='b')) is None