- 
                Notifications
    
You must be signed in to change notification settings  - Fork 174
 
fix Feature: inheritance checks for typed dictionaries #1346
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
          
     Closed
      
      
    
      
        
          +244
        
        
          −32
        
        
          
        
      
    
  
  
     Closed
                    Changes from all commits
      Commits
    
    
            Show all changes
          
          
            4 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      
    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
          Some comments aren't visible on the classic Files Changed page.
        
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
    
  
  
    
              
  
    
      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 | 
|---|---|---|
| 
          
            
          
           | 
    @@ -1780,6 +1780,100 @@ impl<'a, Ans: LookupAnswer> AnswersSolver<'a, Ans> { | |
| .is_some_and(|metadata| metadata.fields.contains_key(field_name)) | ||
| } | ||
| 
     | 
||
| fn typed_dict_field_info( | ||
| &self, | ||
| metadata: &ClassMetadata, | ||
| field_name: &Name, | ||
| field: &ClassField, | ||
| ) -> Option<TypedDictField> { | ||
| metadata | ||
| .typed_dict_metadata() | ||
| .and_then(|typed_dict| typed_dict.fields.get(field_name)) | ||
| .and_then(|is_total| field.clone().as_typed_dict_field_info(*is_total)) | ||
| } | ||
| 
     | 
||
| fn validate_typed_dict_field_override( | ||
| &self, | ||
| child_cls: &Class, | ||
| child_metadata: &ClassMetadata, | ||
| parent_cls: &Class, | ||
| parent_metadata: &ClassMetadata, | ||
| field_name: &Name, | ||
| child_field: &ClassField, | ||
| parent_field: &ClassField, | ||
| range: TextRange, | ||
| errors: &ErrorCollector, | ||
| ) -> bool { | ||
| let Some(child_info) = self.typed_dict_field_info(child_metadata, field_name, child_field) | ||
| else { | ||
| return true; | ||
| }; | ||
| let Some(parent_info) = | ||
| self.typed_dict_field_info(parent_metadata, field_name, parent_field) | ||
| else { | ||
| return true; | ||
| }; | ||
| 
     | 
||
| let parent_mutable = !parent_info.is_read_only(); | ||
| let child_mutable = !child_info.is_read_only(); | ||
| 
     | 
||
| if parent_mutable { | ||
| if !child_mutable { | ||
| self.error( | ||
| errors, | ||
| range, | ||
| ErrorInfo::Kind(ErrorKind::BadTypedDictKey), | ||
| format!( | ||
| "TypedDict field `{field_name}` in `{}` cannot be marked read-only; parent TypedDict `{}` defines it as mutable", | ||
| child_cls.name(), | ||
| parent_cls.name() | ||
| ), | ||
| ); | ||
| return false; | ||
| } | ||
| if parent_info.required && !child_info.required { | ||
| self.error( | ||
| errors, | ||
| range, | ||
| ErrorInfo::Kind(ErrorKind::BadTypedDictKey), | ||
| format!( | ||
| "TypedDict field `{field_name}` in `{}` must remain required because parent TypedDict `{}` defines it as required", | ||
| child_cls.name(), | ||
| parent_cls.name() | ||
| ), | ||
| ); | ||
| return false; | ||
| } | ||
| if !parent_info.required && child_info.required { | ||
| self.error( | ||
| errors, | ||
| range, | ||
| ErrorInfo::Kind(ErrorKind::BadTypedDictKey), | ||
| format!( | ||
| "TypedDict field `{field_name}` in `{}` cannot be made required; parent TypedDict `{}` defines it as non-required", | ||
| child_cls.name(), | ||
| parent_cls.name() | ||
| ), | ||
| ); | ||
| return false; | ||
| } | ||
| } else if parent_info.required && !child_info.required { | ||
| self.error( | ||
| errors, | ||
| range, | ||
| ErrorInfo::Kind(ErrorKind::BadTypedDictKey), | ||
| format!( | ||
| "TypedDict field `{field_name}` in `{}` cannot be made non-required; parent TypedDict `{}` defines it as required", | ||
| child_cls.name(), | ||
| parent_cls.name() | ||
| ), | ||
| ); | ||
| return false; | ||
| } | ||
| 
     | 
||
| true | ||
| } | ||
| 
     | 
