-
Notifications
You must be signed in to change notification settings - Fork 161
Inconsistent inheritance check follow ups #1298
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
Open
fangyi-zhou
wants to merge
5
commits into
facebook:main
Choose a base branch
from
fangyi-zhou:inconsistent-inheritance-check-follow-up
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
6fccf4a
Skip multiple inheritance check for fields that are overridden
fangyi-zhou badc31d
[nfc] Refactor class override consistency check condition
fangyi-zhou b290fd1
Skip multiple inheritance checks for "special" fields
fangyi-zhou b2de588
Patch typeshed for inconsistent param name in importlib
fangyi-zhou 4407868
Patch another typeshed issue in importlib
fangyi-zhou File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1754,30 +1754,18 @@ impl<'a, Ans: LookupAnswer> AnswersSolver<'a, Ans> { | |
.is_some_and(|metadata| metadata.fields.contains_key(field_name)) | ||
} | ||
|
||
pub fn check_consistent_override_for_field( | ||
&self, | ||
cls: &Class, | ||
field_name: &Name, | ||
class_field: &ClassField, | ||
bases: &ClassBases, | ||
errors: &ErrorCollector, | ||
) { | ||
let is_override = class_field.is_override(); | ||
if matches!(class_field.1, IsInherited::No) && !is_override { | ||
return; | ||
} | ||
|
||
fn should_check_field_for_override_consistency(&self, field_name: &Name) -> bool { | ||
// Object construction (`__new__`, `__init__`, `__init_subclass__`) should not participate in override checks | ||
if field_name == &dunder::NEW | ||
|| field_name == &dunder::INIT | ||
|| field_name == &dunder::INIT_SUBCLASS | ||
{ | ||
return; | ||
return false; | ||
} | ||
|
||
// `__hash__` is often overridden to `None` to signal hashability | ||
if field_name == &dunder::HASH { | ||
return; | ||
return false; | ||
} | ||
|
||
// TODO(grievejia): In principle we should not really skip `__call__`. But the reality is that | ||
|
@@ -1789,21 +1777,41 @@ impl<'a, Ans: LookupAnswer> AnswersSolver<'a, Ans> { | |
// -- any overrider must be able to take ANY arguments which can't be practical. We need to either | ||
// special-case typeshed or special-case callable subtyping to make `__call__` override check more usable. | ||
if field_name == &dunder::CALL { | ||
return; | ||
return false; | ||
} | ||
|
||
// Private attributes should not participate in overrload checks | ||
// Private attributes should not participate in override checks | ||
if field_name.starts_with("__") && !field_name.ends_with("__") { | ||
return; | ||
return false; | ||
} | ||
|
||
// TODO: This should only be ignored when `cls` is a dataclass | ||
if field_name == &dunder::POST_INIT { | ||
return; | ||
return false; | ||
} | ||
|
||
// TODO: This should only be ignored when `_ignore_` is defined on enums | ||
if field_name.as_str() == "_ignore_" { | ||
return false; | ||
} | ||
|
||
true | ||
} | ||
|
||
pub fn check_consistent_override_for_field( | ||
&self, | ||
cls: &Class, | ||
field_name: &Name, | ||
class_field: &ClassField, | ||
bases: &ClassBases, | ||
errors: &ErrorCollector, | ||
) { | ||
let is_override = class_field.is_override(); | ||
if matches!(class_field.1, IsInherited::No) && !is_override { | ||
return; | ||
} | ||
|
||
if !self.should_check_field_for_override_consistency(field_name) { | ||
return; | ||
} | ||
|
||
|
@@ -1969,13 +1977,24 @@ impl<'a, Ans: LookupAnswer> AnswersSolver<'a, Ans> { | |
// Maps field from inherited class | ||
let mro = self.get_mro_for_class(cls); | ||
let mut inherited_fields: SmallMap<&Name, Vec<(&Name, Type)>> = SmallMap::new(); | ||
let current_class_fields: SmallSet<_> = cls.fields().collect(); | ||
|
||
for parent_cls in mro.ancestors_no_object().iter() { | ||
let class_fields = parent_cls.class_object().fields(); | ||
for field in class_fields { | ||
// Skip fields that are not relevant for override consistency checks | ||
if !self.should_check_field_for_override_consistency(field) { | ||
continue; | ||
} | ||
let key = KeyClassField(parent_cls.class_object().index(), field.clone()); | ||
let field_entry = self.get_from_class(cls, &key); | ||
if let Some(field_entry) = field_entry.as_ref() { | ||
// Skip fields that are overridden in the current class, | ||
// they will have been checked by | ||
// `check_consistent_override_for_field` already | ||
if current_class_fields.contains(field) { | ||
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. i think we can lift this check up to the beginning of the loop body, it's a bit buried nested in here |
||
continue; | ||
} | ||
inherited_fields | ||
.entry(field) | ||
.or_default() | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
dataclasses' post_init as well