#11794·pyright

Types silently degrade to Unknown when elif branch count is too high

Author: jernstCreated Sep 22, 2026Updated Sep 22, 2026
Labelsbug
  • pyright 1.1.410
  • Python 3.14.6
  • Linux aarch64 (7.2.4-1-aarch64)
  • typeCheckingMode: strict

Summary

In strict mode, a function containing many (if/elif) branches, each calling the same function whose signature involves a recursive type alias (e.g. a JSON-value union JsonElement = None | bool | int | float | str | list[JsonElement] | dict[str, JsonElement]), type-checks cleanly up to some branch count, then — with no warning, no "too complex" diagnostic, nothing — silently starts reporting every argument in every branch as Unknown (reportUnknownArgumentType), including arguments that have nothing to do with the recursive alias (e.g. str(_e) on a plain Exception subclass).

On my machine the exact threshold is 33 branches: 32 is clean, 33 produces 66 errors, and the pattern scales roughly linearly with branch count beyond that (35→70, 47→94, ...).

This is dangerous because it's a silent complexity cliff: adding one more elif branch to an existing, clean file makes pyright suddenly report dozens of spurious reportUnknownArgumentType/reportUnknownMemberType errors throughout the function, none of which reflect a real type problem — deleting the type annotations does not fix it, and neither does simplifying any individual branch. Only reducing the number of branches (or restructuring away from one giant if/elif chain) makes it go away.

Minimal repro

Generator script (32 branches = clean, 33 = fails):

python
def gen(n: int) -> str:
    lines = [
        "type JsonPrimitive = bool | int | float | str",
        "type JsonElement = None | JsonPrimitive | JsonList | JsonObject",
        "type JsonList = list[JsonElement]",
        "type JsonObject = dict[str, JsonElement]",
        "",
        "class JsonRpcRequest:",
        "    method: str",
        "    id: JsonElement | None",
        "",
        "class JsonRpcResponse:",
        "    @classmethod",
        '    def failure(cls, id: JsonElement | None, code: int, message: str, data: JsonElement | None = None) -> "JsonRpcResponse":',
        "        return cls()",
        "",
        "class InsufficientPrivilegesException(Exception):",
        "    CODE: int = -32001",
        "",
        "def dispatch(request: JsonRpcRequest) -> object | None:",
    ]
    for i in range(n):
        kw = "if" if i == 0 else "elif"
        lines += [
            f'    {kw} request.method == "m{i}":',
            f'        try:',
            f'            do_something_{i}()',
            f'        except InsufficientPrivilegesException as _e:',
            f'            return JsonRpcResponse.failure(request.id, InsufficientPrivilegesException.CODE, str(_e))',
            f'        except Exception as _e:',
            f'            return JsonRpcResponse.failure(request.id, -32603, str(_e))',
        ]
    lines.append("    return None")
    lines.append("")
    for i in range(n):
        lines.append(f"def do_something_{i}() -> None: ...")
    return "\n".join(lines) + "\n"

open("repro.py", "w").write(gen(33))  # change to 32 to see it pass

pyrightconfig.json:

json
{ "typeCheckingMode": "strict", "pythonVersion": "3.14" }

Run

pyright --project pyrightconfig.json repro.py

Expected: 0 errors, 0 warnings, 0 informations (same as with n=32, and same as with n=33 if you remove the recursive alias and just use id: int).

Actual (n=33): 66 errors, all reportUnknownArgumentType / reportUnknownMemberType, e.g.:

repro.py:23:98 - error: Argument type is unknown
    Argument corresponds to parameter "object" in function "__new__" (reportUnknownArgumentType)

— on str(_e), where _e is a plain Exception subclass with no relation to JsonElement at all. The errors appear starting from the second branch (m1) onward; the first branch is always clean.

Notes

  • Removing the recursive type alias (replacing JsonElement with e.g. int) makes the exact same 33-branch file type-check cleanly — so the recursive alias is necessary to trigger it, but the failure surfaces on unrelated arguments in the same branches once triggered.