||
| 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 | ||
| 
          
            
          
           | 
    @@ -1855,8 +1949,8 @@ impl<'a, Ans: LookupAnswer> AnswersSolver<'a, Ans> { | |
| let mut got_attribute = None; | ||
| let mut parent_attr_found = false; | ||
| let mut parent_has_any = false; | ||
| let is_typed_dict_field = | ||
| self.is_typed_dict_field(&self.get_metadata_for_class(cls), field_name); | ||
| let metadata = self.get_metadata_for_class(cls); | ||
| let is_typed_dict_field = self.is_typed_dict_field(metadata.as_ref(), field_name); | ||
| 
     | 
||
| let bases_to_check: Box<dyn Iterator<Item = &ClassType>> = if bases.is_empty() { | ||
| // If the class doesn't have any base type, we should just use `object` as base to ensure | ||
| 
          
            
          
           | 
    @@ -1935,6 +2029,21 @@ impl<'a, Ans: LookupAnswer> AnswersSolver<'a, Ans> { | |
| // keys but not regular fields. | ||
| continue; | ||
| } | ||
| if is_typed_dict_field | ||
| && !self.validate_typed_dict_field_override( | ||
| cls, | ||
| metadata.as_ref(), | ||
| parent_cls, | ||
| parent_metadata.as_ref(), | ||
| field_name, | ||
| class_field, | ||
| &want_class_field, | ||
| range, | ||
| errors, | ||
| ) | ||
| { | ||
| continue; | ||
| } | ||
| // Substitute `Self` with derived class to support contravariant occurrences of `Self` | ||
| let want_attribute = self.as_instance_attribute( | ||
| &want_class_field, | ||
| 
          
            
          
           | 
    @@ -2005,38 +2114,56 @@ impl<'a, Ans: LookupAnswer> AnswersSolver<'a, Ans> { | |
| 
     | 
||
| /// For classes with multiple inheritance, check that fields inherited from multiple base classes are consistent. | ||
| pub fn check_consistent_multiple_inheritance(&self, cls: &Class, errors: &ErrorCollector) { | ||
| // Maps field from inherited class | ||
| struct InheritedFieldInfo { | ||
                
      
                  asukaminato0721 marked this conversation as resolved.
               
          
            Show resolved
            Hide resolved
         | 
||
| class: Class, | ||
| metadata: Arc<ClassMetadata>, | ||
| field: ClassField, | ||
| ty: Type, | ||
| } | ||
| 
     | 
||
| let mro = self.get_mro_for_class(cls); | ||
| let mut inherited_fields: SmallMap<&Name, Vec<(&Name, Type)>> = SmallMap::new(); | ||
| let mut inherited_fields: SmallMap<&Name, Vec<InheritedFieldInfo>> = 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(); | ||
| let class_object = parent_cls.class_object(); | ||
| let class_fields = class_object.fields(); | ||
| let parent_metadata = self.get_metadata_for_class(class_object); | ||
| 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()); | ||
| if current_class_fields.contains(field) { | ||
| continue; | ||
| } | ||
| let key = KeyClassField(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) { | ||
| continue; | ||
| } | ||
| let Some(field_entry) = field_entry.as_ref() else { | ||
| continue; | ||
| }; | ||
| if let Some(parent_member) = self.get_class_member(class_object, field) { | ||
| let parent_field = Arc::unwrap_or_clone(parent_member.value.clone()); | ||
| inherited_fields | ||
| .entry(field) | ||
| .or_default() | ||
| .push((parent_cls.name(), field_entry.ty())); | ||
| .push(InheritedFieldInfo { | ||
| class: class_object.dupe(), | ||
| metadata: parent_metadata.clone(), | ||
| field: parent_field, | ||
| ty: field_entry.ty(), | ||
| }); | ||
| } | ||
| } | ||
| } | ||
| 
     | 
||
| for (field_name, class_and_types) in inherited_fields.iter() { | ||
| if class_and_types.len() > 1 { | ||
| let types: Vec<Type> = class_and_types.iter().map(|(_, ty)| ty.clone()).collect(); | ||
| let child_metadata = self.get_metadata_for_class(cls); | ||
| 
     | 
||
| for (field_name, inherited_field_infos_by_ancestor) in inherited_fields.iter() { | ||
| if inherited_field_infos_by_ancestor.len() > 1 { | ||
| let types: Vec<Type> = inherited_field_infos_by_ancestor | ||
| .iter() | ||
| .map(|info| info.ty.clone()) | ||
| .collect(); | ||
| if types.iter().any(|ty| { | ||
| matches!( | ||
| ty, | ||
| 
        
          
        
         | 
    @@ -2046,8 +2173,6 @@ impl<'a, Ans: LookupAnswer> AnswersSolver<'a, Ans> { | |
| | Type::Overload(_) | ||
| ) | ||
| }) { | ||
| // TODO(fangyizhou): Handle bound methods and functions properly. | ||
| 
         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. Don't think this should be removed --- I still plan to come back to this  | 
||
| // This is a leftover from https://github.com/facebook/pyrefly/pull/1196 | ||
| continue; | ||
| } | ||
| let intersect = self.intersects(&types); | ||
| 
        
          
        
         | 
    @@ -2058,11 +2183,11 @@ impl<'a, Ans: LookupAnswer> AnswersSolver<'a, Ans> { | |
| ), | ||
| "Inherited types include:".to_owned() | ||
| ]; | ||
| for (cls, ty) in class_and_types.iter() { | ||
| for info in inherited_field_infos_by_ancestor.iter() { | ||
| error_msg.push(format!( | ||
| " `{}` from `{}`", | ||
| self.for_display(ty.clone()), | ||
| cls | ||
| self.for_display(info.ty.clone()), | ||
| info.class.name() | ||
| )); | ||
| } | ||
| errors.add( | ||
| 
        
          
        
         | 
    @@ -2071,6 +2196,38 @@ impl<'a, Ans: LookupAnswer> AnswersSolver<'a, Ans> { | |
| error_msg, | ||
| ); | ||
| } | ||
| 
     | 
||
| if let Some(child_member) = self.get_class_member(cls, field_name) { | ||
| let child_field = Arc::unwrap_or_clone(child_member.value.clone()); | ||
| if self | ||
| .typed_dict_field_info(child_metadata.as_ref(), field_name, &child_field) | ||
| .is_some() | ||
| { | ||
| for info in inherited_field_infos_by_ancestor { | ||
| if self | ||
| .typed_dict_field_info( | ||
| info.metadata.as_ref(), | ||
| field_name, | ||
| &info.field, | ||
| ) | ||
| .is_some() | ||
| && !self.validate_typed_dict_field_override( | ||
| cls, | ||
| child_metadata.as_ref(), | ||
| &info.class, | ||
| info.metadata.as_ref(), | ||
| field_name, | ||
| &child_field, | ||
| &info.field, | ||
| cls.range(), | ||
| errors, | ||
| ) | ||
| { | ||
| continue; | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| 
          
            
          
           | 
    ||
  
    
      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
    
  
  
    
              
  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.
  
    
  
    
Uh oh!
There was an error while loading. Please reload this page.