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

Common type in container is determined using constructor args #11659

Open
robertschweizer opened this issue Dec 3, 2021 · 1 comment
Open
Labels
bug mypy got something wrong

Comments

@robertschweizer
Copy link

Bug Report

To Reproduce

from typing import Type, List


class NonLiskovChild(Exception):
    """Does not match Exception.__init__() signature"""
    def __init__(self, _some_other_arg: object) -> None:
        super().__init__()


types1 = [TypeError, ValueError]
reveal_type(types1)
# Revealed type is 'builtins.list[def (*args: builtins.object) -> builtins.Exception]'

types2 = [TypeError, NonLiskovChild]
reveal_type(types2)
# Revealed type is 'builtins.list[builtins.type*]'

# When explicitly setting the type, mypy accepts NonLiskovChild as an Exception subtype:
types3: List[Type[Exception]] = [TypeError, NonLiskovChild]
reveal_type(types3)
# Revealed type is 'builtins.list[Type[builtins.Exception]]'

Expected Behavior

The lowest common type should be determined by inheritance, not with __init__() signature matching.

Otherwise stdlib classes like CalledProcessError do not work correctly, e.g. in this example:

import pytest
from subprocess import CalledProcessError

with pytest.raises((TypeError, CalledProcessError)) as exc_info:
    pass
# error: Need type annotation for "exc_info"
# error: Argument 1 to "raises" has incompatible type "Tuple[Type[TypeError], Type[CalledProcessError]]"; expected "Union[Type[<nothing>], Tuple[Type[<nothing>], ...]]"

Your Environment

  • Mypy version used: 0.910
  • Mypy command-line flags:
  • Mypy configuration options from mypy.ini (and other config files):
  • Python version used: 3.8.10
  • Operating system and version: Windows 10
@robertschweizer robertschweizer added the bug mypy got something wrong label Dec 3, 2021
@robertschweizer robertschweizer changed the title Common type in container is determined using constructor (__init__) args Common type in container is determined using constructor args Dec 3, 2021
@robertschweizer
Copy link
Author

A similar discussion is going on in #11486, which complains that most of Mypy does not follow Liskov's Subsitution Principle for constructors. I guess I found a part that does.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong
Projects
None yet
Development

No branches or pull requests

1 participant