Skip to content

Mypyc AssertionError: Self`0 #17793

@CoolCat467

Description

@CoolCat467

Bug Report

Mypyc raises AssertionError: Self`0 in NamedTuple subclass

To Reproduce

vector.py

from __future__ import annotations

from typing import TYPE_CHECKING, NamedTuple

from typing_extensions import Self

if TYPE_CHECKING:  # pragma: nocover
    from collections.abc import Iterable


class Vector2(NamedTuple):
    """Vector2 Object. Takes an x and a y coordinate."""

    x: float
    y: float

    @classmethod
    def from_iter(cls, iterable: Iterable[float]) -> Self:
        """Return new vector from iterable."""
        return cls(*iter(iterable))

    def __neg__(self) -> Self:
        """Return result of negating self components."""
        return self.from_iter(-c for c in self)
mypyc vector.py

Expected Behavior

Expected no issues

Actual Behavior

Traceback (most recent call last):
vector.py:24: AssertionError: Self`0

Changing the erroring line in question to be

def __neg__(self) -> Vector2:

instead results in

Traceback (most recent call last):
vector.py:24: AssertionError: tuple[builtins.float, builtins.float, fallback=vector.Vector2]

Your Environment

  • Mypy version used: mypy 1.11.2 (compiled: yes)
  • Mypy command-line flags: None
  • Mypy configuration options from mypy.ini (and other config files): None
  • Python version used: Python 3.12.3 (main, Sep 11 2024, 14:17:37) [GCC 13.2.0] on linux

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions