最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

structural pattern matching - Using Python's matchcase statement to check for a list of a given type - Stack Overflow

programmeradmin6浏览0评论

I'm trying to use a match/case instruction to process things differently based on type:

Code looks like:

def checkArg(arg: str | list[str]) -> str:
    match arg:
        case str():
            return 'str'
        case list():
            return 'list(str)'
        case _:
            return "unmatched"

def test(arg: str | list[str]):
    print(f'{arg=!r}: {checkArg(arg)}')

test(0)
test('This should match')
test(['This', 'should', 'match'])
test(['This', 'should', 0, 'not', 'match'])
  • As expected type hinting makes PyCharm flag test(0)
  • But I don't get any warning for test(['This', 'should', 0, 'not', 'match']) so I could be missing something already
  • I can't find what I could use instead of case list() to reject the 4th test and restrict the match to proper list of strings (I tried things like case list[str]() but either the syntax is not valid or that doesn't work).

I'm trying to use a match/case instruction to process things differently based on type:

Code looks like:

def checkArg(arg: str | list[str]) -> str:
    match arg:
        case str():
            return 'str'
        case list():
            return 'list(str)'
        case _:
            return "unmatched"

def test(arg: str | list[str]):
    print(f'{arg=!r}: {checkArg(arg)}')

test(0)
test('This should match')
test(['This', 'should', 'match'])
test(['This', 'should', 0, 'not', 'match'])
  • As expected type hinting makes PyCharm flag test(0)
  • But I don't get any warning for test(['This', 'should', 0, 'not', 'match']) so I could be missing something already
  • I can't find what I could use instead of case list() to reject the 4th test and restrict the match to proper list of strings (I tried things like case list[str]() but either the syntax is not valid or that doesn't work).
Share Improve this question edited Mar 21 at 10:14 mkrieger1 23.5k7 gold badges64 silver badges82 bronze badges asked Mar 21 at 9:52 xenoidxenoid 9,0464 gold badges28 silver badges52 bronze badges 6
  • 2 it's like you define a test function with str type argu, but you are passing 0 which is an int? – Coco Q. Commented Mar 21 at 10:04
  • pass '0' instead of 0 or write your function that it also accepts ints as input – Bending Rodriguez Commented Mar 21 at 10:05
  • 2 @CocoQ. This was done to test that the type hinting works... – xenoid Commented Mar 21 at 10:06
  • FWIW, in VSCode (so, Pylance actually) I do get a warning: Argument of type "list[str | int]" cannot be assigned to parameter "arg" of type "str | list[str]" in function "test" – matszwecja Commented Mar 21 at 10:11
  • 1 Mypy also flags test(['This', 'should', 0, 'not', 'match']) so I guess this is a problem in your PyCharm setup (Mypy playground) – mkrieger1 Commented Mar 21 at 10:18
 |  Show 1 more comment

1 Answer 1

Reset to default 0

You can add a condition that validates all items as instances of str:

def checkArg(arg: str| list[str]) -> str:
    match arg:
        case str():
            return 'str'
        case [*items] if all(isinstance(item, str) for item in items):
            return 'list(str)'
        case _:
            return "unmatched"

Demo: https://ideone/pHVPRI

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论