python / python/mypy

"Invalid base class" when inheriting from tuple of classes

Open
#5,928 2 comments 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

I appreciate there are some complexities around handling inheritance from dynamic sources, so it's possible that this is a duplicate of one of those (#4284 seems most likely related), though I think this is a slightly different case.

I would like the following to pass type checking, yet I get an "Invalid base class" error even after adding an explicit type annotation.

Because in this case the tuple is of a fixed size (and the members of the tuple are all valid types) it feels like it should be possible to determine that it's valid to inherit from an expansion of that tuple.

# foo.py
from typing import Tuple, Type

class A: pass
class B: pass

PlainBases = (A, B)
reveal_type(PlainBases)
# ^ Revealed type is 'Tuple[def () -> foo.A, def () -> foo.B]'

class PlainC(*PlainBases):
# ^ Invalid base class
    pass

AnotatedBases = (A, B)  # type: Tuple[Type[A], Type[B]]
reveal_type(AnotatedBases)
# ^ Revealed type is 'Tuple[Type[foo.A], Type[foo.B]]'

class AnotatedC(*AnotatedBases):
# ^ Invalid base class
    pass

I'm using Python 3.5 and mypy 0.620. Having just updated to 0.641 I see that there are now also errors that "Can use starred expression only as assignment target" in the above, which goes some way to explaining why this is currently unsupported, though leads to another one (apologies if this is documented somewhere):
Why is it that mypy is unable to expand the types of this starred expression?

Is this a result of the issue relating to multiple passes which is mentioned in https://github.com/python/mypy/issues/4284#issuecomment-346908509 or something else?

Is this something which is possible/likely to be supported?

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 reproducing the starred-base examples from the issue with the referenced mypy versions, then read the discussion in issue #4284 about multiple passes. The work is complete only when the expected handling of fixed tuples in class bases is decided and implemented or its unsupported behavior is clearly documented with appropriate checks.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
32/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.