mypy icon indicating copy to clipboard operation
mypy copied to clipboard

Mypy thinks that `class` statement always produces some class

Open denballakh opened this issue 3 years ago • 1 comments

from typing import Any
s: Any = slice(1, 2, 3)

class X(s): ...

print(X) # slice('X', (slice(1, 2, 3),), {'__module__': '__main__', '__qualname__': 'X'})
x = X() # TypeError: 'slice' object is not callable

reveal_type(x) # test.X
reveal_type(X) # def (*_args: Any, **_kwds: Any) -> test.X

At runtime X is not a class, it is instance of slice, but mypy thinks that class statement always produces some class.

Expected Behavior

  • no errors
  • revealed types Any and Any

Actual Behavior

  • no errors
  • revealed type X and def (...) -> X, what is wrong

Your Environment

  • Mypy version used: mypy 0.971 (compiled: yes)
  • Mypy command-line flags: none
  • Mypy configuration options from mypy.ini (and other config files): none
  • Python version used: 3.10.6
  • Operating system and version: Win10

denballakh avatar Sep 04 '22 10:09 denballakh

The same is true for:

  • Classs decorators that return something else
  • Custom metaclasses that return something else
  • Probably other magic :)

But, yes, this is a limitation we have for now.

sobolevn avatar Sep 05 '22 11:09 sobolevn