Skip to content

Segmentation fault on callable protocol with Self annotationΒ #16629

@ryo-tagami

Description

@ryo-tagami

Crash Report

mypy crashes with segmentation fault as such:

mypy-segmentation-fault on ξ‚  main is πŸ“¦ v0.1.0 via 🐍 v3.11.6 (mypy-segmentation-fault-py3.11)
❯ mypy --version
mypy 1.7.1 (compiled: yes)

mypy-segmentation-fault on ξ‚  main is πŸ“¦ v0.1.0 via 🐍 v3.11.6 (mypy-segmentation-fault-py3.11)
❯ mypy -p mypy_segmentation_fault
zsh: segmentation fault  mypy -p mypy_segmentation_fault

with mypy_segmentation_fault.py:

from datetime import UTC, datetime
from typing import Protocol, Self

from injector import Binder, Injector, Module


class NowGenerator(Protocol):
    def __call__(self: Self) -> datetime:
        ...


class NowGeneratorImpl:
    def __call__(self: Self) -> datetime:
        return datetime.now(tz=UTC)


class GeneratorsModule(Module):
    def configure(self: Self, binder: Binder) -> None:
        binder.bind(
            NowGenerator,  # type: ignore[type-abstract]
            to=NowGeneratorImpl,
        )


injector = Injector([GeneratorsModule])
print(injector.get(NowGenerator)())  # type: ignore[type-abstract]

My observation is that if I remove Self type annotation, mypy does not crash and I do realise it is not necessary
(though, it was a good way to avoid hitting ruff's missing-type-self rule, which in theory I could disable).
I don't know if I could reproduce the crash without using python-injector.

Traceback

Unfortunately, nothing useful shows up:

mypy --show-traceback -p mypy_segmentation_fault
zsh: segmentation fault  mypy --show-traceback -p mypy_segmentation_fault

To Reproduce

Your Environment

  • Mypy version used: 1.7.1
  • Mypy command-line flags: -p mypy_segmentation_fault
  • Mypy configuration options from mypy.ini (and other config files): None
  • Python version used: 3.11.6
  • Operating system and version: macOS Sonoma Version 14.1.2 (23B92) on Apple M2 Pro

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions