Skip to content

Functions returning a TypeGuard value are incorrectly propagating the TypeGuard as their return type #1998

@Wizzerinus

Description

@Wizzerinus

Describe the Bug

from typing import TypeGuard


def is_int(x: int | str) -> TypeGuard[int]:
    return isinstance(x, int)


class X:
    def __init__(self, param: int | str):
        self.param = param

    def has_int(self):
        return is_int(self.param)

This reports X::has_int as an invalid TypeGuard function, because it is detected to return a TypeGuard, and has no parameters. Seems to work fine if the function does not always return the typeguard.

Sandbox Link

https://pyrefly.org/sandbox/?project=N4IgZglgNgpgziAXKOBDAdgEwEYHsAeAdAA4CeS4ATrgLYAEALqcROgOZ0Q3G6UN0AVZjADiAV1SVMAHXSzZmGGE5wA%2BqwYAKfIk7p%2BAHzpwGlAJR0AtAD5Bw8ZMwBtDQF1Esul7qUYDMZToKqwmGADGMNoANHoMZvJy6GFQqHBwdAAaHkHeisqq6ugQDAWacDBQYDHEkqg0uhp0Ribm2d7txhVgJLX0ALx0NZR1Ce15dAAWqYVa5ZVmbR1evv6BKjNlXT3DNPHoIFEgYgzQcCTkiCAAxHQAqidQxaR0YGJJJ7jocAnjYLw0qBK6DENGwMEo2ga%2BgsNmMpkWyz8ASCYGkIAAciCwZRdMB8ABfNGyA4gMi%2BMBQUiEBi0KAUG4ABVI5MpxgwOAIdDCn0gbACgIgn0IshuAGUYDBJgwGMQ4IgAPTyslKSmEXhseUwdDyzC4MJweXc9C8-kfbUvXh0VAAN1Q0FQ2FgXJ5ED5wzNdFwxDNZ1kZAYE0%2Blmt4LggqCAzRAGZCABGABMRP2%2BMOqDCJxDADFoDAKGgsHgiGQQPigA

(Only applicable for extension issues) IDE Information

No response

Metadata

Metadata

Assignees

Labels

good first issueGood for newcomersnarrowingIssues with narrowing - root cause is usually narrowing, flow handling, or bothtypechecking

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions