i2mint / py2store

Tools to create simple and consistent interfaces to complicated and varied data sources.
MIT License
11 stars 2 forks source link

Handle missing keys better. #56

Open thorwhalen opened 4 years ago

thorwhalen commented 4 years ago

Several things came to my attention during the development of py2store, whose prior knowledge would have probably made me make different choices. There is a need to comb through the code and find where these better choices should be made.

(1) I didn't know about __missing__, which can probably be used advantageously.

(2) If a class has a __getitem__, but not a __iter__, it still uses __getitem__ on integers to possibly create iterable. This seems to be a not-so-great legacy from the past. But it's still there. I wouldn't want to create code that relies on it, but probably better to make sure we disable iteration when there's not an explicit __iter__, to avoid bugs or confusing behavior. I got one already when doing list(misc_objs) and got:

TypeError: expected str, bytes or os.PathLike object, not int

See:

(3) python docs says: Note for loops expect that an IndexError will be raised for illegal indexes to allow proper detection of the end of the sequence. Sometimes I decided to raise the "local" error (such as FileNotFound, etc.). This is also useful because the user IndexError alone would hide the real origin from the user or handling code. Perhaps the right error to raise should be a type('Specific', (FileNotFound, IndexError), {})?