patma icon indicating copy to clipboard operation
patma copied to clipboard

Type annotations or not?

Open gvanrossum opened this issue 5 years ago • 7 comments

We could support type annotations in patterns, like so:

match x:
    case [a: int, b: str]: print(f"An int {a} and a string {b}:)
    case [a: int, b: int, c: int]: print(f"Three ints", a, b, c)
    ...

However this makes the syntax for patterns fundamentally different from that of expressions. Also, it allows multiple ways of saying the same thing, since presumably we could also write

match x:
    case [int(a), str(b)]: print(f"An int {a} and a string {b}:)
    case [int(a), int(b), int(c)]: print(f"Three ints", a, b, c)
    ...

or:

match x:
    case [a := int(), b := str()]: print(f"An int {a} and a string {b}:)
    case [a := int(), b := int(), c := int()]: print(f"Three ints", a, b, c)
    ...

I still like the clarity of [a: int, b: str] over the other variations.

Another downside would be that you need extra parentheses if you're matching a single value of a given type (not a sequence of length 1):

match x:
    case str(a): ...

or

match x:
    case a := str(): ...

while with an annotation you need parentheses:

match x:
    case (a: str): ...

This becomes especially bothersome inside other patterns, e.g.

match x:
    case Foo(arg1=(a: str), arg2=(b: str)): ...

compared to

match x:
    case Foo(arg1=str(a), arg2=str(b)): ...

gvanrossum avatar Apr 11 '20 21:04 gvanrossum

The argument for keeping the syntax of patterns the same as that for expressions, but giving it a (quite!) different semantics, seems weak, since the semantics really are so different already -- e.g. 1|2 in a pattern doesn't mean the same as 3, it matches either 1 or 2.

Presumably this means that we shouldn't allow any other operators (e.g. 1+2 should be disallowed), except unary - (for negative numbers only). Oh, and if we support complex numbers we should allow + to add a real and an imaginary part, e.g. 1+0j.

gvanrossum avatar Apr 11 '20 21:04 gvanrossum

More arguments against type annotations:

  • Annotations are for static checkers, but matching is runtime behavior.
  • What should case [x: list[int]] check? isinstance(x, list) and all(isinstance(a, int) for a in x)?
  • What about case [x: list[T]]?

gvanrossum avatar Apr 12 '20 06:04 gvanrossum

FWIW, impossibility to use list[int] is the main reason why I don't like the annotation syntax.

ilevkivskyi avatar May 14 '20 16:05 ilevkivskyi

So what was the final judgment on this? I know it was marked as rejected, but it's not clear from the conversation why.

viridia avatar Jun 09 '20 04:06 viridia

Mostly because of TOOWTDI, and because we don't know what we would do with things like list[int] -- Python currently disallows isinstance(x, list[int]), and it would be yet another source of inconsistency.

gvanrossum avatar Jun 09 '20 16:06 gvanrossum

OK after reading this a couple more time I think I misunderstood what was being proposed. You were proposing to use a syntax similar to type annotations to replace or substitute the pattern expression syntax proposed in other threads.

However, that is not what I thought you were proposing - I thought the proposal was to add type annotations in addition to the pattern expression. Something like this:

  case [a := list: list[int], b := str]:

or possibly

  case [a: list[int] := list, b := str]:

What this is saying is that the pattern expression matches a list, and should be assigned to the variable a; and, oh by the way you static checkers, the type of that list just happens to be list[int]. Like all other type annotations, it has no effect on runtime and is only meaningful to static type checkers.

The problem with this, of course, is that it doesn't work for patterns not enclosed in brackets:

  case a := list: list[int]:

...because now you have an ambiguous grammar on colon.

viridia avatar Jun 10 '20 06:06 viridia

Yeah, I think the "obvious" way to type extracted variables is to explicitly annotate them before the match block. Colons/parentheses already have multiple meanings in patterns, and giving them any more just looks like unreadable screen-grit-soup to me. :slightly_smiling_face:

brandtbucher avatar Jun 10 '20 06:06 brandtbucher