Skip to content

Nondeterministic type-var error with LiteralString-bounded TypeVars #20528

Open

Description

@ericbuehl

After upgrading from mypy 1.19.0 to 1.19.1, we're experiencing nondeterministic type checking behavior:

  1. Without type: ignore[type-var]: Sometimes reports "Value of type variable 'S' of 'Response' cannot be 'object'" [type-var]
  2. With type: ignore[type-var]: Sometimes reports "Unused 'type: ignore[type-var]' comment" [unused-ignore]

The error state is inconsistent and appears related to PR #20345 (which fixed issue #20344 about nondeterministic behavior with tuple type vars).

Environment

  • Mypy version: 1.19.1
  • Python version: 3.13
  • Upgraded from: mypy 1.19.0 (no errors)

Example

Granted, this is a simplified, sanitized example I have extracted from our code base but I believe it highlights the case where the issue is happening

from typing import LiteralString


class Container[S: LiteralString, T]:
    """A container with a LiteralString key."""

    def __init__(self, key: S, value: T) -> None:
        self.key = key
        self.value = value


class Response[S: LiteralString, T]:
    """A response parameterized by LiteralString."""

    def __init__(self, container: Container[S, T]) -> None:
        self.container = container


def process[S: LiteralString, S1: LiteralString](
    containers: list[Container[S, int] | Container[S1, str]],
) -> dict[int, Response]:
    """Create responses from a union of containers with different LiteralString keys."""
    return {
        i: Response(container)  # May error: type-var or unused-ignore
        for i, container in enumerate(containers)
    }

Scenario 1: With type: ignore[type-var] comment

example.py:28: error: Unused "type: ignore[type-var]" comment  [unused-ignore]

Scenario 2: Without the comment (in different contexts)

Sometimes passes with no errors, sometimes shows:

example.py:28: error: Value of type variable "S" of "Response" cannot be "object"  [type-var]

I'm not able to nail down exactly when each scenario surfaces but hopefully this is enough to go on as it otherwise presents as a regression since 1.19.0

Activity

  1. hauntsaninja commented on Jan 5, 2026

    @hauntsaninja
    Collaborator

    Thanks for the issue! Yeah, #20345 is certainly related.

    However, I can't actually reproduce any error on the snippet you provide. I ran several configurations of mypy 50 times each (master, 1.19.1, with and without --no-incremental).

    Am I missing something? Are you able to make a playground that reproduces the issue on https://mypy-play.net ?

  2. ericbuehl commented on Jan 6, 2026

    @ericbuehl
    Author

    Hi @hauntsaninja I'm struggling to get a clean, isolated repro as this is in a non-trivial chunk of proprietary code. I'll keep trying though

    What I can say from the outside is that this was the only new error finding by mypy when going from 0.19.0 to 0.19.1.

    error: Value of type variable "S" of "Response" cannot be "object"  [type-var]
    

    then, when adding ignore[misc, type-var] (there was already ignore[misc] here) I get

    error: Unused "type: ignore[type-var]" comment  [unused-ignore]
    
  3. hauntsaninja commented on Jan 6, 2026

    @hauntsaninja
    Collaborator

    Thanks. If you can find a shareable repro, will be much much easier to find a fix

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrong

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions