8000 Loosen base class attribute compatibility checks when attribute is redefined by syastrov · Pull Request #6585 · python/mypy · GitHub
[go: up one dir, main page]

Skip to content

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

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Restore the current behavior regarding incompatible type in assignment.
  • Loading branch information
syastrov committed Apr 11, 2019
commit 3a38d4b320f0fe58db129eeadf1c98476b0746ea
6 changes: 6 additions & 0 deletions mypy/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -1870,6 +1870,9 @@ def check_compatibility_all_supers(self, lvalue: RefExpr, lvalue_type: Optional[
# Show only one error per variable
break

direct_bases = lvalue_node.info.direct_base_classes()
last_immediate_base = direct_bases[-1] if direct_bases else None

for base in lvalue_node.info.mro[1:]:
# Only check __slots__ against the 'object'
# If a base class defines a Tuple of 3 elements, a child of
Expand All @@ -1893,6 +1896,9 @@ def check_compatibility_all_supers(self, lvalue: RefExpr, lvalue_t CD45 ype: Optional[
# Only show one error per variable; even if other
# base classes are also incompatible
return True
if base == last_immediate_base:
# At this point, the attribute was found to be compatible with all immediate parents,
break
return False

def check_compatibility_super(self, lvalue: RefExpr, lvalue_type: Optional[Type],
Expand Down
2 changes: 0 additions & 2 deletions test-data/unit/check-classes.test
Original file line number Diff line number Diff line change
Expand Up @@ -3725,7 +3725,6 @@ class C(B):
a = "a"
[out]
main:4: error: Incompatible types in assignment (expression has type "str", base class "A" defined the type as "int")
main:6: error: Incompatible types in assignment (expression has type "str", base class "A" defined the type as "int")

[case testVariableSubclassTypeOverwriteImplicit]
class A:
Expand Down Expand Up @@ -4018,7 +4017,6 @@ class B(A):
class C(B):
x = ''
[out]
main:6: error: Incompatible types in assignment (expression has type "str", base class "A" defined the type as "int")

[case testClassIgnoreType_RedefinedAttributeTypeIgnoredInChildren]
class A:
Expand Down
0