Skip to content

Special case typing.get_args() with Literals #3285

@diego-pm

Description

@diego-pm

Question

A real use case I have found is something similar to the following snippet, to not repeat the available options:

from typing import Literal, get_args

type Value = Literal['a', 'b']
VALUES = get_args(Value)

def foo(x: Value): pass

def bar(x: str):
    if x in VALUES:
        foo(x)  # invalid-argument-type

Ty check fails because the type annotation of get_args is too general ((tp: Any) -> tuple[Any, ...]).
To support this pattern, could the get_args function be special cased to better infer the resulting type, at least when the type is a Literal?

Version

ty 0.0.31

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions