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

ENH: adding minimalist BaseVOQuery baseclass #2836

Merged
merged 6 commits into from
Sep 27, 2023

Conversation

bsipocz
Copy link
Member

@bsipocz bsipocz commented Sep 20, 2023

Inheriting a lot of unused methods, properties, etc from BaseQuery makes little sense for modules that rely fully on pyvo rather than making their own GET/POST queries.

This PR also ensures that the session set in the class is being passed onto TAPServcie rather than using a new one from pyvo. cadc is basically doing this already, the PR also adds the same approach to alma, nexsci and irsa.

To merge this, I would like to see approvals or thumbs-ups from the affected modules: alma and nexsci

cc @andamian @rickynilsson

@bsipocz bsipocz added the query astroquery internals label Sep 20, 2023
@bsipocz bsipocz added this to the v0.4.7 milestone Sep 20, 2023
@codecov
Copy link

codecov bot commented Sep 20, 2023

Codecov Report

Merging #2836 (46d9ed5) into main (6eefe6d) will increase coverage by 0.02%.
The diff coverage is 86.20%.

@@            Coverage Diff             @@
##             main    #2836      +/-   ##
==========================================
+ Coverage   66.49%   66.51%   +0.02%     
==========================================
  Files         235      235              
  Lines       18081    18096      +15     
==========================================
+ Hits        12023    12037      +14     
- Misses       6058     6059       +1     
Files Coverage Δ
astroquery/cadc/core.py 81.11% <100.00%> (ø)
astroquery/alma/core.py 55.57% <66.66%> (ø)
astroquery/ipac/irsa/core.py 75.00% <66.66%> (ø)
...roquery/ipac/nexsci/nasa_exoplanet_archive/core.py 57.55% <66.66%> (ø)
astroquery/query.py 64.23% <94.44%> (+1.68%) ⬆️

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

@andamian
Copy link

Excellent idea. Glad to see the third VO usage in here. The BaseVOQuery class could eventually include auth functionality but that is still being worked on in the IVOA.

@bsipocz - there are a few self._request calls in the cadc.core and alma.core which will stop working. They could probably be replaced with direct requests calls.

@bsipocz
Copy link
Member Author

bsipocz commented Sep 20, 2023

Excellent idea. Glad to see the third VO usage in here. The BaseVOQuery class could eventually include auth functionality but that is still being worked on in the IVOA.

Yes, I'm all for adding all other useful VO related functionality to this base class, but first really just wanted to cut out the clutter as e.g. having all the cache-related things there is more confusing than useful.

@bsipocz
Copy link
Member Author

bsipocz commented Sep 20, 2023

As for alma and cadc and also for the exoplanet_archive: I've seen that they do some non-VO self._request usage, which is all fine as I kept those classes to be subclasses of both the bases, so with this PR both this works and the only functional change for them is the User-Agent has changed and now hopefully consistently sending something like
astroquery/0.4.7.dev8868 pyVO/1.5.dev192+g75bcb73 Python/3.10.8 (Darwin) python-requests/2.28.1

@bsipocz
Copy link
Member Author

bsipocz commented Sep 20, 2023

@andamian - So, as I see, the main question here is whether you see any problems with passing the session from the instances onto pyvo rather than defaulting it using a new one.

@andamian
Copy link

I would have to review both alma and cadc, more specifically their login methods. At least in cadc they seem to bypass the base session opting for PyVO new authsessions instead. This is where the astroquery header can go missing. That needs to be fixed.
alma on the other hand seems to be using self._session but that it's through self._request (core.py: 292) so it's not authenticating the right session.
Maybe we need to offer login and logout functionality in the BaseVOQuery or at least setter and getters to the session that we can augment with the right agent. The idea would be that the users (or packages such as alma and cadc) create auth sessions and register them with the base class.
A&A is a big subject in IVOA bright now but a clear solution is not yet available so we can only speculate at this point.

@bsipocz
Copy link
Member Author

bsipocz commented Sep 22, 2023

alma on the other hand seems to be using self._session but that it's through self._request (core.py: 292) so it's not authenticating the right session.

But that's the same self._session it inherits from the baseclass, so all should be good, isn't it? (it's just not being passed fwd to pyvo)

@andamian
Copy link

alma on the other hand seems to be using self._session but that it's through self._request (core.py: 292) so it's not authenticating the right session.

But that's the same self._session it inherits from the baseclass, so all should be good, isn't it? (it's just not being passed fwd to pyvo)

Is it? The session auth is set in self._request() method which belongs to the BaseQuery which has its own _session. So in this case alma is authenticating on BaseQuery._session but using BaseVOQuery._session for PyVO calls, no?

@bsipocz
Copy link
Member Author

bsipocz commented Sep 22, 2023

BaseQuery._session but using BaseVOQuery._session for PyVO calls, no

Indeed, I'll need to double-check the inheritance hierarchy then, and add a test for it. The intention was to use BaseVOQuery._session's only when there is no other one around, it definitely shouldn't override the authenticated ones.

Copy link
Member Author

@bsipocz bsipocz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahh, I cannot request changes on my own PR 😭

"""
def __init__(self):
super().__init__()
self._session = requests.Session()
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, I suppose putting this into a conditional should be sufficient to address the issue @andamian pointed out.

But it all needs thorough testing.

@andamian
Copy link

Maybe CadcClass and AlmaClass should only inherit from BaseVOQuery if they are not supposed to find anything else useful in BaseQuery?

It's confusing to have all these sessions flying around- which one is auth, which ones was created externally, etc? So it's nice to have one set in the base class and shared by the implementations. But is that practical? Can one session be shared in a multithread environment (multiple downloads for ex)? It's not very clear whether requests.Session is thread safe psf/requests#2766.

It would be useful I think to add a factory create_session method to the BaseVOQuery for external users to get astroquery prepped sessions. And we'll have to remember to use it all the time in the implementation.

@bsipocz
Copy link
Member Author

bsipocz commented Sep 26, 2023

Made this a little bit more robust, and now I believe it's good to go.

As for the bigger picture question:

t's confusing to have all these sessions flying around- which one is auth, which ones was created externally, etc? So it's nice to have one set in the base class and shared by the implementations. But is that practical? Can one session be shared in a multithread environment (multiple downloads for ex)?

I don't have a good answer for this, but the current inconsistency feels more ad-hoc than on purpose, and definitely not consistent over astroquery or even within pyvo, so if we switch over from having one shared version we need to do it purposefully and as consistently as possible (and preferably we should leave some doc trail about it ;) )

else:
user_agents = [f"astroquery/{version.version} pyVO/{pyvo.__version__} "
f"Python/{platform.python_version()} ({platform.system()})"] + user_agents

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not potentially repeating the Python version?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Potentially yes, but it's already the case in the current main, I just made sure that we don't do this if we (==astroquery or pyvo) already modified the user-agent. The one corner case that I haven't covered if the session is somehow made in pyvo, and thus only have the pyvo version listed, but I don't think that will ever be the case here.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PyVO might be used to create auth sessions if that is going to be standardized eventually. It already has authsession.AuthSession although that is a bit outdated.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I see correctly, then authsession.AuthSession doesn't modify the headers. Though yes, I suppose the above logic should better be fixed in case it would modify it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The latest commit should cover the case when PyVO already modified the user-agent.

Some other tools might have already added the python and platform specifics, too, but I think we should just roll with that and add our own, too, and thus duplicate if that's the case.

@bsipocz bsipocz merged commit abeabdc into astropy:main Sep 27, 2023
@bsipocz bsipocz deleted the ENH_basevoquery branch December 8, 2023 18:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
query astroquery internals
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants