Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

“Duplicate base class” blocks checking #15349

Closed
xiaxinmeng opened this issue Jun 2, 2023 · 4 comments · Fixed by #15367
Closed

“Duplicate base class” blocks checking #15349

xiaxinmeng opened this issue Jun 2, 2023 · 4 comments · Fixed by #15367

Comments

@xiaxinmeng
Copy link

The duplicate base class causes the checking blocked.

example.py

class N(type):
    pass

class B(N, metaclass=N):
    pass

class D(B, B):
    pass

The expected behavior:

The error can be ignored and the checking will not be blocked.

The actual behavior:

>> mypy example.py
Desktop/example.py:7: error: Duplicate base class "B"
Found 1 error in 1 file (errors prevented further checking)

Environment

Mypy version: 1.3.0
Python version: 3.11.3
Operating System: Ubuntu 18.04
How to install MyPy: "python3.11 -m pip install mypy"

@gregorysantosa
Copy link
Contributor

@xiaxinmeng I believe mypy is correct in this case because when class D is constructed, since it inherits two instances of class B, when an attribute or method of class N is invoked from an instance of class D, Python does not know which instance of class N should be called. I believe this issue can be worked around by removing the "metaclass=N" argument from class B

@sobolevn
Copy link
Member

sobolevn commented Jun 4, 2023

I think that this is a correct thing to do:

  1. It is a TypeError in runtime:
>>> class A: ...
... 
>>> class B(A, A): ...
... 
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: duplicate base class A

So, you app won't work and mypy catches that.

  1. It is not clear how to construct D type info for us, so we can continue type checking

@JelleZijlstra
Copy link
Member

Can't we make it inherit from Any or ignore the second base? I feel we should strongly avoid blocking errors if possible.

@sobolevn
Copy link
Member

sobolevn commented Jun 4, 2023

We can still inherit from one of them, I guess. I will take a look and send a PR.

sobolevn added a commit that referenced this issue Jun 5, 2023
Continue type checking if a class has two or more identical bases.
Closes #15349

---------

Co-authored-by: hauntsaninja <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants