Skip to content

NoReturn on __new__ and __init__ disables accessing @classmethods and ClassVars directly from the class #14044

@bzoracler

Description

@bzoracler

Bug Report

Annotating __new__(...) -> typing.NoReturn or __init__(...) -> typing.NoReturn causes false positives when accessing @classmethods and typing.ClassVar-annotated attributes.

To Reproduce

from typing import NoReturn, Any, ClassVar

class A:
    attr: ClassVar[str] = ""
    def __init__(cls, *args: Any, **kwargs: Any) -> NoReturn:
        raise RuntimeError
    @classmethod
    def f(cls) -> None:
        pass

class B:
    attr: ClassVar[str] = ""
    @classmethod
    def f(cls) -> None:
        pass

Expected Behavior

No issues on any of the following:

A.f()
A.attr
B.f()
B.attr

Actual Behavior

A.f()  # mypy: "Callable[[VarArg(Any), KwArg(Any)], NoReturn]" has no attribute "f"  [attr-defined]
A.attr  # mypy: "Callable[[VarArg(Any), KwArg(Any)], NoReturn]" has no attribute "attr"  [attr-defined]
B.f()
B.attr

Your Environment

  • Mypy version used: 0.990 and master branch of https://mypy-play.net/
  • Mypy command-line flags: None
  • Mypy configuration options from mypy.ini (and other config files): None
  • Python version used: 3.10

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