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

Support using not with a context manager #21

Closed
kylebarron opened this issue Oct 1, 2020 · 2 comments · Fixed by #23
Closed

Support using not with a context manager #21

kylebarron opened this issue Oct 1, 2020 · 2 comments · Fixed by #23

Comments

@kylebarron
Copy link
Member

kylebarron commented Oct 1, 2020

Currently there's a significant amount of initialization done in __enter__ instead of __init__. This means that code will silently fail if not used with a context manager:

scene = S2COGReader('S2B_12SYJ_20200901_0_L2A')
# no error
scene.center
# errors:
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-77-5a7975690aa4> in <module>
----> 1 self.center

<ipython-input-43-587f5a19de47> in center(self)
     41         """Dataset center + minzoom."""
     42         return (
---> 43             (self.bounds[0] + self.bounds[2]) / 2,
     44             (self.bounds[1] + self.bounds[3]) / 2,
     45             self.minzoom,

AttributeError: 'S2COGReader' object has no attribute 'bounds'

I believe __init__ should be used for any necessary initialization of the class. __enter__ should only instantiate context-specific attributes like a file descriptor or a database cursor. Such a context really doesn't apply here, and __enter__ is essentially on there for syntactical sugar. Hence initialization should be move entirely to __init__.

@vincentsarago
Copy link
Member

We built rio_tiler.io.BaseReader with only __enter__ and __exit__ to force the user to use context manager. I understand there is no need for CTX in rio-tiler-pds, mostly because we don't need to close or delete any resource. but I'd like to keep this syntax. I'm ok for moving stuff outisde the __enter__ but I'll keep the documentation as it is.

Note: because we are using attrs we don't need __init__ but we will need instead a __attrs_post_init__ method.

@vincentsarago
Copy link
Member

@kylebarron We can move forward once cogeotiff/rio-tiler#271 will be merged ;-)

This was referenced Oct 1, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants