Skip to content
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

Bug: KeyError rather than TypeError in LocStream for python >=3.12 #320

Open
2 tasks done
billsacks opened this issue Nov 6, 2024 Discussed in #313 · 0 comments
Open
2 tasks done

Bug: KeyError rather than TypeError in LocStream for python >=3.12 #320

billsacks opened this issue Nov 6, 2024 Discussed in #313 · 0 comments
Assignees

Comments

@billsacks
Copy link
Member

Discussed in https://github.com/orgs/esmf-org/discussions/313

Originally posted by xylar October 27, 2024

Requirements

Affiliation(s)

LANL

ESMF Version

v8.7.0 (and earlier)

Issue

I'm trying to build ESMPy 8.7.0 on conda-forge:
conda-forge/esmpy-feedstock#85
In the tests, I am seeing a KeyError:

___________________________ TestLocStream.test_slice ___________________________

self = <esmpy.test.test_api.test_locstream.TestLocStream testMethod=test_slice>

    @pytest.mark.skipif(pet_count()!=1, reason="test must be run in serial")
    def test_slice(self):
        locstream = LocStream(5, name="Test LocStream")
    
>       locstream2 = locstream[0:2]

src/esmpy/test/test_api/test_locstream.py:95: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

self = LocStream:
    name = 'Test LocStream' 
    lower_bounds = array([0], dtype=int32) 
    upper_bounds = array([5], dtype=int32) 
    keys = dict_items([]) 

slc = slice(0, 2, None)

    def __getitem__(self, slc):
        # initialize slc_ls
        slc_ls = slc
    
        # check that this is actually a slicing operation and not just regular item retrieval
        if not isinstance(slc, str):
            # parallel slicing is not yet enabled (collective operation)
            if pet_count() > 1:
                raise SerialMethod
            # re-initialize slc_ls
            slc_ls = get_formatted_slice(slc, self.rank)
    
        # slice at will
        try:
>           ret = super(LocStream, self).__getitem__(slc_ls)
E           KeyError: slice(0, 2, None)

src/esmpy/api/locstream.py:130: KeyError

The cause of this is that slice() objects are hashable in Python >=3.12, whereas they were not in Python <3.12, and raised a TypeError. I believe the fix would be to change the try/catch to:

       # slice at will
       if not isinstance(slc_ls, slice):
           ret = super(LocStream, self).__getitem__(slc_ls)
       else:
           # as of Python 3.12, slice objects are hashable so don't
           # raise a TypeError
           ret = self.copy()
...

This at least seems to have allowed all tests to pass.

Autotag

@billsacks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant