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
{{ message }}
Repository navigation
Inconsistent errors when reassigning Union[list|Callable] in subclass #12126
Given that ChildClassB and ChildClassC passes with no issues, I would expect ChildClassD to also pass, however mypy raises the error:
main.py:23: error: Signature of "example" incompatible with supertype "BaseClass"
This seems to be unexpected as ChildClassC and ChildClassD should be equivalent. From my understanding it should also be equal to ChildClassB but it uses a lambda function so I suppose it is not the same.
Also if we change the annotation on BaseClass.example to example: Callable[[T], list[int]] | list[int] (swapping Any to bounded T) then both ChildClassC and ``ChildClassB` raise an error:
main.py:20: error: Incompatible types in assignment (expression has type "Callable[[ChildClassC], List[int]]", base class "Class" defined the type as "Union[Callable[[T], List[int]], List[int]]")
main.py:23: error: Signature of "example" incompatible with supertype "BaseClass"
From what I understand this is intended, however in that case shouldn't ChildClassB also fail?
changed the title [-]Inconsistent errors when reassigning `Union[ list | callable ]` in subclass[/-][+]Inconsistent errors when reassigning `Union[list|Callable]` in subclass[/+]on Feb 4, 2022
Actually I think this one should stay open. Although it now gives errors more consistently, it is not the best we can do. If we have a union of callable with non-callable, we need to handle bind_self() separately on both items of the union.
OK, on current master it should work without errors if you will use ClassVar in the explicit annotations you have (according to PEP 526 an explicit annotation in class body automatically makes a variable an instance variable).
Activity
[-]Inconsistent errors when reassigning `Union[ list | callable ]` in subclass[/-][+]Inconsistent errors when reassigning `Union[list|Callable]` in subclass[/+]ilevkivskyi commentedon Mar 31, 2025
Actually I think this one should stay open. Although it now gives errors more consistently, it is not the best we can do. If we have a union of callable with non-callable, we need to handle
bind_self()separately on both items of the union.Use checkmember.py to check variable overrides (python#18847)
ilevkivskyi commentedon Apr 3, 2025
OK, on current master it should work without errors if you will use
ClassVarin the explicit annotations you have (according to PEP 526 an explicit annotation in class body automatically makes a variable an instance variable).