-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Loosen base class attribute compatibility checks when attribute is redefined #6585
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
Changes from all commits
8e20acc
8adafdb
3a38d4b
e807d81
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -228,7 +228,7 @@ class B(A, int): pass | |
from typing import Callable, TypeVar | ||
T = TypeVar('T') | ||
class A(B, C): | ||
def f(self): pass | ||
pass | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just wanted to note that this change was necessary so that the original error would still be triggered (since redefining the method |
||
class B: | ||
@dec | ||
def f(self): pass | ||
|
@@ -461,6 +461,121 @@ class Combo(Base2, Base1): ... | |
[out] | ||
main:10: error: Definition of "NestedVar" in base class "Base2" is incompatible with definition in base class "Base1" | ||
|
||
[case testMultipleInheritance_NestedVariableOverriddenWithCompatibleType] | ||
from typing import TypeVar, Generic | ||
T = TypeVar('T', covariant=True) | ||
class GenericBase(Generic[T]): | ||
pass | ||
class Base1: | ||
Nested: GenericBase['Base1'] | ||
class Base2: | ||
Nested: GenericBase['Base2'] | ||
class A(Base1, Base2): | ||
Nested: GenericBase['A'] | ||
[out] | ||
|
||
[case testMultipleInheritance_NestedVariableOverriddenWithIncompatibleType1] | ||
from typing import TypeVar, Generic | ||
T = TypeVar('T', covariant=True) | ||
class GenericBase(Generic[T]): | ||
pass | ||
class Base1: | ||
Nested: GenericBase['Base1'] | ||
class Base2: | ||
Nested: GenericBase['Base2'] | ||
class A(Base1, Base2): | ||
Nested: GenericBase['Base1'] | ||
[out] | ||
main:10: error: Incompatible types in assignment (expression has type "GenericBase[Base1]", base class "Base2" defined the type as "GenericBase[Base2]") | ||
|
||
[case testMultipleInheritance_NestedVariableOverriddenWithIncompatibleType2] | ||
from typing import TypeVar, Generic | ||
T = TypeVar('T', covariant=True) | ||
class GenericBase(Generic[T]): | ||
pass | ||
class Base1: | ||
Nested: GenericBase['Base1'] | ||
class Base2: | ||
Nested: GenericBase['Base2'] | ||
class A(Base1, Base2): | ||
Nested: GenericBase['Base2'] | ||
[out] | ||
main:10: error: Incompatible types in assignment (expression has type "GenericBase[Base2]", base class "Base1" defined the type as "GenericBase[Base1]") | ||
|
||
[case testMultipleInheritance_NestedVariableOverriddenWithCompatibleType] | ||
from typing import TypeVar, Generic | ||
T = TypeVar('T', covariant=True) | ||
class GenericBase(Generic[T]): | ||
pass | ||
class Base1: | ||
Nested: GenericBase['Base1'] | ||
class Base2: | ||
Nested: GenericBase['Base1'] | ||
class A(Base1, Base2): | ||
Nested: GenericBase['Base1'] | ||
[out] | ||
|
||
[case testMultipleInheritance_MethodDefinitionsCompatibleWithOverride] | ||
from typing import TypeVar, Union | ||
_T = TypeVar('_T') | ||
|
||
class Flag: | ||
def __or__(self: _T, other: _T) -> _T: ... | ||
|
||
# int defines __or__ as: | ||
# def __or__(self, n: int) -> int: ... | ||
class IntFlag(int, Flag): | ||
def __or__(self: _T, other: Union[int, _T]) -> _T: ... | ||
[out] | ||
|
||
[case testMultipleInheritance_MethodDefinitionsIncompatibleOverride] | ||
from typing import TypeVar, Union | ||
_T = TypeVar('_T') | ||
|
||
class Flag: | ||
def __or__(self: _T, other: _T) -> _T: ... | ||
|
||
class IntFlag(int, Flag): | ||
def __or__(self: _T, other: str) -> _T: ... | ||
[out] | ||
main:8: error: Argument 1 of "__or__" incompatible with supertype "Flag" | ||
|
||
[case testMultipleInheritance_MethodDefinitionsCompatibleNoOverride] | ||
from typing import TypeVar, Union | ||
_T = TypeVar('_T') | ||
|
||
class Flag: | ||
def __or__(self: _T, other: _T) -> _T: ... | ||
|
||
class IntFlag(int, Flag): | ||
pass | ||
[out] | ||
|
||
[case testMultipleInheritance_MethodsReturningSelfCompatible] | ||
class A(object): | ||
def x(self) -> 'A': | ||
return self | ||
|
||
class B(object): | ||
def x(self) -> 'B': | ||
return self | ||
|
||
class C(A, B): | ||
def x(self) -> 'C': | ||
return self | ||
|
||
[case testMultipleInheritance_MethodsReturningSelfIncompatible] | ||
class A(object): | ||
def x(self) -> 'A': | ||
return self | ||
|
||
class B(object): | ||
def x(self) -> 'B': | ||
return self | ||
|
||
class C(A, B): # E: Definition of "x" in base class "A" is incompatible with definition in base class "B" | ||
pass | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for adding all the test cases! 👍 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thank you for reviewing. |
||
|
||
[case testNestedVariableRefersToSubclassOfAnotherNestedClass] | ||
class Mixin1: | ||
class Meta: | ||
|
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think in order to maintain consistency with the current behavior, you still need this break after last immediate base.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
EDIT: (non-immediate -> immediate).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See my replies below.