python / python/mypy

Mypy thinks that `class` statement always produces some class

Open
#13,609 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the minimal example in the issue and reproduce its reveal_type output using mypy. Trace the class-statement analysis and handling of the dynamic base expression; done means the example reports Any for both x and X while preserving the no-errors result.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
devtools
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.