`NoReturn` on `__new__` and `__init__` disables accessing `@classmethod`s and `ClassVar`s directly from the class
Open
Nobody has claimed this yet.
bug
- Dominant language
- Python
- Stars
- 20.6k
- Forks
- 3.3k
- PR merge metrics
- PR metrics pending
Description
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
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the provided Python reproducer and trace how mypy represents classes whose new or init returns NoReturn. Add regression coverage for classmethod and ClassVar access, ensuring A.f(), A.attr, B.f(), and B.attr produce no issues.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- devtools
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 42/100