python / python/mypy

list of types with multiple bases only uses first shared base?

Open
#8,513 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

feature needs discussion priority-2-low
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

i may be misunderstanding something, but it seems that when making a list of types, each with multiple shared base classes, mypy mistakenly infers this to be a list of the first base only, missing others

$ cat mypytest.py
# two different base classes with different attributes
class B1:
    b1 = None


class B2:
    b2 = None


# two classes that subclass both B1 and B2
class S1(B1, B2):
    pass


class S2(B1, B2):
    pass


# a list of the subclasses
types = [S1, S2]

# it seems mypy thinks `types` is List[Type[B1]]
ok = [x.b1 for x in types]
why_not_ok = [x.b2 for x in types]
$ mypy mypytest.py
mypytest.py:24: error: "Type[B1]" has no attribute "b2"
$ mypy --version
mypy 0.770+dev.b2edab25b7e4a6131484bb6edf314e2fa078aee7
$ python -V
Python 3.7.3

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 reproduction in mypytest.py and run it with mypy to confirm the inferred type and error. Trace how the type of a list of classes with multiple shared bases is inferred, then verify that both b1 and b2 are accepted without losing the multiple-base relationship.

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
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.