You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
fromtypingimportType, ListclassNonLiskovChild(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:
importpytestfromsubprocessimportCalledProcessErrorwithpytest.raises((TypeError, CalledProcessError)) asexc_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
The text was updated successfully, but these errors were encountered:
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
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.
Bug Report
To Reproduce
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:Your Environment
mypy.ini
(and other config files):The text was updated successfully, but these errors were encountered: