- 
                Notifications
    You must be signed in to change notification settings 
- Fork 46
TST: Test copy behavior in astype #335
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
          
     Draft
      
      
            lithomas1
  wants to merge
  1
  commit into
  data-apis:master
  
    
      
        
          
  
    
      Choose a base branch
      
     
    
      
        
      
      
        
          
          
        
        
          
            
              
              
              
  
           
        
        
          
            
              
              
           
        
       
     
  
        
          
            
          
            
          
        
       
    
      
from
lithomas1:test-astype-copy
  
      
      
   
  
    
  
  
  
 
  
      
    base: master
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.
          
          
  
     Draft
                    Changes from all commits
      Commits
    
    
  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
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
|  | @@ -3,9 +3,12 @@ | |
| from inspect import getfullargspec | ||
| from typing import Any, Dict, Optional, Sequence, Tuple, Union | ||
|  | ||
| from . import _array_module as xp | ||
| from hypothesis import note | ||
|  | ||
| from . import _array_module as xp, xps | ||
| from . import dtype_helpers as dh | ||
| from . import shape_helpers as sh | ||
| from . import hypothesis_helpers as hh | ||
| from . import stubs | ||
| from . import xp as _xp | ||
| from .typing import Array, DataType, Scalar, ScalarType, Shape | ||
|  | @@ -28,6 +31,7 @@ | |
| "assert_0d_equals", | ||
| "assert_fill", | ||
| "assert_array_elements", | ||
| "assert_kw_copy" | ||
| ] | ||
|  | ||
|  | ||
|  | @@ -483,6 +487,48 @@ def assert_fill( | |
| assert xp.all(xp.equal(out, xp.asarray(fill_value, dtype=dtype))), msg | ||
|  | ||
|  | ||
| def scalar_eq(s1: Scalar, s2: Scalar) -> bool: | ||
| if cmath.isnan(s1): | ||
| return cmath.isnan(s2) | ||
| else: | ||
| return s1 == s2 | ||
|  | ||
|  | ||
| def assert_kw_copy(func_name, x, out, data, copy): | ||
| """ | ||
| Assert copy=True/False functionality is respected | ||
|  | ||
| TODO: we're not able to check scalars with this approach | ||
| 
      Comment on lines
    
      +497
     to 
      +501
    
   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. This doesn't look right. 
 | ||
| """ | ||
| if copy is not None and len(x.shape) > 0: | ||
| stype = dh.get_scalar_type(x.dtype) | ||
| idx = data.draw(xps.indices(x.shape, max_dims=0), label="mutating idx") | ||
| old_value = stype(x[idx]) | ||
| scalar_strat = hh.from_dtype(x.dtype).filter( | ||
| lambda n: not scalar_eq(n, old_value) | ||
| ) | ||
| value = data.draw( | ||
| scalar_strat | scalar_strat.map(lambda n: xp.asarray(n, dtype=x.dtype)), | ||
| label="mutating value", | ||
| ) | ||
| x[idx] = value | ||
| note(f"mutated {x=}") | ||
| # sanity check | ||
| assert_scalar_equals( | ||
| "__setitem__", type_=stype, idx=idx, out=stype(x[idx]), expected=value, repr_name="x" | ||
| ) | ||
| new_out_value = stype(out[idx]) | ||
| f_out = f"{sh.fmt_idx('out', idx)}={new_out_value}" | ||
| if copy: | ||
| assert scalar_eq( | ||
| new_out_value, old_value | ||
| ), f"{f_out}, but should be {old_value} even after x was mutated" | ||
| else: | ||
| assert scalar_eq( | ||
| new_out_value, value | ||
| ), f"{f_out}, but should be {value} after x was mutated" | ||
|  | ||
|  | ||
| def _has_functional_signbit() -> bool: | ||
| # signbit can be available but not implemented (e.g., in array-api-strict) | ||
| if not hasattr(_xp, "signbit"): | ||
|  | ||
  
    
      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
    
  
  
    
              
      
      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.
  
    
  
    
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.
We should definitely be able to check 0-dimensional arrays. What we can't check is np.generic, which AFAIK is a numpy-specific type