Skip to content

__match_args__ annotations #5402

Closed
Closed
@freundTech

Description

@freundTech

Python 3.10 adds the match statement and with it adds the __match_args__ attribute to some classes.
To statically check class patterns in match statements the value of that attribute must be statically known.

I'm currently implementing match statement support for mypy and would like to hear some opinions on how __match_args__ should be annotated.

Let's take ast.BinOp as an example. It's __match_args__ is ('left', 'op', 'right').

One option to annotate it would be

__match_args__: Final[Tuple[Literal["left"], Literal["op"], Literal["right"]]]

That is however very verbose and not easy to read. Mypy can also deal with annotations in the form of

__match_args__: Final = ("left", "op", "right")

, inferring that the strings must be literal. This approach is much easier to read, but might be harder for type checkers to support and breaks with established typeshed conventions.

I would like to hear some opinions on those two approaches and other suggestions, especially from developers of the other type checkers.

A second point of discussion would be actually adding the annotations. In theory they could be added by hand, however that would be a lot of work. Ideally a program could be written to look up the match args at runtime and automatically add them to the stubs.

People who might be interested in this:
@erictraut

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions