Skip to content

dmypy + torch: second run produces different results (Final name must be initialized with a value) #17652

Closed

Description

@randolf-scholz

Today I tried using dmypy for the first time and noticed a really strange bug: the second run produced different results than the first run!
More specifically (and even stranger) the error message "Final name must be initialized with a value" spreads from a known false positive (#5608) to other cases.

To Reproduce

I created a repository with complete repro: https://github.com/randolf-scholz/dmypy_bug
It seems 3 things are relevant:

  1. Using python 3.12.
    (I believe this is because the 3.12 wheel of torch==2.4.0 ships with the py.typed marker, but the 3.11 wheel doesn't)
  2. torch must be imported.
  3. A dataclass using a Final annotation is defined.
minimal working example
__all__ = ["ImmutablePoint", "MyDataClass"]

from typing import Final
from dataclasses import dataclass

import torch

@dataclass
class MyDataClass:
    foo: Final[int]
    bar: Final[int]


class ImmutablePoint:
    x: Final[int]
    y: Final[int]

    def __init__(self, x: int, y: int) -> None:
        super().__init__()
        self.x = x
        self.y = y

Expected Behavior

Subsequent runs should yield the same results.

Actual Behavior

Subsequent spread the error from cases where it is expected to cases where it is not.

$ dmypy start
Daemon started
$ dmypy check src
src/dmypy_final_bug/__init__.py:10: error: Final name must be initialized with a value  [misc]
src/dmypy_final_bug/__init__.py:11: error: Final name must be initialized with a value  [misc]
Found 2 errors in 1 file (checked 1 source file)
$ dmypy check src
src/dmypy_final_bug/__init__.py:10: error: Final name must be initialized with a value  [misc]
src/dmypy_final_bug/__init__.py:11: error: Final name must be initialized with a value  [misc]
src/dmypy_final_bug/__init__.py:15: error: Final name must be initialized with a value  [misc]
src/dmypy_final_bug/__init__.py:16: error: Final name must be initialized with a value  [misc]
Found 4 errors in 1 file (checked 1 source file)

Your Environment

  • python==3.12.4
  • mypy==1.11.1
  • torch==2.4.0

Activity

  1. added a commit that references this issue on Apr 12, 2025
    1ba23f1
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

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions