Skip to content

Pyrefly has issues with type aliases and base classes. BasedPyright does not #3394

@OutSquareCapital

Description

@OutSquareCapital

Describe the Bug

I have issues with pyrefly in one of my project, specifically here

Minimal repro:

import ast
import itertools
from collections.abc import Callable, Iterable, Iterator
from typing import TypeIs

type Parsed = tuple[str, str, int]
type IsDef = ast.FunctionDef | ast.ClassDef


def flat_map[T, R](data: Iterable[T], func: Callable[[T], Iterable[R]]) -> Iterator[R]:
    return itertools.chain.from_iterable(map(func, data))


def _is_def(n: object) -> TypeIs[IsDef]:
    return isinstance(n, ast.FunctionDef | ast.ClassDef)


def _extract_all_docs(node: IsDef, prefix: str) -> Iterator[Parsed]:
    full_name = f"{prefix}{node.name}" if prefix else node.name
    match node:
        case ast.ClassDef():
            yield from (
                flat_map(
                    filter(_is_def, node.body),
                    lambda n: _extract_all_docs(n, f"{full_name}."),
                )
            )
        case _:
            return

output:

PS C:\Users\tibo\python_codes\doctester> uvx basedpyright t.py 
0 errors, 0 warnings, 0 notes
PS C:\Users\tibo\python_codes\doctester> uvx pyrefly check t.py
ERROR Argument `stmt` is not assignable to parameter `node` with type `ClassDef | FunctionDef` in function `_extract_all_docs` [bad-argument-type]
  --> t.py:25:49
   |
25 |                     lambda n: _extract_all_docs(n, f"{full_name}."),
   |                                                 ^
   |
 INFO 1 error
PS C:\Users\tibo\python_codes\doctester> 

unfortunately I just can't make it work in the sandbox due to unavailability of typing::TypeIs. I tried a few things but always stumbled on errors.
But it's really trivial to repro in any Python 3.13 environnement

Sandbox Link

No response

(Only applicable for extension issues) IDE Information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions