python / python/mypy

isinstance(x, x2.__class__) fails type check

Open
#13,918 2 comments 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

Bug Report

Mypy shows problems with taking a __class__ attribute and using this in isinstance check.

To Reproduce
Run mypy on the code below:

import itertools


class _MISSING:
    """Placeholder for missing values."""

    pass


def foo(x, x2) -> None:
    for t, t2 in itertools.zip_longest(x, x2, fillvalue=_MISSING):
        reveal_type(t)
        reveal_type(t2)
        a = isinstance(t, t2.__class__)

Expected Behavior

Check should pass

Actual Behavior

$ mypy simple.py                                                                                 (rlcore2) 12:44:04
simple.py:12: note: Revealed type is "Union[Any, def () -> simple._MISSING]"
simple.py:13: note: Revealed type is "Union[Any, def () -> simple._MISSING]"
simple.py:14: error: Argument 2 to "isinstance" has incompatible type "Union[Any, overloaded function]"; expected "Union[type, Tuple[Union[type, Tuple[Any, ...]], ...]]"  [arg-type]
Found 1 error in 1 file (checked 1 source file)

Your Environment

  • Mypy version used: 0.982
  • Mypy command-line flags: None
  • Mypy configuration options from mypy.ini (and other config files):
[tool.mypy]
warn_unused_configs = true
allow_redefinition = true
disallow_untyped_defs = false
show_error_codes = true
  • Python version used: Python 3.8.5

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 by saving the reproduction as simple.py and running mypy with the reported Python 3.8.5 setup and configuration. Trace how the checker infers t2.class for the isinstance call, then add a regression test showing that this check passes without the reported arg-type error.

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
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.